trying to dynamically change drupal commerce order I am trying to create a pane on the drupal commerce checkout page which will will give the user some options to change which product is in the cart. I want this done with ajax, without leaving the checkout page.

This is the code I have written so far:

  global $user;
  $product_id = 3;
  $order_id = commerce_cart_order_id($user-&gt;uid);
  commerce_order_delete($order_id); //delete the present order
  $newOrder = commerce_order_new($user-&gt;uid); //and create a new one
  commerce_order_save($newOrder);   //NEED?
  $product = commerce_product_load($product_id);  
  $lineItem = commerce_product_line_item_new($product, 1, $newOrder-&gt;order_id);
  commerce_line_item_save($lineItem); //NEED?

  // Add the line item to the order using fago's rockin' wrapper.
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $order_wrapper-&gt;commerce_line_items[] = $line_item;

  // Save the order again to update its line item reference field.
  commerce_order_save($newOrder);
  commerce_cart_order_refresh($newOrder);

  return $form;


However, this code gives me an ajax error


  Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://example.com/system/ajax


Am I making a code error? A Commerce error?
Going out on a limb a bit...

There's no function called commerce_new_order, you probably mean commerce_order_new()

Same thing with commerce_load_product() and commerce_product_load()