I removed a product from the shopping cart, then deleted the product, then removed the product type. And am now getting this error which makes the site unusable.

EntityMetadataWrapperException: Unable to load the commerce_product with the id 3. in EntityDrupalWrapper->value() (line 623 of /Users/henrique/Webroot/commerce/sites/all/modules/entity/includes/entity.wrapper.inc).

I think this problem is that the product display node is already there. So we should either remove the reference from the node display node. Or prevent from deleting products which were referenced by a product display node, just like we do with products that were already been ordered.

Comments

recidive’s picture

Title: Error when deleting a product » Error when deleting a product that's referenced by a node
rszrama’s picture

Component: Line item » Product reference
Issue tags: +beta blocker

Use the strategy of the line item module to delete product IDs from product reference fields when products are deleted here.

recidive’s picture

Assigned: Unassigned » recidive

I talked with Ryan on IRC and we agreed in doing two things:

  1. Add message to the confirmation form telling "This product is referenced by X entities."
  2. Delete the product from product reference field.

If user go ahead and delete the product anyway, and a) if the product is the only one referenced by the node, and b) the product reference field is set to "Required". The user will end up with a broken/inconsistent node. There's a issue for dealing with that already: #1029950: Deleting a Product doesn't delete associated Product Display.

recidive’s picture

Status: Active » Needs review

I committed the changes to my branch here:

https://github.com/recidive/drupalcommerce/tree/1035050

I also replaced the commerce_line_item_reference_delete() function with commerce_entity_reference_delete(), since I needed that function to work for product reference fields as well. That genericized functions was put into commerce.module as Ryan advised.

rszrama’s picture

Status: Needs review » Fixed

Committed this with the following fixes:

https://github.com/rszrama/drupalcommerce/commit/11e4c49fcaca80055cb169f...

I'm actually not sure to what extent you tested this patch before sending the pull request. At least w/ the Commerce Dev profile, delete was failing outright because commerce_entity_reference_delete() wouldn't work for single value references. I grant you may have had a product display node type with a multi-value reference... but even so the function commerce_product_reference_commerce_product_delete() was trying to pass $line_item->line_item_id to the function instead of $product->product_id.

I also changed my mind and went ahead and made the delete confirmation form show the different types of entities referencing the product... not a big deal, and I also went ahead and prevented submission for products referenced by line items since commerce_product_reference_commerce_product_can_delete() will prevent it anyways.

Status: Fixed » Closed (fixed)
Issue tags: -beta blocker

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