6

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.
    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);
    //commerce_cart_order_refresh($order);
//    ym(commerce_cart_order_id($uid));
    //commerce_cart_commerce_order_load($order);
    //ym(commerce_cart_order_is_cart($order));
    //commerce_cart_product_add($uid, $line_item);
    //commerce_cart_product_add_by_id($product_id);
    //$form_state['redirect'] = 'cart';
    drupal_goto('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 Mar 5 '14 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 !!! – Yusef Mohamadi Mar 5 '14 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 Mar 5 '14 at 15:04
  • cart module and checking out works for anonymous user correctly, but when I create lineitem and order manually not work:( – Yusef Mohamadi Mar 6 '14 at 9:10
15

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

Try this code:

Variables:

 $product : commerce product
 $quantity: number of elements

Code

 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!
2

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

0

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.

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.