When using the cart and updating multiple products at a time using 'update cart' the cart breaks.
The error it gives is the following
Recoverable fatal error: Argument 1 passed to Drupal\commerce_cart\CartManager::updateOrderItem() must implement interface Drupal\commerce_order\Entity\OrderInterface null given, called in modules/contrib/commerce/modules/cart/src/Plugin/views/field/EditQuantity.php on line 134 and defined in Drupal\commerce_cart\CartManager->updateOrderItem() (line 137 of modules/contrib/commerce/modules/cart/src/CartManager.php)
Just to be as thorough as possible;
- Updating one product at a time with different quantities works just fine.
- But updating 2 products at once (even when increasing or decreasing both with just 1) breaks the cart.
Comment | File | Size | Author |
---|---|---|---|
#14 | 2891190-14.patch | 1.16 KB | drugan |
#12 | commerce_cart-fix-updating-multiple-items-in-cart-2891190-12.patch | 6.4 KB | jsacksick |
#10 | commerce_cart-fix-updating-multiple-items-in-cart-2891190-10.patch | 2.7 KB | jsacksick |
#8 | 2891190-8.patch | 829 bytes | drugan |
#6 | 2891190-6.patch | 968 bytes | drugan |
Comments
Comment #2
vasike@Maikel: what version of the module do you use.
The lines indicated by the error message do not seem to be accurate for the latest version (dev).
could you, please, update your commerce package and try again.
Thanks
Comment #3
jsacksick CreditAttribution: jsacksick commentedI reproduce that issue all the time, all I have to do is update several quantities and click on "Update cart".
For some reasons, from the second item order item in the loop, the order_id field is empty.
So $order_item->getOrder() will return NULL.
Right now, the order is saved by the cart manager for each order item, we call:
We should fix this and save the order only once, but we need to figure out what's causing the order reference from the order item to be lost.
When passing FALSE as the third parameter, the order won't be saved by the cart manager, then we'll have to save it ourselves.
Comment #4
SchwebDesign CreditAttribution: SchwebDesign commentedWe are seeing this issue as well.
Steps to recreate:
- add 2 different products to the cart
- in cart, change quantity of each and click 'update cart'
The error we get is:
If i can provide any information to help find a fix let me know.
Comment #5
kala4ekSubscribe, the same problem.
Comment #6
drugan CreditAttribution: drugan as a volunteer commentedPlease, check if this patch helps you.
Comment #7
jsacksick CreditAttribution: jsacksick commentedThe bug is still occurring with the patch from #6.
$row_index was already correct, so no need to increment it in the loop, I just think we should save the order after the loop, that seems to fix the issue.
I did some tests locally and it seems to fix the issue.
Comment #8
drugan CreditAttribution: drugan as a volunteer commented@jsacksick
I can't reproduce your issue. So, there is a patch which prevents error but not actually fixes it. I think that the reason is related with cache. Have you some weird cache settings?
It would be helpful if you fresh install drupalcommerce site, create products and add them to cart to reproduce the error. Then just dump the site's database to dump.sql.gz file and upload it on this page.
Comment #9
drugan CreditAttribution: drugan as a volunteer commentedComment #10
jsacksick CreditAttribution: jsacksick commentedWhy do you set the status to "postponed"? Several people reported the issue, it's reproducible all the time on a fresh install, all you have to do is add more than one item to the cart, go to the cart page, update several quantities then click on "Update cart".
The attached patches fixes it (we save the order only once on submit, and also output the same message that was displayed in 1.x.
I tried to understand why after the first order item was saved (and the order), the order_id was nullified for other order items which was causing the issue described here.
Comment #11
mglamanThis needs a test. For all I know we do not have any test coverage of the quantity edit field and saving the cart form. We need a Functional test to cover this and also to try and reproduce the reported bug.
Comment #12
jsacksick CreditAttribution: jsacksick commentedThere's already a test (CartTest), but it isn't testing updating multiple quantities in cart, I updated the test and also made sure the order was saved if quantities were updated (See attached patch).
I'm going to run the test without my patch to see if it's failing.
Comment #13
jsacksick CreditAttribution: jsacksick commentedI just ran the updated CartTest with the current codebase (after removing asserting that the shopping cart update message is present), and surprisingly it's not failing... :p
Comment #14
drugan CreditAttribution: drugan as a volunteer commentedI applied modules/cart/tests/src/Functional/CartTest.php (this file only) changes suggested on the #12 and the test is green for me.
Obviously that the reason is in different indices on
$quantities
and$this->view->result
arrays. This happens because when the form is constructed in theEditQuantity->viewsForm()
then we loop through$this->view->result
array but when the form is submitted we loop through the$quantities
array in theEditQuantity->viewsFormSubmit()
.For the first look it seems the same but the current issue proves that it's not. A possible reason might be here:
http://cgit.drupalcode.org/drupal/tree/core/modules/views/src/Plugin/vie...
When the
$this->view->result
is populated it looks like this (on my installation):$this->view->result = [
/** @var \Drupal\views\ResultRow */,
/** @var \Drupal\views\ResultRow */,
/** @var \Drupal\views\ResultRow */,
// ...
];
So. it is a non-associative array with auto indices (0, 1, 2, ...). But, let's imply that for some unknown reasons (like in @jsacksick case) the
$result
passed toiterator_to_array()
has at least one associated key assigned before:As the iterator_to_array() is called with default
$use_keys = true
argument so the'999'
will be assigned as the key for a Quantity field in theEditQuantity->viewsForm()
.So, my suggestion is to loop through
$this->view->result
in theEditQuantity->viewsFormSubmit()
too.Comment #17
bojanz CreditAttribution: bojanz at Centarro commentedLet's go with this for now. Thank you, Jonathan.
Comment #19
hisik CreditAttribution: hisik commentedI am also seeing this issue when multiple products in the Drupal 7 Commerce cart, it is crashing during check out. Has anyone found a solution to this?