This site needs to enable registered users (not admin) to create new products that can be sold on the site. I have created a Product node type, and a Commerce Product Type. The node type has a product_reference field that can reference the Product type. This works fine, and I can create new products, and product nodes that reference those products.

I don't want standard members of the site (who are able to create products) having to go anywhere near the Commerce backend to create their products. Rather, I want them to be able to create Product nodes, and the system should create a product for them based on the node parameters.

I have this working using Rules - I'm using the 'After saving new content' event, checking the 'Content is of type' condition, and using the 'Create a new entity' action to create a Product entity - it works!

But the problem is, how can I reference this newly created Product entity in the node's product_reference field?

I have been trying using the 'Set a data value' action, but it's not working. I have it set like Parameter: Data: [node:field-product-product], Value: [entity-created] but when the rule runs, it tells me that Invalid data value given for the field. Here is the debug output from the rule.

* 0 ms Reacting on event After saving new content.
* 8.667 ms Evaluating rule Create product from product node.
* 9.523 ms The condition node_is_of_type evaluated to TRUE
* 9.566 ms AND evaluated to TRUE.
* 21.464 ms Evaluating the action entity_create.
* 23.499 ms Added the provided variable entity_created of type commerce_product
* 31.028 ms Evaluating the action data_set.
* 31.944 ms Unable to modify data "node:field-product-product": Invalid data value given. Be sure it matches the required data type and format.
* 31.985 ms Unable to execute action data_set.
* 32.109 ms Saved entity_created of type commerce_product.
* 38.434 ms Finished reacting on event After saving new content.

So, am I using the correct value to populate the [node:field-product-product] field ([entity-created]? I can't see what else it would be.

My thinking was that it's because the product entity isn't actually saved until the end. I tried adding a 'Save entity' Action to my rule, and told it to force the save to ensure that it was saved before attempting to update the product_reference field, but that didn't work either.

Also worth noting that currently this approach won't really work, until #971460: Error when creting node with empty product_reference field is fixed, since the rule doesn't get triggered at all if the product_reference field is blank - I'm using field_permissions to hide the product_reference field from standard members.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rszrama’s picture

Holy crap, dude. This is ingenious. Good job on coming up with the idea - getting this implemented will be a nice win for sites like yours and potentially for general usability. : )

So, about debugging it... I'm going to try and setup something similar and play around with it. Rules is still a bit of a mystery to me, but it looks like maybe the token you're using to set the value is returning an actual entity instead of just its ID. If we get stumped, I'm sure we can see if fago can clue us in. ; )

rszrama’s picture

Poked at this a bit and all I can say is without some serious debugging I won't come up with a solution. I also see that we're going to need to expose some data entry forms to Rules so you can do things like set the price of the new product to include both the price amount and currency code. I couldn't really tell how exactly I was supposed to set the product reference field or what kind of data it expects...

mrfelton’s picture

I suspect we can can already set the price using the set data value action. I think the problem here is just that the value of the [entity-created] token is not what the [node:field-product-product] field is expecting.

EDIT: Hmm.. maybe not. I thought I would be able to set the price using the [node:field-product-product:purchase-price] data selector, but rules complains "The selected data property doesn't support writing". How do we get a data property to support writing?! Maybe that's what you were talking about when you said 'expose some data entry forms'?

rszrama’s picture

Yeah, that's kinda what I was thinking, although I was actually thinking of the wrong form. : P

I did get that error message, too, but it kinda set me to thinking... why should we have to have a separate action to set the price when Price is a default, locked field on Products. It'd be much nicer to be able to set the price on the same action that creates the product.

mrfelton’s picture

Right - just like we can set the SKU, type and title when creating the Product Entity. It makes sense since all those fields are required.

rszrama’s picture

I'll have to connect with fago about this - I recently added proper support for setting price values, either the amount / currency_code separately or in a single price field widget, but it's still not working for me. Additionally, I discovered that the Entity module is doing manual support for node body fields, meaning I don't see a generic way for field data to be set (i.e. product reference fields).

Anyways, thought I'd at least let you know about the Price stuff in case you wanted to play with this further. I'm going to try and add support for selecting a product type instead of having that textarea.

rszrama’s picture

Cool, figured out product type selection and fixed currency code selection at the same time:

https://github.com/rszrama/drupalcommerce/commit/8b687377e67136e9233abeb...

tahiticlic’s picture

Any progress on this point? I'm facing a problem with this method : when adding the action to set the linked product, I've this error returned :

