I want to add a product to the cart programmatically on form submit

My code below works only for root, but in my site an anonymous user can access the cart and checkout, but when submitting form with non user=1 it returns Your shopping cart is empty.

In my submit function:

$uid = ($user->uid) ? $user->uid : 0;
    $order = commerce_order_new($uid, 'checkout_checkout');

// Save the order to get its ID.

// 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.
// 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.
//    ym(commerce_cart_order_id($uid));
    //commerce_cart_product_add($uid, $line_item);
    //$form_state['redirect'] = 'cart';

Why does it not work correctly with non root (uid=1)?

  • leading towards standard permissions issue here, so does this work with other users other than anonymous? eg, another authorized/logged-in user? or does it work only with your root uid 1 user?
    – Jimajamma
    Commented Mar 5, 2014 at 13:36
  • The requested page "/nashrtest/checkout/22" could not be found for anonymous user, and first return "access is denied" , I sett view check out all roles, for authorized get correct buy in anonymous user return page not found !!!
    – Yuseferi
    Commented Mar 5, 2014 at 14:17
  • Have you confirmed that all of this, eg this module, even works with anonymous users? drupal.stackexchange.com/questions/89412/… seems to suggest that it doesn't?
    – Jimajamma
    Commented Mar 5, 2014 at 15:04
  • cart module and checking out works for anonymous user correctly, but when I create lineitem and order manually not work:(
    – Yuseferi
    Commented Mar 6, 2014 at 9:10

3 Answers 3


You don't need to change the order directly, commerce_cart_product_add do it for you.

Try this code:


 $product : commerce product
 $quantity: number of elements


 global $user;
 // Create new line item:
 $line_item = commerce_product_line_item_new($product, $quantity);

 // Add to current user's cart: if the user is not logged in ($user->uid: 0) Drupal Commerce manages the $_SESSION
 $line_item_added = commerce_cart_product_add($user->uid, $line_item);

 // If $line_item_added !== FALSE the line_item has been added OK!

You need to add commerce_cart_order_session_save() so anonymous user have their session to link the browser with cart content.


You should check Anonymous User permissions on the store components.

I was trying to do something similar for authenticated users, and got the same sort of problem. I then checked my permissions for the various components in the store, and when I made them available to authenticated users, my (mostly your) code started to work.

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