If you add 2 products to your cart, go to /cart and click both the remove buttons before the page reload(because you pressed the first remove). The cart session becomes corrupt and you cant access anypage with the cart on it without getting this fatal error:

EntityMetadataWrapperException: Unable to load the commerce_line_item with the id <commerce_line_item id>. in EntityDrupalWrapper->value() (line 696 of <drupal_root>/sites/all/modules/entity/includes/entity.wrapper.inc).

Comments

j0rd’s picture

Same problem, but for me, I'm pramatically clearing the items in my cart (commmerce_cart_order_product_line_item_delete) before adding a new line_item (commerce_product_line_item_new) in a custom add to cart page I've made.

j0rd’s picture

Here's how I have to clear from carts to get it to work.

  if(!empty($order->commerce_line_items['und'])) {                                                            
    $line_item_ids = array();                                                                                 
    foreach($order->commerce_line_items['und'] as $line_item) {                                               
      $line_item_ids[] = $line_item['line_item_id'];                                                          
      commerce_cart_order_product_line_item_delete($order, $line_item['line_item_id']);                       
    }                                                                                                         
    if(!empty($line_item_ids)) {                                                                              
      commerce_line_item_delete_multiple($line_item_ids);                                                     
    }                                                                                                         
  }

That's a little silly, as only doing one of them will completely break your store and leave it in an invalid state which needs to be fixed by manually deleting entries from the database then clearing cache.

Here are those entries

DELETE FROM field_data_commerce_line_items WHERE entity_id=$ORDER_ID;
DELETE FROM commerce_line_item WHERE order_id=$ORDER_ID;

I would like to know if this is the proper way to clear the cart, or if there are other functions I should be using. I tried these two as they have function names which related to what I'm trying to accomplish.

serialjaywalker’s picture

serialjaywalker’s picture

Status: Active » Needs review
StatusFileSize
new2.78 KB

As far as I can tell, this problem arises because commerce_entity_reference_delete() uses the Entity API classes which require us to load the (already deleted) line item in order to delete the reference to it.

The attached patch rewrites commerce_entity_reference_delete() to avoid using the Entity API classes. I'm not so experienced with Commerce that I'm sure that this is the best way to fix the problem, but this does seem to fix the problem (at least when deleting with the Line Item Manager widget), so I wanted to put this on the table.

serialjaywalker’s picture

Status: Needs review » Active

Ah, scratch that. I decided that this is actually a completely different issue. I'll put my patch where it belongs.

rszrama’s picture

Title: EntityMetadataWrapperException: Unable to load the commerce_line_item » Errors when multiple remove buttons in the Cart form are clicked at once

Yeah, the multiple remove button clicking error is one I've turned up in the past as well. I suppose if it comes down to it we can just lock all remove buttons on the View once one is clicked, but that feels a little brute force and would be JS dependent... I just fear that there may be something in our database query transactioning that causes the requests to collide and screw the order up. Of course, we could just try to get a fix in the Entity API to not choke when a wrapper finds a reference to a non-existent entity.

Edit: looks like I opened an issue for this a while ago. Just updated it: #1030140: Do not throw exceptions when wrapping an entity with a stale reference

rszrama’s picture

Status: Active » Needs review

Can I get some tests on the bug here with the latest dev version? In local testing, I cannot reproduce the bug any more... I know I was getting it before vacation, but one of the first commits I made when I got back addressed the function that may have been at the bottom of this: commerce_entity_reference_delete().

sirtet’s picture

Just downloaded kickstart through drush, web-installed, added all three sample-products to the cart, and clicked all "remove"-buttons before the first reload. this brought me to a blank page with only the following text:

Commerce Kickstart
Error

The website encountered an unexpected error. Please try again later.
Error messageEntityMetadataWrapperException: Unable to load the commerce_line_item with the id 5. in EntityDrupalWrapper->value() (line 696 of /usr/[path partly removed]/d7/com/kick/profiles/commerce_kickstart/modules/entity/includes/entity.wrapper.inc).

"Commerce Kickstart" is a Link to the Homepage, clicking it brought the next error and a success message:

Commerce Kickstart
Error

The website encountered an unexpected error. Please try again later.
Status messageProduct Three removed from your cart.
Error messageEntityMetadataWrapperException: Unable to load the commerce_line_item with the id 5. in EntityDrupalWrapper->value() (line 696 of /usr/[path partly removed]/d7/com/kick/profiles/commerce_kickstart/modules/entity/includes/entity.wrapper.inc).

every following click, and manually going to the Homepage reloaded the same error.
so i tried to go to /user. Same result.
So i went back in History to the Cart. Worked. Reloading the cart brought the error back.
going back in history worked.
then i logged out, and the error was gone.

i repeated the whole procedure, and this time, i couldn't get rid of the error, even after logging out.

[INTERMISSION]

Only now i realized: ??? WHEN DID I LOG IN? Never i think. Guess that's a different Bug, or maybe a normal behaviour, due to the Fact, that i have several other installations with the same superuser name/pw. And to some of them i may have been loged in.
But these reside in sub-directories of d7/ (see errors above).

