Discussions

Rules to create/delete a corresponding Product Display upon create/delete Product

Trying to figure out a way with rules to automatically create a new display node when a product is created. The display node title should be the sku of the product. Also trying to figure out a way to do this in reverse, ie when the product is deleted so is the display. I'm already using Product Display Manager but I'm setting up a "marketplace" and don't want to confuse users with having products AND displays to manage.

Posted: Oct 17, 2011

Comments

dasmoermel on October 17, 2011

Hello!

For adding a DisplayNode:

- use rules -> Add a new rule
- Fetch the Event "After a new Product is saved"
- Create a new entity Typ = yourDisplayNode
- set up all values in your new DisplayNode
- set Node:Title = Product:SKU
- maybe you must add your Product to a list of Products then set this list as your new
DisplayNode's ProductReferecne
- save the new entity

The way back might be easier by deleting the DisplayNode.
- use rules
- fetch event "After deleting a node"
- set the condition: If nodeTyp = DisplayNode
- get all refereced Products of your DisplayNode
- Loop this list of Products
- for each list-item: get entity by ID -> delete entity

Hope it helps.

regards, Marc

phillbd on October 17, 2011

Ok I got it to create a new Product Display using the product's SKU as the title (doing this because i figure I can make the Product Display use the referenced product's Title in it's display. Also it would be easier to fetch the Product Display to be deleted by it's title if it's the same as the product's sku? I will only have 1 product per product display so no need to loop through the referenced products to delete...)

I'm having trouble figuring out how to add the product reference to the newly created Product Display as an action in this rule though..

Thanks for the help so far!

dasmoermel on October 17, 2011

If it works better for you to delete the Product Display with the Product instead deleting the Product with the product display, it might work in that way:

rules: "After a Product is deleted"
action: Write own PHP - code

$sql = "SELECT t.`entity_id` AS NodeID FROM {YOUR PRODUCT REFERNCE TABLE} t WHERE `YOUR PRODUCT REFERENCE COLUMN` = :deletedPrductID";
$res = db_query($sql, array(':deletedPrductID' => $deletedProductID));
foreach ($res as $row) {
$nid = $row->NodeID;
}
return $nid;

Now you got the NodeID from your Display Node which referenced to your deleted Product.

rule action:
"get Entity by ID"
-> to get the Node with $nid as NodeID

rule action:
"delete Entity"
-> delete the fetched node

I will only have 1 product per product display so no need to loop through the referenced products to delete...

I'm not sure, but I think the ProductReferenceField is a List of Products, but it might also work without the loop.

phillbd on October 20, 2011

Thanks so much.. though I have to say this is a bit over my head :)
Where would I go to add this PHP code? Also, I'm guessing "YOUR PRODUCT REFERENCE TABLE" and "YOUR PRODUCT REFERENCE COLUMN" should each be replaced with some machine name but i'm not sure how to find that. Thanks again!

dasmoermel on October 20, 2011

For adding PHP Code in Rules:
Enable Core Module "PHP Filter" that provides you an Option in Rule Action "Write own PHP Code" or something like that.

Got to your Displaynodes Fieldadministration and take the machinereadable name of your Productreferencefield.
e.g.: "field_commerce_ref_tour" in my case
So the Datatablename is "field_data_field_commerce_ref_tour"

And the Datacolumn like that: "field_commerce_ref_tour_product_id"

emjayess on December 20, 2012

Oddly, none of my product display types are appearing in the select dropdown, thus cannot be selected as the entity to create > <

no idea why this is, but it would seem that I cannot create corresponding displays.
EDIT: got it sorted, it was sort of a bug in the theme and some missing settings. (Thanks @Josh Miller on IRC)

ndavidg on December 12, 2012

I remember testing a module that creates both the product and the product display from within the product display edit page. It was pretty easy/straightforward. Might want do do a search for it.

emjayess on December 20, 2012

@ndavidg may be alluding to the commerce_product_display_manager module (PDM, for short), which actually embeds a form within the *add/edit product* page to establish the requisite reference to either a new (created on the fly) or existing *product display*....

So, PDM's functional enhancements are the inverse of ndavidg's recollection. If there's *also* something similar that enhances the add/edit product display form with nested functionality for adding/editing products, I haven't seen it yet.

emjayess on December 20, 2012

See also: the power user product management capabilities that Commerce Guys bolted together in the latest version of Commerce Kickstart, using views and views_megarow to join product(s) with linked product displays and inline "Quick Edit" functionality is also pretty powerful.

emjayess on December 20, 2012

I haven't implemented a corresponding delete rule, but I just recently got a create rule working successfully - a rule that auto-creates a product display for each newly created product, and establishes the requisite product reference. Here's the rundown...

Event:

  • After saving a new commerce product

Condition:

  • Entity has field (ensure/assert that [commerce-product] has field field_sku)

The condition helps to make the sku field available in the [re]Actions...

Actions:

  • Create a new entity
    • Entity type: Content (or Node?)
    • Content type: <Your Display Type>
    • Title: [commerce-product:title] (inherit from product)
    • Author: [commerce-product:creator] (inherit from product)
  • Set a data value (<--trick responsible for successful product reference)
    • Step (1) above 'Create new entity' provides a variable for use in subsequent actions, by default I think it is 'product-display' or similar but feel free to rename it, let's call it 'my-new-product-display'
    • specify selected data as: my-new-product-display:field-products:0
    • specify value as: commerce-product
    • Save!
  • Save entity
    • Save: my-new-product-display

The resulting rule, when exported, looks like this gist.

This rule's workflow assumes a 1:1 relationship of product to product display. If a situation requires adding multiple product references to a given display, it will probably require a loop. I have not implemented that for my own needs, yet.

cheers!

luthien on May 14, 2013

"This rule's workflow assumes a 1:1 relationship of product to product display. If a situation requires adding multiple product references to a given display, it will probably require a loop. I have not implemented that for my own needs, yet."
does anyone knows how to do this?