TransWikia.com

How to automatically delete all product displays when a product is deleted?

Drupal Answers Asked on October 26, 2021

To automatically delete the product displays when a product is deleted, is a feature of the module Auto Product Display, but I don’t need that module.

Preferably using the Rules module, I would like to accomplish the same.
An issue in drupal.org is present, but currently open.

I’m trying to fetch the referencing product displays:

Trigger: After deleting a Commerce Product

  • Fetch entity by property
  • Entity is of type: node
  • The property by which the entity is to be selected: product (the product reference field)
  • The property value of the entity to be fetched: ?? (The node
    ID of the product is not available to select)

How to get the node ID’s of all product displays referencing the product?

One Answer

1) You need the commerce product id (which is the one of the commerce product that has been deleted) and not the nid because of the reference you are using. If this parameter is not available you must bring it into scope using a proper validation.

Notice that after deleting the commerce product there is no product id so no products are referencing it. The correct trigger should be "before deleting commerce product" but does not exist by default.

Also, how do you handle a Product display (node) with multiple product variations? Should this node be deleted? There should be an additional condition to check if there are other variations for the same product display. Except if you have a 1 to 1 reference.

2) Another option should be to do the opposite which is to fetch all the nodes (Product displays) that have an empty commerce product reference. This is easier to create.

Answered by TheodorosPloumis on October 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP