2

I created a rule to be fired when a "New Commerce product is saved" Then I set a action to create a Product Display node and and another action to set the title for the Product Display to be the same as for the new commerce product. So far so good.

Now I need to create another action to set the field product of the Product display to be the same as the Commerce product SKU.

Here is the problem since I can not get this value. I tried to add a condition of the type "Entity has field" but it seems like the SKU vale is not a field at all.

How can I accomplish with this?

Code for the rule:

{ "rules_create_product_display" : {
    "LABEL" : "Create Product Display",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "commerce" ],
    "REQUIRES" : [ "rules", "entity" ],
    "ON" : [ "commerce_product_insert" ],
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "node",
            "param_type" : "product_display",
            "param_title" : "[commerce-product:title]",
            "param_author" : [ "commerce-product:creator" ]
          },
          "PROVIDE" : { "entity_created" : { "product_display_created" : "Created Product Display" } }
        }
      },
      { "entity_save" : { "data" : [ "product-display-created" ], "immediate" : 1 } },
      { "redirect" : { "url" : [ "product-display-created:edit-url" ] } },
      { "drupal_message" : { "message" : "Product display created. You can edit it at: [product-display-created:nid]" } }
    ]
  }
}

1 Answer 1

1

In order to access the SKU for the product you must first use two conditions in your rule to assure that you are dealing with the right type, these are :

  • Entity is of type commerce-line-item
  • Entity has field commerce_product

Just as an example this Rule export applies a discount ( 5_eur_disc ) to a product with an SKU of SKU-FOO-ITEM after checking that the discount dates are current.

{ "commerce_discount_rule_discount_discount_per_sku" : {
    "LABEL" : "Discount per SKU",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [
      "commerce_discount_date",
      "rules",
      "commerce_discount",
      "commerce_product_reference"
    ],
    "ON" : [ "commerce_product_calculate_sell_price" ],
    "IF" : [
      { "commerce_discount_date_condition" : { "commerce_discount" : "5_eur_discount" } },
      { "entity_is_of_type" : { "entity" : [ "commerce-line-item" ], "type" : "commerce_line_item" } },
      { "entity_has_field" : { "entity" : [ "commerce-line-item" ], "field" : "commerce_product" } },
      { "data_is" : {
          "data" : [ "commerce-line-item:commerce-product:sku" ],
          "value" : "SKU-FOO-ITEM"
        }
      }
    ],
    "DO" : [
      { "commerce_discount_fixed_amount" : {
          "entity" : [ "commerce_line_item" ],
          "commerce_discount" : "5_eur_discount"
        }
      }
    ]
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.