Can someone show me how to add an item to the cart by product ID programmatically?
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | commerce.add_to_cart_programmatically_1288414_11.patch | 1.05 KB | rfay |
Can someone show me how to add an item to the cart by product ID programmatically?
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | commerce.add_to_cart_programmatically_1288414_11.patch | 1.05 KB | rfay |
Comments
Comment #1
rszrama commentedcommerce_cart_product_add() is the function you want. Pass it a uid and a product line item to add to the cart. To make your line item, just use commerce_product_line_item_new(). You don't technically need to run it through Rules to calculate the price before adding to the cart, as the next time the cart is loaded this will happen.
Comment #2
mmilano commentedThanks for the response. I've tried to do my due diligence, but I can't seem to get it to work. Also, I apologize in advance if setting to active is not proper protocol, I thought it was best though since I don't think notifications are sent when they are closed. (I didn't get one)
My code:
Result: Nothing in my cart
Expected result: 1 if these items in my cart.
I traced it into function commerce_cart_product_add() and it shorts out here. There's no $line_item_wrapper->commerce_unit_price->value().
The product object itself does have a price. Here's a dump of some of the object:
Any insight would be greatly appreciated. Thank you.
Comment #3
bojanz commentedI think we should have an APi function that just takes a product, instead of requiring the user to construct a line item.
@mmilano
You've seen what your problem is. The line item needs to have a price as well (just take the one from the product).
Comment #4
mmilano commentedThanks bojanz. An API function would be nice.
I'm trying not to bother you guys too much, but it's not that intuitive to do even with drupal experience.
The line item object seems to use the same format for price as the product. So, I thought this should work.
$line_item->commerce_unit_price = $p->commerce_price;
But it does not. I confirmed there are no items in cart after I call commerce_cart_product_add().
Can you show me the proper way for me to assign the price to the line item?
Comment #5
rszrama commentedYou shouldn't have to assign a price to the item. That happens when it gets created as a product line item. Manually updating it before adding it won't affect anything, anyways, because the price of a cart item is recalculated every time the shopping cart is loaded. A simple API function shouldn't be that hard to cook up.
Comment #6
mmilano commentedI found a working example in commerce_base.test and am able to add to cart by product ID now. Looks like the main thing is how I was setting quantity.
I wrapped it in a function here:
Comment #7
rszrama commentedThis could be a simple DX improvement for 1.1.
Comment #8
davidwhthomas commentedJust doing the same thing, to allow reordering a previous order..
I agree an API function to add a product to cart by product id would be v.useful.
I notice commerce_cart_add_to_cart_form_submit does this:
field_attach_submit('commerce_line_item', $line_item, $form['line_item_fields'], $form_state);Is that important to replicate when adding a product to the cart?
thanks!
DT
Comment #9
rszrama commentedThat's because it collects additional field data for the line item in the form. A simplified add to cart function would probably just not be able to accommodate line item types with additional fields. That may in fact be a good argument against providing such an API function to begin with... creating the line item and passing that to commerce_cart_product_add() isn't so much of a hassle that we necessarily must have a crippled add to cart API function.
Comment #10
davidwhthomas commented@rszrama Thanks for the reply!
So this means it's ok to ignore those 'line_item_fields' for now when adding a product to the cart?
If not, is there an easy way to generate them for a particular line item?
I had a look, but the data structure looks complex, am wondering if there's an api call as it doesn't appear commerce_product_line_item_new adds those fields.
Suggestions much appreciated,
thanks,
David
Comment #11
rfayHere's a patch that adds the function. Thanks to all for the hints on how to do it.
Comment #13
rfayNeeds review, of course. Not sure why this one was allowed to test with tests on the branch broken.
Comment #16
rszrama commented#11: commerce.add_to_cart_programmatically_1288414_11.patch queued for re-testing.
Comment #17
rszrama commentedSilly me, we don't need to wait for a test request. This code would only cause failures if it had fatal errors in itself, since it's not called from anywhere.
I've updated the comments, added the $combine parameter, and changed the name to commerce_cart_product_add_by_id(). I'm still not a huge fan of this function, because it doesn't interact with additional product line item fields like the display_path. I had a mind to add support for that, but then I realized I'd basically be recreating commerce_cart_product_add(). : P
So I've added comments about it. The function can be used as is, and if you want to use this function and set a display_path or other line item fields, you can just edit and save the $line_item that gets returned by the function.
Commit: http://drupalcode.org/project/commerce.git/commitdiff/41cb23e
Comment #18
rfayI think this may need more work.
Shouldn't be be able to specify the line item type, and have some way to populate fields in the line item type?
Comment #19
rszrama commentedAs I mentioned in #17, we could continue to improve this function, but at the end of the day we'll just end up recreating commerce_cart_product_add(). For anything more specific than a simple cart add, I think it best to recommend developers use the full function where it's all based around passing in a line item anyways. It may be confusing to have to work with a line item up front, but the alternative is to introduce some big variables array with custom notation for populating its fields specific to this one function. I think that'd be worse than people just creating the line item themselves and passing it to commerce_cart_product_add().
Comment #20
rfayWorks for me. #1 has the complete formula also.
Comment #22
nathan.bolin commented#1 works perfect. Here is an implementation of simply adding one product to the cart :
commerce_product_line_item_new()
commerce_cart_product_add()
Comment #23
meaton commentedThanks @nathan.bolin your solution worked great!
Comment #24
johnpitcairn commentedIf you have pricing or shipping rules that affect the line item price or cart totals, that won't happen correctly for the first item in the cart using the code above. You'll need: