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 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!

Related Questions

Social Media Embed Problems With the URL Embed module

0  Asked on November 30, 2021 by mckinzie25


Contextual links for node blocks

3  Asked on November 28, 2021


Set node to flagged by default

1  Asked on November 28, 2021 by andrew-morris


How do I replace drush_set_error()?

1  Asked on November 28, 2021


Get file ID and path to use in View template

1  Asked on November 25, 2021 by srodrig


How to use drupal_add_library with a module?

2  Asked on November 25, 2021


Ajax Form Not Working in some Some Regions

2  Asked on November 23, 2021 by sandesh-yadav


Facebook Album fetcher

1  Asked on November 23, 2021 by rajat-goyal


Condition on current moderation state

2  Asked on November 23, 2021 by paul-cappucci


how to apply a css code for a specific screen size

1  Asked on November 23, 2021 by coding-mv


hook_entity_predelete never called

0  Asked on November 23, 2021


User reference and Views 3 and Panels 3

1  Asked on November 21, 2021


MetaTag with JSON API won’t serialize

2  Asked on November 21, 2021 by johnventions


How to disable Rich Text Editor in image field caption ?

1  Asked on November 19, 2021 by lee-woodman


search block form not accepting special characters in drupal 7

1  Asked on November 19, 2021 by blueztechz


Unknown engine type outputformat

1  Asked on November 19, 2021 by gilles9999


Ask a Question

Get help from others!

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