1

I'm trying to create a rule for automatically generating a commerce products titles from all the values of products fields. After each values I need to add suffix like cm, W, kg, mm e.g. In order to make the title beautiful, for example "Product Name 10cm 150W 35kg" instead of "Product Name cm W kg", I need to check for empty tokens, but this check for empty tokens is my problem.

$power = "[current-product:field_power]";
if (!empty($power)) {echo "$power". "W ";}
echo strlen($power); //test for what happens

Second line all time print "W" with or without value. (Empty check not work) The third line all time print "29" (same as token name length). As a result I can't check $power var for emptiness. For node fields I success with token replace:

$brand = token_replace('[node:field-brand]', array('node' => $node));
if ($brand != "") {echo $brand . " ";}

But token replace not work for products variants. How correctly use token replace for products field of how to check products field for emptiness without token replace?

Export of rule:

{ "rules_change_product_variants_titles_after_node_save" : {
    "LABEL" : "Change product variants titles after node save",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "php", "rules" ],
    "ON" : {
      "node_insert--product" : { "bundle" : "product" },
      "node_update--product" : { "bundle" : "product" }
    },
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "node:field-product-variants" ] },
          "ITEM" : { "current_product" : "Current product variant" },
          "DO" : [
            { "data_set" : {
                "data" : [ "current-product:title" ],
                "value" : "[node:title] \r\n\u003C?php\r\n    $brand = token_replace(\u0027[node:field-brand]\u0027, array(\u0027node\u0027 =\u003E $node));\r\n    $type = token_replace(\u0027[node:field-type]\u0027, array(\u0027node\u0027 =\u003E $node));\r\n\r\n\/\/add space after brand and type of product display\r\n    if ($brand != \u0022\u0022) {echo $brand . \u0022 \u0022;}\r\n    if ($type != \u0022\u0022) {echo $type . \u0022 \u0022;}\r\n\r\n    $x = \u0022[current-product:field-width]\u0022;\r\n    $y = \u0022[current-product:field-height]\u0022;\r\n    $z = \u0022[current-product:field-length]\u0022;\r\n    $diameter = \u0022[current-product:field_diameter]\u0022;\r\n    $power = \u0022[current-product:field_power]\u0022;\r\n    $weight = \u0022[current-product:field-weight]\u0022;\r\n\r\n\/\/ make product size like 10x20x15cm, not like 10cmxcm15cm\r\nif (empty($x)) {\r\n  if (empty($y)) {\r\n    if (empty($z)) {}\r\n    else {echo $z . \u0022cm \u0022;}\r\n  }\r\n  else {\r\n    if (empty($z)) {echo $y . \u0022cm \u0022;}\r\n    else {echo $y . \u0022x\u0022 . $z . \u0022cm \u0022;}\r\n  }\r\n}\r\nelse {\r\n  if (empty($y)) {\r\n    if (empty($z)) {echo $x . \u0022cm \u0022;}\r\n    else {echo $x . \u0022x\u0022 . $z . \u0022cm \u0022;}\r\n  }\r\n  else {\r\n    if (empty($z)) {echo $x . \u0022x\u0022 . $y . \u0022cm \u0022;}\r\n    else {echo $x . \u0022x\u0022 . $y . \u0022x\u0022 . $z . \u0022cm \u0022;}\r\n  }\r\n}\r\n\r\n\/\/ I can\u0027t check this data for empty value\r\necho $power . \u0022W \u0022;\r\necho $weight . \u0022kg \u0022;\r\necho $diameter . \u0022cm \u0022;\r\n?\u003E"
              }
            }
          ]
        }
      }
    ]
  }
}
  • Add rule export to question – Dr.Osd Jun 27 '17 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.