0

I am creating products on the fly with Rules, using Drupal Commerce. After user clicks on checkbox near product (additional service to product), I need to "add the service" to cart. Service is always 15% of price of Base product.

I created a rule which creates new products, and add them to cart. This works nicely but I am unable to set up the price field. In Data Selector mode I can not see my calculation field anywhere, not even when calculated value is Integer and price is also integer.

Any help with this? I am posting a Rule here:

{ "rules_grafeon_doplnkov_slu_by_pri_pridan_do_kosika" : {
"LABEL" : "Grafeon - Doplnkov\u00e9 slu\u017eby pri pridan\u00ed do ko\u0161\u00edka",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "commerce_discount_extra", "commerce_cart" ],
"ON" : { "commerce_cart_product_add" : [] },
"IF" : [
  { "entity_has_field" : {
      "entity" : [ "commerce-line-item" ],
      "field" : "field_doplnkove_sluzby_item"
    }
  },
  { "data_is" : {
      "data" : [ "commerce-line-item:field-doplnkove-sluzby-item:0" ],
      "value" : "Service SLA"
    }
  },
  { "commerce_discount_extra_line_item_has_product_type" : {
      "commerce_line_item" : [ "commerce_line_item" ],
      "product_types" : { "value" : { "product" : "product" } }
    }
  }
],
"DO" : [
  { "variable_add" : {
      "USING" : { "type" : "date", "value" : "now" },
      "PROVIDE" : { "variable_added" : { "variable_added" : "Added variable" } }
    }
  },
  { "data_convert" : {
      "USING" : { "type" : "text", "value" : [ "variable-added" ] },
      "PROVIDE" : { "conversion_result" : { "conversion_result" : "Conversion result" } }
    }
  },
  { "entity_query" : {
      "USING" : {
        "type" : "commerce_order",
        "property" : "order_id",
        "value" : [ "commerce-line-item:order-id" ]
      },
      "PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
    }
  },
  { "entity_create" : {
      "USING" : {
        "type" : "commerce_product",
        "param_sku" : "DS-[commerce-line-item:order-id]-[conversion-result:value]",
        "param_type" : "doplnkov_slu_by",
        "param_title" : "Service SLA",
        "param_creator" : [ "site:current-user" ],
        "param_commerce_price" : { "value" : { "amount" : 1000, "currency_code" : "EUR" } },
        "param_commerce_stock" : "1000"
      },
      "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
    }
  },
  { "entity_save" : { "data" : [ "entity-created" ], "immediate" : "1" } },
  { "commerce_cart_product_add_by_sku" : {
      "USING" : {
        "user" : [ "site:current-user" ],
        "sku" : "DS-[commerce-line-item:order-id]-[conversion-result:value]",
        "quantity" : "1",
        "combine" : "0"
      },
      "PROVIDE" : { "product_add_line_item" : { "product_add_line_item" : "Added product line item" } }
    }
  }
]

} }

Any advice how to get the value to Price value? Thanks a lot.

0

Well It looks like I found a way. I needed to save an entity before adding to cart.

If someone will found himself struggling in the same spot, I will leave an exported rule here:

{ "rules_grafeon_doplnkov_slu_by_pri_pridan_do_kosika" : {
    "LABEL" : "Grafeon - Doplnkov\u00e9 slu\u017eby pri pridan\u00ed do ko\u0161\u00edka",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "commerce_discount_extra", "commerce_cart" ],
    "ON" : { "commerce_cart_product_add" : [] },
    "IF" : [
      { "entity_has_field" : {
          "entity" : [ "commerce-line-item" ],
          "field" : "field_doplnkove_sluzby_item"
        }
      },
      { "data_is" : {
          "data" : [ "commerce-line-item:field-doplnkove-sluzby-item:0" ],
          "value" : "Service SLA"
        }
      },
      { "commerce_discount_extra_line_item_has_product_type" : {
          "commerce_line_item" : [ "commerce_line_item" ],
          "product_types" : { "value" : { "product" : "product" } }
        }
      }
    ],
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "date", "value" : "now" },
          "PROVIDE" : { "variable_added" : { "variable_added" : "Added variable" } }
        }
      },
      { "data_convert" : {
          "USING" : { "type" : "text", "value" : [ "variable-added" ] },
          "PROVIDE" : { "conversion_result" : { "conversion_result" : "Conversion result" } }
        }
      },
      { "entity_query" : {
          "USING" : {
            "type" : "commerce_order",
            "property" : "order_id",
            "value" : [ "commerce-line-item:order-id" ]
          },
          "PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
        }
      },
      { "entity_create" : {
          "USING" : {
            "type" : "commerce_product",
            "param_sku" : "DS-[commerce-line-item:order-id]-[conversion-result:value]",
            "param_type" : "doplnkov_slu_by",
            "param_title" : "Service SLA",
            "param_creator" : [ "site:current-user" ],
            "param_commerce_price" : { "value" : { "amount" : 100, "currency_code" : "EUR" } },
            "param_commerce_stock" : "1000"
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      },
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "commerce-line-item:commerce-unit-price:amount" ],
            "op" : "*",
            "input_2" : "0.15"
          },
          "PROVIDE" : { "result" : { "result" : "Calculation result" } }
        }
      },
      { "data_set" : {
          "data" : [ "entity-created:commerce-price:amount" ],
          "value" : [ "result" ]
        }
      },
      { "entity_save" : { "data" : [ "entity-created" ], "immediate" : "1" } },
      { "commerce_cart_product_add_by_sku" : {
          "USING" : {
            "user" : [ "site:current-user" ],
            "sku" : "DS-[commerce-line-item:order-id]-[conversion-result:value]",
            "quantity" : "1",
            "combine" : "0"
          },
          "PROVIDE" : { "product_add_line_item" : { "product_add_line_item" : "Added product line item" } }
        }
      }
    ]
  }
}

Your Answer

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

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