Tags/topics: 
2
Answers
Vote up!
1
Vote down!

Multiply by amount is not working

Hello I created this simple rule, just to test if this works, my actual rule is more complicated and that is why I need multiply by some amount.

{ "rules_test" : {
    "LABEL" : "test",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "commerce_line_item", "commerce_cart" ],
    "ON" : [ "commerce_cart_product_add" ],
    "DO" : [
      { "commerce_line_item_unit_price_divide" : {
          "commerce_line_item" : [ "commerce_line_item" ],
          "amount" : "50",
          "component_name" : "base_price",
          "round_mode" : "1"
        }
      }
    ]
  }
}

And this doesn't work :(
Any suggestions?

Asked by: Banzai
on May 27, 2013

2 Answers

Vote up!
1
Vote down!

Basically, the answer is in the content of the link provided by @realskorpion. However, a bit of experimentation is required to achieve what you really want to do.

I created a test rule that multiplies the commerce_product:price by .5, and then modifies the line_item:price with that value, using the "Calculating the sell price of a product" event.

A different case is discussed here http://drupal.stackexchange.com/questions/11469/commerce-rules-multiply-... but the understanding it provides, serves for creating other types of rules for other needs.

My rules screen looks like: http://i.imgur.com/N5WFf2c.png

And this is the export:

{ "rules_test" : {
    "LABEL" : "test",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_product_reference" ],
    "ON" : [ "commerce_product_calculate_sell_price" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "commerce-line-item" ], "field" : "commerce_product" } }
    ],
    "DO" : [
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "commerce-line-item:commerce-product:commerce-price:amount" ],
            "op" : "*",
            "input_2" : ".5"
          },
          "PROVIDE" : { "result" : { "super_result" : "Calculation result" } }
        }
      },
      { "data_set" : {
          "data" : [ "commerce-line-item:commerce-unit-price:amount" ],
          "value" : [ "super-result" ]
        }
      }
    ]
  }
}
Answer by: Favio Manriquez
Posted: May 28, 2013

Comments

Missed the part about updating the order total, so here's a slightly modified rule to actually Save the updated line item price

{ "rules_test" : {
    "LABEL" : "test",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_line_item", "commerce_product_reference" ],
    "ON" : [ "commerce_product_calculate_sell_price" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "commerce-line-item" ], "field" : "commerce_product" } }
    ],
    "DO" : [
      { "data_calc" : {
          "USING" : {
            "input_1" : [ "commerce-line-item:commerce-product:commerce-price:amount" ],
            "op" : "*",
            "input_2" : ".5"
          },
          "PROVIDE" : { "result" : { "super_result" : "Calculation result" } }
        }
      },
      { "commerce_line_item_unit_price_amount" : {
          "commerce_line_item" : [ "commerce_line_item" ],
          "amount" : [ "super-result" ],
          "component_name" : "base_price",
          "round_mode" : "0"
        }
      },
      { "entity_save" : { "data" : [ "commerce-line-item" ] } }
    ]
  }
}
- Favio Manriquez on May 29, 2013

I think I missed the entity save thing, I tested a rule almost identically with this and it didn't work, on the event "calculation the sell price of a product", but on a clean install of Drupal with Commerce installed, it worked, I think I have some other problems.
The rule you provided is working, thanks so much favrik!

- Banzai on June 1, 2013