I have the following code inside rules - action (being triggered by webform submission)
global $user;
$product_id = 1;
// Create the new order in checkout; you might also check first to
// see if your user already has an order to use instead of a new one.
$order = commerce_order_new($user->uid , 'checkout_checkout');
// Save the order to get its ID.
commerce_order_save($order);
// Load whatever product represents the item the customer will be
// paying for and create a line item for it.
$product = commerce_product_load($product_id);
$line_item = commerce_product_line_item_new($product, 1, $order->order_id);
// Save the line item to get its ID.
commerce_line_item_save($line_item);
// 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($order);
// Redirect to the order's checkout form. Obviously, if this were a
// form submit handler, you'd just set $form_state['redirect'].
drupal_goto('checkout/' . $order->order_id);
the code works perfectly when i am logedin, however when i submit the webform as not authenticated user, i get sent to checkout/12, I get page not found.
there is no errors in the log, and i see in the backend that the order was created... please help...
Comments
Comment #1
Anonymous (not verified) commentedNeeded to add the following line:
$_SESSION['commerce_cart_orders'][] = $order->order_id;between saving the order and redirecting.
Comment #2
arosboro commentedThanks, this solved the same issue I was having using the same code as above with a rules action.
Comment #3
tlangston commentedHow can I set this via the rules ui?
Comment #4
arosboro commentedtlangston, I had to create a custom rules action which I did in a feature. Make a module with a rules action that sets the session variable and then use that action in the rules ui.
Comment #5
raphael apard commented