0

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->uid);
  commerce_order_delete($order_id); //delete the present order
  $newOrder = commerce_order_new($user->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->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->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?

  • Check your server logs, they will contain a proper error message. – Clive Oct 30 '13 at 13:28
1

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()

  • You're right. I changed that, an now I get a different error pop up: An AJAX HTTP error occured: Debugging information follows. Path /system/ajax StatusText: error ResponseText: EntityMetadataWrapperException: MIssing data values. in EntityMetadataWrapper->value() line 83 etc – user1015214 Oct 30 '13 at 13:45
  • It doesn't look like you've initialised the $order variable – Clive Oct 30 '13 at 13:46
  • your right, edited that above. But still have the same error. – user1015214 Oct 30 '13 at 13:51
  • You still haven't initialised an $order variable. The problems you're having are all to do with incorrectly named variables. Do yourself a favour - delete the code you have, and start again from fresh, paying close attention to the names of the variables you're creating, and how you're then using them – Clive Oct 30 '13 at 13:55
  • sorry, did a full edit now. I think it might work, just not sure yet because I think I have some rules set up which are messing with the order as well. – user1015214 Oct 30 '13 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.