PDOException : SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '12' for key 'PRIMARY': INSERT INTO {node} (nid, vid, type, language, title, uid, status, created, changed, comment, promote, sticky, tnid, translate) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13); Array ( [:db_insert_placeholder_0] => 12 [:db_insert_placeholder_1] => 12 [:db_insert_placeholder_2] => forfait_baln_o_visible [:db_insert_placeholder_3] => fr [:db_insert_placeholder_4] => dfggf [:db_insert_placeholder_5] => 1 [:db_insert_placeholder_6] => 1 [:db_insert_placeholder_7] => 1297997311 [:db_insert_placeholder_8] => 1297997311 [:db_insert_placeholder_9] => 0 [:db_insert_placeholder_10] => 1 [:db_insert_placeholder_11] => 0 [:db_insert_placeholder_12] => 0 [:db_insert_placeholder_13] => 0 ) dans drupal_write_record() (ligne 6776 dans /data/Informatique/www/0270/includes/common.inc).

When only creating the Commerce product, there's no error. That's the product reference which is leading to this.

rszrama’s picture

I'm pretty sure you need this patch: http://drupal.org/node/1044342#comment-4069856

tahiticlic’s picture

That's the same with the patch applied.

As mrfelton, I have set an action "Set Data Value" with Parameter: Data: [node:field-produits-lies], Value: [entity-created]

(field-produits-lies being my product reference)

tahiticlic’s picture

Any chance for this method to work? did somebody succeed to achieve to create a product from a node and then to reference the product into the node?

WilliamV’s picture

subscribe

redrolr’s picture

I have read and taken a shot at this problem and I have a different angle. Something I am trying is, rather than having a product reference field on a content type, I have a boolean "For sale" field in the content type. Then I am attempting to create a rule that checks for this flag and if it is true, it creates a new product, otherwise it does nothing. In the rule, the SKU is [node:author]-[node:nid] and the title and price get passed on from the respective fields from the content type. Does this make sense?

Like @mrfelton, I don't want to give authors control of creating products and if a product reference field is provided, it seems logical that the user would need some knowledge of creating a product to populate the product reference field. This is my justification for taking the flag field route.

That said, I am having a problem creating the rule that I hoped would solve this problem. In order to keep the peace, I'll post that question elsewhere.

Cheers!

ytsurk’s picture

sub

WilliamV’s picture

I want to create a rule that creates a node upon creation of a product.

I have set a specific product type and created a similar content type.

As rule i apply:

  • After saving a new product
  • Entity exists by property
    Parameter: Entity type: Product, Property: [product:type], Value: [product:type]
  • Create a new entity
    Parameter: Entity type: Node, Content type: Test1, Title: [product:title], Author: [product:creator]
    Provides variables: Created entity (node)

What would be the correct values for 'Entity exists by property - Parameter: Entity type: Product, Property: [product:type], Value: [product:type]' knowing the product type would have the value of Test1?
'

djalloway’s picture

Here is an exported Rule to create a new Product Display Node when a new Commerce Product has been created.
A few things to take note of are:

  • My Product Display content type is called product_display
  • My Product Reference field on my Product Display content type is called field_product_reference
  • This is 'not' accounted for in this rule, but you may want to consider what should happen on a Product Display Node update, or a Commerce Product update.
{ "rules_new_commerce_product_display" : {
    "LABEL" : "Create Product Display for new Commerce Product",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_product" ],
    "ON" : [ "commerce_product_insert" ],
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "node",
            "param_type" : "product_display",
            "param_title" : "[product:title]",
            "param_author" : [ "product:creator" ]
          },
          "PROVIDE" : { "entity_created" : { "product_display_created" : "Created product display" } }
        }
      },
      { "data_set" : {
          "data" : [ "product-display-created:field-product-reference" ],
          "value" : [ "product" ]
        }
      }
    ]
  }
}
WilliamV’s picture

Thanks!
I'll give this a try and keep you informed.
Grtz.

tahiticlic’s picture

@djalloway : have you tried to create a Commerce Product from the Display Node? (since this is the goal of this thread)

Scott J’s picture

tahiticlic’s picture

Yep, exactly. But the purpose is completely different here since there's a need to manage only nodes and no products at all.

Is there something usable to achieve the main goal of this thread : automatically create a product from a node, and then reference it in the node?

There's a very important point to notice : final clients of Commerce are not geeks... We (developers) can handle several forms and concepts, but a lambda user is not familiar with such concepts. If you try to bend him toward the logic of your tool, I guess he will choose a more user friendly software, like prestashop, even if this software is less powerful.
Moreover, among the fact that my clients won't understand why they have to fill 2 forms to present one product, they won't have time to do so... Saying that, I think that this point to have auto creation of a product from a node is essential.

WilliamV’s picture

Category: support » feature
Priority: Normal » Major

I can only confirm your statement and insist that this topic becomes a feature request and thus a standard implementation for the commerce-module.

sonar_un’s picture

I definitely want to see this reaction get implemented. I have already had a difficult time explaining the difference between display and the product in the database. Most customers won't understand the need for all of the extra work. If the rule can somehow use a field (Say for instance a multiple selector for shirt size) then, create the three separate products in the commerce-product, this would also be a definite benefit.

We could use commerce_feeds in order to just create product displays, and then the commerce products would be created automatically as well.

