Duplicating an order
Hi all,
I am currently pulling my hair out trying to add a button to my website that will allow me to duplicate an order.
I took the code from the module "Commerce Reorder" which is displayed below:
function commerce_reorder_helper($order = NULL, $account = NULL) {
if (!isset($order)) {
return;
}
if (empty($account)) {
global $user;
$account = $user;
}
// Get the line items of the order.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
if (in_array($line_item_wrapper->type->value(), commerce_product_line_item_types())) {
$product = $line_item_wrapper->commerce_product->value();
if ($product->status) {
$line_item = $line_item_wrapper->value();
// Generate a line item product based in the current one.
$new_line_item = commerce_product_line_item_new($product, $line_item->quantity, $line_item->order_id, $line_item->data, $line_item->type);
// Merge both line items to get the fields (if any).
$new_line_item = (object) array_merge((array) $line_item, (array) $new_line_item);
// @TODO Add option to combine / add sepparately.
// See @commerce_cart_product_add
commerce_cart_product_add($account->uid, $new_line_item);
}
else {
drupal_set_message(t('Some products weren\'t copied to the cart as they aren\'t currently available'), 'status', FALSE);
}
}
else {
drupal_set_message(t('Some products weren\'t copied to the cart as they aren\'t currently available'), 'status', FALSE);
}
}
}
I have changed a few things with this code to make it work with my current set-up:
1) I took the code and put it in another function which is called within an AJAX call
2) I changed the "commerce_product_line_item_new" function with a custom one which passes more variables
3) I added true & cart to the function: "commerce_cart_product_add($account->uid, $new_line_item, 'true', 'cart');
I think that is all that I have changed (as far as I am aware). The code above works properly and successfully adds the products to commerce_line_item and commerce_order but the error comes when I attempt to view the new product(s) in the cart. I get a site error. The information in watchdog is:
a:6:{s:5:"%type";s:30:"EntityMetadataWrapperException";s:8:"!message";s:77:"Unable to get the data property data as the parent data structure is not set.";s:9:"%function";s:42:"EntityStructureWrapper->getPropertyValue()";s:5:"%file";s:79:"C:\xampp\htdocs\websitet\sites\all\modules\entity\includes\entity.wrapper.inc";s:5:"%line";i:442;s:14:"severity_level";i:3;}
Now, can anyone tell me why this is happening and what I can do to avoid it? I have printed out the DATA variable before it is saved in the database via my ajax call and a normal call via commerce_cart_add_to_cart_form_submit and they are identical, so why does it say it doesn't exist?
I hope someone can shed some light on this problem as I am completely baffled, many thanks!