Closed (fixed)
Project:
Commerce Core
Version:
7.x-1.x-dev
Component:
Cart
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
22 Apr 2011 at 03:08 UTC
Updated:
20 Mar 2013 at 13:34 UTC
We'll probably see a few of these as support situations, so I thought I'd put this out there for google to find. The answer is pretty easy.
I created a bunch of products using Commerce Feeds and a bunch of product reference nodes to go with them. I absent-mindedly added one to my cart. Then I deleted the nodes and the products using the Feeds never-mind-delete-them-all interface.
But my cart still referred to them. So now on every page with the cart block I get this fatal:
Error message
EntityMetadataWrapperException: Unable to load the commerce_product with the id 5053. in EntityDrupalWrapper->value() (line 682 of /home/rfay/workspace/commerce/sites/all/modules/entity/includes/entity.wrapper.inc).
The solution (assuming you have no orders or line items of value):
truncate commerce_order;
truncate commerce_line_item;
Comments
Comment #1
rszrama commentedHmm, so the feeds deletion process somehow bypasses hook_commerce_product_delete()? I would've expected commerce_product_reference_commerce_product_delete() to deal with this.
Comment #2
pcambraThe problem is that if you have a line item referencing a product that doesn't exists anymore, you get this error, actually you're screwed because you can't access the order UI view to delete the order b/c you get the same error there.
Steps to reproduce this:
1- Import products using commerce feeds module.
2- Create a product display referencing one of the products imported.
3- Add one product to the cart. (As an alternative you could directly create an order in cart status from order UI)
4- Delete the products via Feeds interface.
5- Visit the cart.
hook_commerce_product_delete() is called and the products are correctly removed from its displays, also commerce feeds implements this same hook to handle feeds_items table that controls the records imported.
This is something that doesn't happen in commerce by itself because you can't delete a product that is referenced by a line item. (Maybe in VBO integration?)
I think that the delete items issued from feeds normally is used for wipe out a migration and start over. I'd say that we could implement in commerce feeds, in hook_commerce_product_delete() a call to delete all line items referenced by this products. Other option would be to do something similar as the hook_commerce_product_can_delete and actually don't delete feeds items referenced in line items (not sure if feeds allows to do this check though)
Comment #3
rszrama commentedWell, the thing is, the function I linked above appears to be able to unset a product reference on any entity when a product is deleted (including line items). That's why I thought it should work fine. Perhaps the $product variable Feeds is sending through the hook isn't what we expected or something?
Comment #4
pcambraOk, I got more information on this, probably this issue is affecting VBO integration as well...
It seems that when Feeds delete one single item, "everything works fine", meaning that if the product is in the cart or part of a line item, commerce_product_reference_commerce_product_can_delete fires and stops the deleting process, but when it has to deal with several entities to delete, those hooks hook_commerce_product_delete and hook_commerce_product_can_delete are not actually being called and this drives into the ugly error that rfay reports.
I'll look further into this in the following days
Comment #5
PI_Ron commentedWas there any progress made on this?
I was doing some testing and created a test product type, 2 test products & test product display referencing them, then went through the order process for this product.
Now went to delete the type, got the 'exists in order X' message, so went to delete the order, but got the above error.
Can I delete the order, type & display somehow, or am I as pcambra put it, actually screwed?
Comment #6
rfayI wasn't aware this could happen unless you used feeds to import, put something in the cart, and then tried to delete.
I *believe* that if you delete the line items and the order, you'll get out of this.
If you get me your database (attach it to this issue, or if it's sensitive send it to me at randy at randyfay.com) I'll take a look at see if I can figure out the way out.
Comment #7
PI_Ron commentedI tried removing the line item through order screen but got similar:
Comment #8
rfayYeah, you won't be able to do it except via the database. You'll have to manually delete in the database.
If you want to upload your database to this issue or email it to me at randy at randyfay.com I'll figure out what it takes to recover. Probably needs a little sql script.
Or figure it out yourself, but please post the way you solve it.
Comment #9
serialjaywalker commentedI think the error in #7 sounds like #1186130: Line item deletion causes EntityMetadataWrapperException. I haven't tried this, but I've been through the code fairly carefully, and I think you could successfully delete a line item (for the purpose of restoring, not as a fix) by calling commerce_line_item_commerce_line_item_delete() before commerce_line_item_delete(). (The former will end up getting called twice, but there shouldn't be anything for it to do the second time).
Comment #10
rfayMarked #1186130: Line item deletion causes EntityMetadataWrapperException as a duplicate. Thanks for pointing that out.
Comment #11
serialjaywalker commentedHmm, I didn't mean to suggest it was a duplicate. I think the bug described in #7 is the same as #1186130: Line item deletion causes EntityMetadataWrapperException, but I'm less convinced that it's the same as this issue.
#1186130: Line item deletion causes EntityMetadataWrapperException will give an exception any time a line item is deleted, simply because we attempt to load the already-deleted line item in trying to delete a reference to it.
The issue here (i.e. in #4) seems to be that the function that deletes the references is not even being called, which strikes me as something a bit different.
Comment #12
serialjaywalker commentedOn further thought, I have no idea what's going on in Tigeda's #5 and #7. If products referenced by line items have already been deleted (as indicated in #5), I would expect that one would not be able to get to the order edit form to remove the line items and get the error in #7.
Comment #13
PI_Ron commented@rfay What tables / fields etc. would I have to look for and delete?
@#12 Sorry I didn't mention the products had been deleted, unable to delete because they exist in the order, thats what I tried first, then wen't to delete the order and got the error.
Comment #14
rfayI'm working on a script to demonstrate how to get out of product-deletion and line item-deletion hell.
Comment #15
rfayThese are at your own risk, and are only to be executed if you know and understand what you're doing. #1 is suitable if you have no live order data and don't care at all about your orders. #2 is suitable if you only want to delete orders that are in the "cart" state.
1. If you don't have any orders or line items that matter at all, then use this query to delete all orders and their line items and related fields.
2. On the other hand, if you only need to delete cart-orders (user carts are actually orders in the 'cart' state) then execute this command, which will delete all orders, line items, and related fields but only for orders that are in the 'cart' state.
Comment #16
rfayI made a mistake on the second query in #15. It needs to delete where status = 'order' OR status = 'checkout_checkout', for when the checkout page has already been visited.
If you want to delete all orders that are in 'cart' or 'checkout_checkout' status, which should be safe in most cases (but will delete any customer carts), try this:
Comment #17
rszrama commentedMy follow-up question to this issue is do we really just need to file bug reports with modules that aren't properly invoking deletion hooks when deleting entities? Perhaps we can get bojanz to confirm whether or not VBO works the same way now that he's maintaining it.
Comment #18
rfayVBO won't delete products that are in a cart. In fact, it thinks it does succeed, but fails. See #1199130: Check if entity_delete() successfully did the job (after Entity API starts returning the status); in #4 there bojanz tells us how to fix up Commerce to give this feedback.
VBO *is* willing to delete line items that belong to an order. The script in this issue is able to clean up after that event.
VBO is also willing to delete orders (cart orders). It actually seems that this one does good cleanup (does the line items in the process).
Feeds is very willing to delete products that it imported that are currently in a cart. The query provided in this issue will clean that up.
Views, Feeds and VBO (really, commerce itself, using the delete link) are very willing to delete a product which still has a product reference node to it. That also causes the same type of disaster until cleanup. I'll try to post a cleanup for that situation as well. The fix is just to delete the referencing node.
Overall, we can make quite a bit of progress by going forward with #1184138: Catch exceptions for render/entity failures, which makes one of these not a complete catastrophe as it currently is.
Things we need to do:
1. Commerce might consider disallow deleting referenced products. I think if commerce prevents this, all will work out for views and vbo as well.
2. Feeds/Commerce Feeds should probably not be able to delete products which are either in a cart or referenced.
3. Commerce should give feedback to entity when failing to delete, per #1199130: Check if entity_delete() successfully did the job (after Entity API starts returning the status)
Comment #19
PI_Ron commentedThanks rfay, that did the trick.
For anyone else with the same situation, just add your table prefix to each line aswell, if you have one.
Comment #20
dbkern commentedRay,
Thank you! I was just about to surrender. First I had this error:
EntityMetadataWrapperException: Unable to get the data property product_id as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue() (line 421 of .../sites/all/modules/entity/includes/entity.wrapper.inc).I managed to get rid of that error only to end up by following advice here: http://drupal.org/node/1185988.
Then I was left with the following error:
EntityMetadataWrapperException: Unable to load the commerce_line_item with the id 2. in EntityDrupalWrapper->value() (line 683 of...sites/all/modules/entity/includes/entity.wrapper.inc).#15 did the trick for me. Thanks so much!
Comment #21
imiksuSubscribe.
Comment #22
webengr commentedsubscribe
Comment #23
rfayI have this problem right now.
I have 3 existing orders. Each can be viewed. The line items in each refer to products that still exist.
Debugging...
Comment #24
rfayMine seems to be an issue with commerce_file: #1237048: Can't delete order: EntityMetadataWrapperException: Unable to load the commerce_order with the id...
Comment #25
rfayI seem to have used #16 to recover again.
Comment #26
ultimikeRandy,
The "truncate" query you have listed above just saved me some time. I'm using Commerce Feeds and kept getting the "EntityMetadataWrapperException" error when I tried to create a new content type (and not even a commerce-related one!). I was also getting the same error when I tried to edit my existing product display type.
Once I ran the truncate queries, all was fine.
I think there is something else going on here, but thanks for the quick fix regardless.
Thanks,
-mike
Comment #27
rfayThanks for the feedback, @ultimike
Comment #28
sleepingmonkLike dbkern I got the error:
EntityMetadataWrapperException: Unable to get the data property product_id as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue() (line 421 of .../sites/all/modules/entity/includes/entity.wrapper.inc).This appeared when I tried to edit a Vocabulary, or create a content type.
Truncating the tables in #15 cleared the error on a development site.
Comment #29
manischewitzbacon commentedI just wanted to note that you'll have to truncate the tables for any fields you've added to orders:
truncate field_data_custom_order_field1
truncate field_data_custom_order_field2
truncate field_revision_custom_order_field1
truncate field_revision_custom_order_field2
Comment #30
manischewitzbacon commenteddangit, didn't mean to change the bug title. just the comment subject...
Comment #31
manischewitzbacon commentedwell, I thought I was out of the water, but after 5 transactions i started having issues again with negative payment amounts being transfered. I fixed this by truncating my payment tables:
truncate commerce_payment_transaction;
truncate commerce_payment_transaction_revision;
Comment #32
rszrama commentedThis issue seems to have petered out, likely thanks to the Entity API being updated months ago to no longer throw exceptions when an out of date reference was found. We can keep an eye on this, but it seems fago fixed this upstream.
Two related issues that remain open will in fact deal with improved API access control over delete functions:
Comment #33
alinouman commentedThanks rfay for it #15 . It solved my problem.