djalloway’s picture

apologies, yes #16 belongs at the thread mentioned in #19
thanks for the catch.

rfay’s picture

Title: Using Rules to create product when node is creted, and reference the newly created product in a product_reference field » Using Rules to create product when node is created, and reference the newly created product in a product_reference field

For usability, we also need a contrib module to do just this - wrap it up nicely and just create everything that needs to be created.

patoshi’s picture

subscribing. would also like a contrib module to add this extra feature. seems troublesome to have to create a product and then fill out another page just for body information.

lupux’s picture

subscribing

martinduys’s picture

subscribing

martinduys’s picture

subscribe

tahiticlic’s picture

FileSize
1.26 KB

For those who are still interested, here is a module that you can use to do this. No time enough to maintain it, but it works for my purpose and maybe that can help!

rfay’s picture

Thanks for sharing that, @tahiticlic. I took a look, and it inspires many thoughts. It's too specific to your particular purpose to make a start of a generic module, but it paves the way to something else. Thanks!

ytsurk’s picture

lovely - and so easy ;)

merci for sharing !

Stalski’s picture

I'll possibly give this a shot to put this in a contrib.

Can we describe something here what the module should do, what it should not, ... and where the boundaries are going to be.

A quick start:
- Define a content type that has the same name as the product types. Keeping this somehow in sync? Maybe providing a conventional way to name the new content type. E.g.: Product => Product display, Tshirt => Tshirt display, etc ..
- Make a rule that takes (all) fields of the entity and delegates (selectable through configuration?) them to the new content type. It must be possible to fully create a valid product entity from a node.
- We need functionality when a product (display) node has been created, to check whether the product entity already exists and thus reference it or create a new one and reference it.

Thanks in advance for the feature request. Other things that the new contrib could facilitate?

regards
Stalski

Summit’s picture

Subscribing, greetings, Martijn

pixelsweatshop’s picture

sub.

seren10pity’s picture

Subscribing.

seren10pity’s picture

Title: Using Rules to create product when node is created, and reference the newly created product in a product_reference field » error on product creation since I imported the rule to auto-create product display

I have problem with the rule above:
Since I imported it into Rules, I've got this error when creating a new commerce product :

EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityDrupalWrapper->set() (line 694 of D:\Documents\PROJETS\drupal7\sites\all\modules\entity\includes\entity.wrapper.inc).

The product display is not created.

I think this comes from this part of the rule :

{ "data_set" : {
          "data" : [ "product-display-created:field-product" ],
          "value" : [ "product" ]
        }
      }

(my reference field in display product content type is named 'field-product' and not 'field-product-reference' ; it comes from the "commerce_kickstart" profile I used to config my drupal site.)

rszrama’s picture

Title: error on product creation since I imported the rule to auto-create product display » Using Rules to create product when node is created, and reference the newly created product in a product_reference field
Priority: Major » Normal

Let's leave the title as is. My hunch is the Rule as exported above needs to be updated for changes we made in beta4 to the parameter names / data selector names in our Rules integration.

seren10pity’s picture

Oh, sorry for the title, I didn't want to change the whole issue title.
And thanks rszrama for the clue. I'll see if I can compare the versions to identify the problem ;)
I'll re-post if I find something that works.

vthirteen’s picture

it's rather intuitive, set a rule with these basic elements:
EVENT "After saving a new commerce product"
ACTION 1 "Create a new entity" and set parameters according to your variable: node type, Title will likely be [commerce-product:title], author will be [commerce-product:creator], etc.
ACTION 2 "Set a data value" something like this: Parameter: Data: [entity-created:field-product], Value: [commerce-product] (my product reference field is called "field-product", that may vary, of course.

i set all information about a product in the commerce product creation form, while in the node creation (that is the creation of a "on-sale" product display) all data are taken from the product information, no need to add anythng else.

if you're using taxonomy that might be a little bit more complicated because i didn't find a way to have a rule/action for the taxonomy data set (yet, i just started using the commerce module last night)

tahiticlic’s picture

@vthirteen : again, this is not the goal of this thread which is "Create a product from a node".
Indeed to create a node from a product is the easy part.

hubrt’s picture

+1

michob’s picture

Has anyone else gotten this to work? I have it working, but I've tried so many iterations that I'm not sure what the magic sauce is. Here's my rule, anyway:

{ "rules_create_product_node" : {
    "LABEL" : "Create Product Node",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "node_insert" ],
    "IF" : [
      { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "proddisp" : "proddisp" } } } }
    ],
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "commerce_product",
            "param_sku" : "[node:nid]",
            "param_type" : "basic_product",
            "param_title" : "node:title] ([node:nid])",
            "param_creator" : [ "node:author" ]
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      },
      { "data_set" : { "data" : [ "entity-created:commerce-price:amount" ], "value" : "10000" } },
      { "entity_save" : { "data" : [ "entity-created" ], "immediate" : 1 } },
      { "data_set" : { "data" : [ "node:field-prodref" ], "value" : [ "entity-created" ] } }
    ]
  }
}

