3

I want to delete an order,but when I try to delete it notify me "This product is referenced by a line item on Order 40 and therefore cannot be deleted. Disable it instead" but I want delete it completely in my product list, It is not important delete an order item that is used in.

  • If I was your client, and I would order X, Y and Z, and some time in the future I would see only X and Z in order history, I would start to think you are cheating somehow. Seeing Y as something I can no longer buy would look normal. So the way it is seems safe, and think twice if you will start to change it. At least add some comments or other annotations to people's orders. Unless you are only talking about test orders made by your own team, if so it is safe. – Mołot Sep 21 '14 at 20:39
6

Commerce is protecting its own data integrity - if you were able to delete a product that's referenced by an order line, the next time you view the associated order something would break.

You'll need to remove the product from all associated orders before deleting it; I think the easiest way to do so would be to construct a View using Views Bulk Operations, select Line Items filtered by the relevant product ID, and bulk delete them. Obviously this will have an effect on existing orders, but since the product is attached to the line item by its ID, there isn't really an alternative (short of rewriting a bunch of stuff in Commerce).

3

I have also run into this problem with test products during a site build, and/or products in a large dataset that needed to be deleted post-import. Once the product is associated with orders or nodes, it is painful to get rid of them. Even after orders or shopping carts are deleted, the database records remain. Even Views Bulk Operations will not get around those.

So, here is my Product Removal Toolkit:

Install the fantastic module: https://www.drupal.org/project/commerce_prodnodelink

This will put a very handy box on your product pages that will tell you what node the product is referenced on so you don't have to go searching for it. Use the info there to delete the product references.

Now you have to go into the DB and do some SQL queries:

  1. commerce_line_item table:

delete from commerce_line_item where line_item_label = 'the-sku-of-the-product-here';

  1. field_data_commerce_product table:

delete from field_data_commerce_product where commerce_product_product_id = 'product-id-of-the-product-entity';

  1. field_revision_commerce_product table:

delete from field_revision_commerce_product where commerce_product_product_id = 'product-id-of-the-product-entity';

Once you do these things, you should be able to delete that product!

Now, if you really have a lot of items to get rid of, you could make arrays with your errant line item data and funnel it all through a PHP function. :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.