4

I have the following code inside rules - action (being triggered by webform submission)

<?php
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...

  • I don't use cart module, i am following this example commerceguys.com/blog/creating-orders-drupal-commerce-api. 12 is just example its the order id, every time i submit it increments, the url is being generated by the commerce if you don't have a cart module activated... – Nathan Bomshteyn May 15 '12 at 15:29
  • Looking at the code for commerce_checkout_menu(), it looks like the only reason you'd get a 404 is if an order with the provided id doesn't exist. Have you confirmed the order is definitely being created successfully before the redirect happens? – Clive May 15 '12 at 15:36
  • when i go to the admin/commerce/orders it is there... – Nathan Bomshteyn May 15 '12 at 15:49
  • Does the anonymous user role have the access checkout permission? – Clive May 15 '12 at 15:56
  • yes, when the permission is off i get "access denied" instead of page not found – Nathan Bomshteyn May 15 '12 at 16:03
7

You need to add the order to the session, see commerce_checkout_access().

Something like this should work:

$_SESSION['commerce_cart_orders'][] = $order->order_id;

between saving the order and redirecting.

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.