If somebody gives this a try, can you post back on whether it works or not? If not, I'll add some of the other bits that might be at play here...

GiorgosK’s picture

Could not import your rule (probably because I am using all dev versions or some module confict)
but I recreated your rule and it seems to do the right thing

of course one has to implement rules for when node is updated/deleted/translated in order to get a complete workflow where the END USER does not need to touch the commerce products ...

michob’s picture

Hmm.. You're right. I've been focusing on the creation scenario - I guess that's something else for the to do list. :)

Thanks!

whisper73’s picture

The new approach of decoupling products from their display nodes has some benefits which are clear. I'm still getting my head around it all. However, I'm still not completely sold on the idea because of the amount of configuration required to get a simple store up and running.

I was able to create a rule to create a display node after creating a product, thanks to the example above. This works fine for create, but not so good for update as you have no reference to the node on the product entity.

Looking at it the other way round, (Create Node => Then Create Product) I can't see the logic behind creating a node with all the required fields and then copying them to a new product entity. This is duplicating all of your data and clearly breaking the DRY principle.

http://en.wikipedia.org/wiki/Don%27t_repeat_yourself

You also run into problems with taxonomy. What's the recommended approach? Should you put taxonomy on your display node or on your products? Assigning taxonomy terms to the product entity seems to be the logical approach, but this doesn't integrate well with core taxonomy system. i.e. your terms are displayed on the product, but when you select a term, nothing is displayed.

Search integration is also a concern in that the product fields won't be included in the search index... only the fields on the display node.

I would simply like to be able to display my products, have the data stored in one place and have the cart/taxonomy link to those products. Would it be possible to create view to display a single product and have that link from the cart??

I understand that the commerce module is still in it's infancy and I can see that's it's definitely the way forward. However, I think providing similar functionality to uber-cart out of the box would make the transition easier and promote early adoption.

Any help in pointing me in the right direction would be appreciated.

Cheers ;)

GiorgosK’s picture

FileSize
38.44 KB
61.04 KB
51.36 KB

Here is a simple set of RULES that will Create/Update/Delete a product (commerce product) when a product_display (node type) is created/updated/deleted

it can be used with multilingual stores when used together with entity & entity_translation (which implies that only one product_display is created and fields are translatable using entities

I am using all bleeding edge DEV versions of commerce/drupal7/entity/entity_translation/rules since some things where not working with earlier versions

NOTES:

1) I have 3 extra fields on both product and product display
notes (textfield) - image (cck image field) - price (price with currency)
which I synchronize on update

2) The weight -1 is not needed

3) I get an warning message when image is being synchronized on create/update but the image does get synchronized but it mentions nothing about image (when not using image no warnings appear)

    Notice: Array to string conversion in DatabaseStatementBase->execute() (line 2108 of G:\drupal\includes\database\database.inc).
    Notice: Array to string conversion in DatabaseStatementBase->execute() (line 2108 of G:\drupal\includes\database\database.inc).
    Notice: unserialize() [function.unserialize]: Error at offset 0 of 5 bytes in commerce_price_field_load() (line 85 of G:\drupal\sites\all\modules\commerce\modules\price\commerce_price.module).

I have tried lots of different configurations with RULES and I would appreciate any feedback if anyone can improve on these or correct the warning

4) the notes/image fields can't get synchronized with "data set action" according to this discussion (#1221446: No tokens for custom fields) so I use php code to do it

Here is the exported rules (attaching also screenshots)

