Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rule-based node creation: commerce product + product display node set

I'm trying to bind a Commerce product type to my own custom type node (serving as a display node). The goal is to enter new data in as few places as possible. I'm therefore exploring a rule-based creation of one type upon creation of the other. Seems like both directions are working. Of the two though, I prefer automatic creation of a Commerce Product upon user creation of Custom Type node, which will then serve as a product display.

I was wondering if anyone has been through this choice and could recommend this. Also, is the commerce_product_display_manager module necessary?

like image 894
user776686 Avatar asked Oct 10 '22 16:10

user776686


2 Answers

Commerce Product Display Manager is not necessary, I've gotten this to work and I've never used that module.

I went for the route of automatically creating a Node after saving the Product.

Below is my Rules export for this:

{ "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'll 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)

Sorry I can't dedicate more time to a better answer now, let me know if you'd like me to elaborate on the process of creating the above using the GUI

like image 85
DanH Avatar answered Oct 13 '22 05:10

DanH


The above example was useful but here is a more specific one:

{ "rules_create_product_display_on_product_creation" : {
    "LABEL" : "Create Product Display on Product creation",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "entity" ],
    "ON" : [ "commerce_product_insert" ],
    "IF" : [
      { "entity_is_of_type" : { "entity" : [ "commerce-product" ], "type" : "commerce_product" } }
    ],
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "node",
            "param_type" : "product_display",
            "param_title" : "[commerce-product:title]",
            "param_author" : [ "commerce-product:creator" ]
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      },
      { "data_set" : {
          "data" : [ "entity-created:field-product:0" ],
          "value" : [ "commerce-product" ]
        }
      }
    ]
  }
}

The only problem I had was with the second action ("data_set")- it was important to select "entity-created:field-product:0", not the "entity-created:field-product" to make it work because we want to assign specific product and not a list of products.

This example is using the standard product display node type (product_display) but you can change it with the one you are using. Also have in mind that this is working only for one product type - for every product type a separated rule should be created. You may create also a rule for deleting the product display node when deleting the product. This rule is useful only when you have connection one product-one product display. If you need to add more products per product display (colors, images with different prices) then you have to use Commerce Bulk Product Creation module.

like image 34
pavlovt Avatar answered Oct 13 '22 05:10

pavlovt