IS this probably a bug i should see if it needs an issue?

[/INTERMISSION]

only clearing browser cache removed the error.
(this was on WIN7, Chrome13)
with FF5 and IE9 i never got an error, however fas t tried to click...

rszrama’s picture

Version: 7.x-1.0-rc1 » 7.x-1.x-dev
Status: Needs review » Needs work

You'll always be logged in after installation, so no mystery there. We're curious to know if the patch in #1243306: Implement pessimistic entity locking and enable it for orders resolves this issue. Can you first update to the latest -dev and try to duplicate the error again? Then can you install that patch and still duplicate the process that made it fail?

rszrama’s picture

Assigned: L-four » Unassigned
Priority: Major » Normal
Status: Needs work » Needs review

I should've left this in needs review. I can't reproduce this bug any more using -dev and that patch I linked. Just looking for validation.

sirtet’s picture

too newbie to be sure about needed steps, so i document:
drush dl commerce --select --> selected 7.x-1.x-dev - 2011-Aug-10 --> success
drush updatedb --> No database updates required
drush cc all --> 'all' cache was cleared

Now i tried Safari and FF5.01 on OS10.6, can't get the error, got confirmationpages with up to 3 simultaneously deleted items.

But on WIN7 CHROME13 it's still here.

Since i can get rid of the Error only by deleting cookies, would't it be a good idea to have a mechanism in this type of Error-Message Routine, to delete cookies if a user saw n Errors in a Row or so? Or at least a text message to clear cookies (which at best will let many ppl run into the kitchen).

next:
wget http://drupal.org/files/issues/1243306-commerce-locking.patch
patch < 1243306-commerce-locking.patch
result:

patching file commerce.controller.inc
patching file commerce_cart.test
Hunk #1 FAILED at 510.
Hunk #2 FAILED at 528.
2 out of 2 hunks FAILED -- saving rejects to file commerce_cart.test.rej
patching file commerce_checkout.test
Hunk #1 FAILED at 77.
Hunk #2 FAILED at 160.
Hunk #3 FAILED at 320.
3 out of 3 hunks FAILED -- saving rejects to file commerce_checkout.test.rej
patching file commerce_order.module
Hunk #1 FAILED at 14.
1 out of 1 hunk FAILED -- saving rejects to file commerce_order.module.rej
patching file commerce_order_ui.test
Hunk #1 FAILED at 61.
Hunk #2 FAILED at 146.
Hunk #3 FAILED at 180.
Hunk #4 FAILED at 252.
4 out of 4 hunks FAILED -- saving rejects to file commerce_order_ui.test.rej
patching file commerce_payment_ui.test
Hunk #1 FAILED at 166.
1 out of 1 hunk FAILED -- saving rejects to file commerce_payment_ui.test.rej

ok, maybe patch applies to rc1?

drush dl commerce --select --> 7.x-1.0-rc1 - 2011-Jul-14
--> Install location... ..../sites/all/modules/commerce already exists. Do you want to overwrite it? (y/n): y
hmm, i did not get asked installing the dev-version, why that?...
however, i continue:

wget again,
patch again,
--> patching fails same way.

what next?
Thomas

rszrama’s picture

You might try patch -p0 < 1243306-commerce-locking.patch.

sirtet’s picture

never got the patch to work.
But i did read in the issue with the patch (http://drupal.org/node/1243306), that an update to Drupal Core 7.x dev would now solve the "multiple remove error".
Trying to update the core, i destroyed my installation. So i did a fresh install with kickstart, which installed DC 7.x-1.0-rc3.
Now i can't reproduce the error anymore.

rszrama’s picture

Status: Needs review » Fixed

Well, if no one else has any objections, I'm going to mark this one fixed thanks to the issue I linked to in comment #9 above. I still can't reproduce any problems here, and it seems to have been solved by the update to 7.x for the only person to get the error (probably do to unrelated db_transaction() problems). We can reopen this if necessary.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

sirtet’s picture

Status: Closed (fixed) » Active

Not sure it that belongs in this issue, the error is different:

    Notice: Undefined offset: 4 in commerce_stock_form_commerce_cart_validate() (line 158 of /home/quickstart/websites/example.dev/sites/all/modules/commerce_stock/commerce_stock.module).
    Notice: Undefined index: in commerce_stock_form_commerce_cart_validate() (line 158 of /home/quickstart/websites/example.dev/sites/all/modules/commerce_stock/commerce_stock.module).
    EntityMetadataWrapperException: Unknown data property commerce_product. in EntityStructureWrapper->getPropertyInfo() (line 328 of /home/quickstart/websites/example.dev/profiles/commerce_kickstart/modules/entity/includes/entity.wrapper.inc).

I used the quickstart ubuntu-VM (http://drupal.org/project/quickstart)
and installed D7.8 with profile from commerce_kickstart 7.x-1.0-rc1

add different items to cart, and then click several remove's before page reloads...

rszrama’s picture

Status: Active » Closed (fixed)

Yeah, that should go in a separate issue reported on the Commerce Stock project.