{ "rules_create_product" : {
    "LABEL" : "Create Product",
    "PLUGIN" : "reaction rule",
    "WEIGHT" : "-1",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "node_insert" ],
    "IF" : [
      { "node_is_of_type" : {
          "node" : [ "node" ],
          "type" : { "value" : { "product_display" : "product_display" } }
        }
      },
      { "data_is_empty" : { "data" : [ "node:source" ] } }
    ],
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "commerce_product",
            "param_sku" : "[node:field-prodcode]",
            "param_type" : "product",
            "param_title" : "[node:title]",
            "param_creator" : [ "node:author" ],
            "param_commerce_price" : [ "node:field-price" ]
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      },
      { "data_set" : {
          "data" : [ "entity-created:field-images" ],
          "value" : [ "node:field-images" ]
        }
      },
      { "entity_save" : { "data" : [ "entity-created" ], "immediate" : 1 } },
      { "data_set" : { "data" : [ "node:field-product" ], "value" : [ "entity-created" ] } }
    ]
  }
}
{ "rules_update_product" : {
    "LABEL" : "Update Product",
    "PLUGIN" : "reaction rule",
    "WEIGHT" : "-1",
    "REQUIRES" : [ "rules", "php" ],
    "ON" : [ "node_update" ],
    "IF" : [
      { "node_is_of_type" : {
          "node" : [ "node" ],
          "type" : { "value" : { "product_display" : "product_display" } }
        }
      },
      { "NOT data_is_empty" : { "data" : [ "node:field-product" ] } }
    ],
    "DO" : [
      { "entity_fetch" : {
          "USING" : {
            "type" : "commerce_product",
            "id" : [ "node:field-product:product-id" ]
          },
          "PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
        }
      },
      { "data_set" : { "data" : [ "entity-fetched:sku" ], "value" : "[node:field-prodcode]" } },
      { "data_set" : { "data" : [ "entity-fetched:creator" ], "value" : [ "node:author" ] } },
      { "data_set" : { "data" : [ "entity-fetched:title" ], "value" : "[node:title]" } },
      { "data_set" : {
          "data" : [ "entity-fetched:commerce-price" ],
          "value" : [ "node:field-price" ]
        }
      },
      { "php_eval" : { "code" : "$entity_fetched-\u003efield_images = $node-\u003efield_images;\r\n$entity_fetched-\u003efield_notes = $node-\u003efield_notes;\r\n" } },
      { "entity_save" : { "data" : [ "entity-fetched" ], "immediate" : 1 } }
    ]
  }
}
{ "rules_delete_product" : {
    "LABEL" : "Delete Product",
    "PLUGIN" : "reaction rule",
    "WEIGHT" : "-1",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "node_delete" ],
    "IF" : [
      { "node_is_of_type" : {
          "node" : [ "node" ],
          "type" : { "value" : { "product_display" : "product_display" } }
        }
      },
      { "NOT data_is_empty" : { "data" : [ "node:field-product" ] } }
    ],
    "DO" : [ { "entity_delete" : { "data" : [ "node:field-product" ] } } ]
  }
}
starsinmypockets’s picture

Subscribing

itamair’s picture

Subscribing ...

petednz’s picture

Hi GiorgosK - thanks for sharing this - does this require other steps before trying? I created the Node and Commerce 'items' with same fields and then tried to import the first set of Rules and got this error
Integrity check for the imported configuratoin failed. Error message: action data_set: Missing configuration for parameter data..
I think that I have all the fields and names aligned and don't think the above msg refers to that sort of problem anyhow. Any suggestions - have i missed a major step?

GiorgosK’s picture

@petednz
I would not know what this error means
but try installing all the latest dev for all mentioned modules

did you read carefully my description / setup ? did you read this issue ? #1221446: No tokens for custom fields

try to create the rules by hand because I think I had the same problem with importing

petednz’s picture

thanks - i will try rebuilding the Rules by hand then

mefisto75’s picture

sub

TimelessDomain’s picture

This would be a great feature.
Perhaps we could take the NAT http://drupal.org/project/nat d7 approach (of linking fields b/w content types & taxonomies), and allow mapping of fields to be synchronized between Product Entities & nodes. This would allow you to choose which fields should be synced & also allow "unrelated" fields to sync if needed.

This would have to be an add-on module (maybe take module in #29 and mod it to do so).
It would make the whole system much easier to manage.
You could also then have product entities being updated automatically by data being inputted into different product nodes that are referencing that product entity.

I didn't see if anyone mentioned a 1 time sync in between entity/node, but there should be an option to choose if it is only populated upon new node & entity creation, upon new node creation & updating pre-existing entity, OR upon updating a pre-existing node which would update the pre-existing entity. You could just have Yes/No rules for whether these actions should be taken per Product Entity <-> Product Node Relationship.

You could also push data the other way, then update the nodes which are referencing a product entity w/ the new product entity data. This would ensure that data (taxonomies, images, etc.) stay attached to the product entity while still being able to be displayed through the node system properly.

Just some stuff to think about as this system is developed. We should try to make it as automated as possible.

kitesystems’s picture

I just wanted to share my experience with this topic. I know this post is about going from Product Display -> Product but after some deliberation I decided it is more appropriate to do it the other way round as this minimizes data redundancy. My approach is to put all the product fields in the product type and no duplicate fields in the associated "product display" content type. I will then have users add products through the store and I have a rule which creates the "product display".

I was skeptical about the separation at first but after getting this to work, and also realising how well Commerce uses Views and follows the Drupal way I am sold.

Relationships in Views are the key to making life with Commerce Module fun and rewarding.

Mike

GiorgosK’s picture

@kitesystems
it makes sense
do you mind sharing the add/delete/update rules so we can try them out ?
or just post them in #1030896: Automatically create a product display node when a new product is created where its more appropriate

GiorgosK’s picture

A different version of what kitesystems is proposing would be to create node displays on the product create form using http://drupal.org/project/commerce_product_display_manager

only problem is that for multilingual sites you can't specify language of node display
if you are insterested please help out #1236498: More fields for node display creation

carn1x’s picture

I have managed to get the rules working whereby the product display is created by rules when the product is created, however I'm having a hard time trying to figure out how to fetch the product display node which refers to the product using rules for the Update/Delete rules.

Below is an example of the rule for creating the product display which I previously posted on DrupalAnswers here:


{ "rules_create_product_display" : {
    "LABEL" : "Create Product Display",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "entity" ],
    "ON" : [ "commerce_product_insert" ],
    "IF" : [
      { "data_is" : { "data" : [ "commerce-product:type" ], "value" : "**PRODUCT_TYPE**" } }
    ],
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "node",
            "param_type" : "**NODE_TYPE**",
            "param_title" : "[commerce-product:title]",
            "param_author" : [ "commerce-product:creator" ]
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      },
      { "data_set" : {
          "data" : [ "entity-created:**PRODUCT_REFERENCE**" ],
          "value" : [ "commerce-product" ]
        }
      }
    ]
  }
}

