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.
Comments
Hello! For adding a
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
Product Reference
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!
If it works better for you to
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.
Thanks so much.. though I
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!
For adding PHP Code in
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"
Please how to load the entity by id
I've added execute arbitrary php code , and it returns the entity_id , but when I add the action load entity by ID I don't have the returned value from php code ,
Thank's for support
[resolved] > < on step 3
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)
Module
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.
product display manager
@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.
views_megarow + quick edit
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.
bulk product creation
bulk product creation also appears to similarly ease the creation of products & product displays.
create rule working great...
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:
Condition:
[commerce-product]
has field field_sku)The condition helps to make the sku field available in the [re]Actions...
Actions:
[commerce-product:title]
(inherit from product)[commerce-product:creator]
(inherit from product)my-new-product-display:field-products:0
commerce-product
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!
adding multiple product references to a given display
"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?