You will need to substitute your own values for:

* PRODUCT_TYPE (product type that has been created)
* NODE_TYPE (node type being created)
* PRODUCT_REFERENCE (field that will reference the created product)

zeta1600’s picture

subscribing...

GiorgosK’s picture

addition on #56

Multilingual sites with going from product > product display which is opposed to this thread but might be useful to some.

Even multilingual sites can be supported using http://drupal.org/project/commerce_product_display_manager

here is an outline how I did it

using lateste dev releases of
commerce
commerce_product_display_manager
entity
entity_translation
title

The commerce product will have ALL required fields
Make product entity translatable and can use title to make also the title entity translatable

The product display (node) will have a product reference only

Store owner should have permission to add/edit/update commerce products and product display

on the product creation form a "refenced by" field exists tha makes the connection
between product and product display (provided by project/commerce_product_display_manager)

product can get translated but product display will be untranslated (default behaviour of project/commerce_product_display_manager)

Now modify product/cart views to use product<->product display relationship and change details to output product title instead or product display title

thus #1236498: More fields for node display creation is not necessary to assitst in multilingual stores

pardalman’s picture

subscribing

tahiticlic’s picture

@kitesystems, your point is a technical point... so for final user, it's worthless.

The discussion is to create a Commerce product from a node, can we maintain this post this way?
The main goal is not technical, even if solutions require technical discussion. It's a usability goal.

If you want to give solution for the (trivial) Commerce product -> Node auto creation, maybe another issue would be the best place.

I guess the best solution is given by Giorkosk on http://drupal.org/node/971484#comment-4753706 and if it is tested correctly, perhaps this could be included in a module.

firfin’s picture

Setting up the Rules as outlined in #46 worked for me. I am not using multi-valued fields. I did not get any errors and was pleasantly surprised by the user-friendliness of the rules UI.

Go to config -> (workflow -> ) rules and set up your rules there. I used #46 as a guideline. But the process isn't that hard, basically:
- create the entity and set the fields
- save it
- set the product ref in the node to the entity

The hardest part ( I think) is deciding which fields you want/need in the Product display (the node) , the Commerce product (the entity) or in both.

Using:
- Drupal core 7.7
- Commerce 7.x-1.0
- Token 7.x-1.0-beta5

tahiticlic’s picture

You're right, the process is hard, that's why embedding this in a module would be great and would give Commerce a user-friendly process to hide the complexity of Commerce product creation.

pixelsweatshop’s picture

For those that are not aware, Ryan is already addressing this issue via http://drupal.org/sandbox/rszrama/1181848

For the mockups #1181862: Design the widget

starsinmypockets’s picture

subscribe :)

Jason Dean’s picture

subscribing

ardnet’s picture

subscribe

Chi’s picture

Title: Using Rules to create product when node is created, and reference the newly created product in a product_reference field » Using Rules to reference the newly created product in a product_reference field

I have the same problem as mentioned in 36.
I tried to set rule manually but I cannot find available token for value of entity-created:field-product

Data: [entity-created:field-product], Value: [commerce-product]

The last release of commerce and rules were used.

I think there is no any reasons to start separate issue about product display creation. In fact the problem is not about creation of product or display node.

carn1x’s picture

@Chi, are you bringing the field into scope?

If you want to access any custom field with Rules, you need to first set a Condition: Entity Has Field and select the field you want to access.

Chi’s picture

@carn1x, which field I need select in condition?
I am trying to import rule from #16. Why that rule doesn't contain any conditions?

Note:

  • My product type is called conditioner.
  • My product display node type is called product_display.
  • My product reference field on the product display node type is called field_product.
lukus’s picture

Status: Active » Closed (fixed)

In an attempt to collate all information related to rules and product display, please continue discussion here: http://drupal.org/node/1300170

Chi’s picture

So if you have similar problem as described in #36 go to the Structure » Content types » YOUR_DISPLAY_TYPE » fields
and set for your product field Number of values = 1.
Hope this helps...

simg’s picture

tahiticlic’s picture

Status: Closed (fixed) » Active

Well, this is marked as fixed for the reason "please, contribute here now", where the "here" is a post with the exact opposite (and easy as it has been said) subject. Maybe this is not a good reason to mark it fixed, don't you think?

I think this is one of the most important concern about Commerce today : the usability missing. So please, can we stick with this problem to create a Product from a node (which is the Product Display in this case)?

This is a use case for customers (which are e-shops managers), telling them "you've to use a 'Product' instead" is not an option because they will tell us "ok, too complex, I'll go with another solution then".

rszrama’s picture

Status: Active » Closed (duplicate)

Can I mark it closed for a different reason, perhaps? I've always felt that Rules based synchronizations are too brittle to be used widely, especially if the purpose is to improve the usability for non-technical users. Instead we're finalizing the Commerce Inline Product Form module that embeds product administration capabilities into the node form via a product reference field widget. Check that out and feel free to post follow-up comments or ideas there.

http://drupal.org/sandbox/rszrama/1181848

pitxels’s picture

I don't wan to create a new issue about this, because I am sure I am missing the big picture, so here is my question:
Why product and product display are not both node types?

One could be able to use the simple product node for simple stores.
And a product display scenario for more complex products with options, colors and stuff...

pixelsweatshop’s picture

Drupal Commerce separates the creation of products (entities) and the display of products (nodes). This means that when you have set your product up, you need to create a display node before a customer can see the item on your website.

Why is this?

Initially, this may seem like duplication of effort, but there are a number of reasons for it that you will appreciate as you build your Drupal Commerce Store.

This relationship is similar to a warehouse and retail location. A warehouse (product) is organized differently from how you would organize a retail store (product display).

You don’t have to display a product in order for it to be added to the cart (eg for subscriptions)
You can combine multiple products together into one display (allowing you to show product variations).
If you no longer want to display a product on your site, you can delete it and still maintain the product details for order history.

The product display can contain more information than the product itself - useful for landing pages and special promotions.

A product can have more than one display - so you can promote the same product in different ways on the site.

pitxels’s picture

Yes I understand they are separated
products (entities), display of products (nodes)

but why not
products (nodes), display of products (nodes) ?

pixelsweatshop’s picture

rei’s picture

the update price at #46 is not working. The price at commerce product still always the same, only the price at product display that changed. I already tried changing with :amount, but still not updated.
{ "data_set" : {
"data" : [ "entity-fetched:commerce-price:amount" ],
"value" : [ "node:field-price:amount" ]
}

is this rules bug ?

vlkff’s picture

I solved the products to content sync task with a different view point and created a custom module for it.
I was thinking about building the code on rules, but this solution seems a tricky for me.

Now I created a contrib module based on it and committed to the sandbox.

You may get it from http://drupal.org/sandbox/vlkff/1516510.

I hope that would be useful.

carn1x’s picture

I was recently asked to provide the rules for editing and deleting products as well, so here's the export for all 3 rules I'm using on a recent project. Specifically they check whether the product type is a "deal" which is relevant to my current project, but I thought it might be useful to leave in:

Create:

{ "rules_create_deal_display" : {
    "LABEL" : "Create Deal Display",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "entity" ],
    "ON" : [ "commerce_product_insert" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "commerce-product" ], "field" : "field_deal_display_node" } },
      { "data_is" : { "data" : [ "commerce-product:type" ], "value" : "deal" } }
    ],
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "node",
            "param_type" : "deal",
            "param_title" : [ "commerce-product:title" ],
            "param_author" : [ "commerce-product:creator" ]
          },
          "PROVIDE" : { "entity_created" : { "node" : "Display node" } }
        }
      },
      { "data_set" : {
          "data" : [ "node:field-node-product-reference" ],
          "value" : [ "commerce-product" ]
        }
      },
      { "entity_save" : { "data" : [ "node" ], "immediate" : 1 } },
      { "data_set" : {
          "data" : [ "commerce-product:field-deal-display-node" ],
          "value" : [ "node" ]
        }
      },
      { "component_rules_set_the_display_node_status_to_the_product_display_status" : { "node" : [ "node" ] } }
    ]
  }
}

Delete:

{ "rules_delete_deal_display" : {
    "LABEL" : "Delete Deal Display",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "entity" ],
    "ON" : [ "commerce_product_delete" ],
    "IF" : [
      { "data_is" : { "data" : [ "commerce-product:type" ], "value" : "deal" } }
    ],
    "DO" : [
      { "entity_delete" : { "data" : [ "commerce-product:field-deal-display-node" ] } }
    ]
  }
}

Update:

{ "rules_update_deal_display" : {
    "LABEL" : "Update Deal Display",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "entity" ],
    "ON" : [ "commerce_product_update" ],
    "IF" : [
      { "data_is" : { "data" : [ "commerce-product:type" ], "value" : "deal" } }
    ],
    "DO" : [
      { "data_set" : {
          "data" : [ "commerce-product:field-deal-display-node:title" ],
          "value" : [ "commerce-product:title" ]
        }
      },
      { "component_rules_set_the_display_node_status_to_the_product_display_status" : { "node" : [ "commerce-product:field-deal-display-node" ] } }
    ]
  }
}
TelFiRE’s picture

How frustrating that I can find tons of documentation on this issue dating back for years, yet no one has solved it definitively :/

It's easy to have a node created. What is the secret behind getting that product SKU into the product reference field? I can't afford to spend more time on this so hopefully I can find an answer or Ubercart it is.

tahiticlic’s picture

Yep... I can't figure out why this complexity is maintained.

It's like having a F40 that would need an engineer diploma to start.

Commerce is a really nice eCommerce module, but if customer can't use the basics and feed the store, there's now way for them to use the thing.

chrisjlee’s picture

@tahiticlic They're addressing this issue with the 2.x branch of the kickstart distribution. But like all drupal projects; If you have a problem with drupal start patching :)

carn1x’s picture

I personally have no issue with the current workflow, but some helper modules to provide the product displays to those who need it would be great. I have recently worked on a site that where it was desired to NOT have individual product nodes, and only required display via a single view, so this product / node distinction was very useful in that instant.

rszrama’s picture

Have you guys looked into http://drupal.org/project/inline_entity_form yet? It's a more robust solution: embed the product form directly into the node form. Then your store administrators only have to know "I need to add a product page to my site" and they're good to go.

tygbiten’s picture

I use the Inline entity form atm. A good solution. However I'm still interested in automating even further and assign product references in a newly created node with products with a bunch of hidden settings based on the choice of one single setting made by the user. I guess this is still not possible? Oh and subscribe :)

fdefeyter@gmail.com’s picture

To me it's very easy.

Products are automatically created by Migrate Module.

I'd like to update only the product field of the Node Display with Rules or any other possibility... I think I will have to do a SQL update in my migrating code... What a tough project this has been.

OK the result is fine but it took me a lot of time :-(

If somebody can help me with rules, he's a angel :-)

TimelessDomain’s picture

I have not found a way to populate the Product Reference field with the SKU of a newly created product entity that was created via a rule triggered from creating a product display node. Forcing a 2-way relationship between a Product Reference field & an Entity Reference field would fix this as it is currently possible to fill in a Entity Reference field on the product entity with the NID of the triggering product display node.

From Corresponding Entity References - #1635438: Work with Commerce Product Reference Fields?
"Corresponding Entity References should provide an api for custom entities to become correspondable. Product References would be a valuable start. Long-term all types of entities, limited or not, should be able to relate to each other in a 2-way relationship that can be triggered from either direction. Thanks"

Another option would be (Entity)Reference Field Synchronization - #1720082: Synchronize Entity References with Product References "(Entity)Reference Field Synchronization allows you to keep 2 Entity reference fields in sync."

Entity Sync "Synchronize entities (or aspects of them) between Drupal installation with Rules -triggered actions."

Commerce Product Entity Sync "This module is automatically synchronizes entities and it's (their's) fields to the commerce products." (not tested yet)

tmsimont’s picture

I just updated #1635438: Work with Commerce Product Reference Fields? with a patch in an attempt to address the issue referenced in #90.

If anyone here is interested in using Corresponding Entity References, please give that patch a try and let me know if it solves or creates any issues.

giorgio79’s picture

One other option is to scrap Rules for this and just go for a simple hook_node_insert and hook_node_update in your module to create the product entity like #29. Thanks for sharing the code. :)

timdiacon’s picture

This is working great for me :-)

I am creating a node and the rule is creating my commerce product then populating the reference field on the node linking it to the product :-)

gilfo1’s picture

I was able to set the product reference value with rules after adding a rules action to save the entity first:

Save entity
Parameter: Entity: [entity-created], Force saving immediately: true

Then the next action (after saving above) worked:

Set a data value
Parameter: Data: [node:field-product-reference], Value: [entity-created]

Toki’s picture

I have built a rule to create 9 new products when a specific node is created (this new node needs the 9 new products, each one for a specific conditioning).
So everything is fine with "Create an entity" and I get my 9 new products each time a node is saved.
But now (as the title mentions from the beginning), I need to connect the new products to the fresh new node (9 values).
And here is my problem : when I start using "Set a data value", I can select the Product reference field from the node but the tokens for the value do not allow me to choose [entity-created] or [entity-created:sku], I only get price, author and feed-node?
So, I guess the solution above is only working for a single value product reference field?
How could I build my rule with a multi-value product reference field?
Maybe using "Add an item to a list"?

EDIT : I found a solution, even if it was not really intuitive. First create an entity to have a new product, then save this entity and finally add an item to a list and the Product reference field can host the new product entity saved. Hope this could helps someone.

4kant’s picture

Thanks to #94!!!
"Force saving immediately: true" - that is what I had been missing.