{ "rules_calculate_wristband_redemption" : { "LABEL" : "Calculate Wristband Voucher Redemption", "PLUGIN" : "reaction rule", "REQUIRES" : [ "rules", "commerce_order", "php", "commerce_coupon" ], "ON" : [ "commerce_coupon_redeem" ], "IF" : [ { "entity_has_field" : { "entity" : [ "coupon" ], "field" : "commerce_coupon_fixed_amount" } }, { "NOT AND" : [ { "data_is_empty" : { "data" : [ "coupon:commerce-coupon-fixed-amount" ] } } ] }, { "data_is" : { "data" : [ "coupon:commerce-coupon-fixed-amount:amount" ], "op" : "\u003e", "value" : 0 } }, { "commerce_order_contains_product" : { "commerce_order" : [ "commerce_order" ], "product_id" : "wrist", "operator" : "\u003e=", "value" : "1" } }, { "data_is" : { "data" : [ "coupon:type" ], "value" : "discounted_wristband" } } ], "DO" : [ { "data_calc" : { "USING" : { "input_1" : [ "coupon:commerce-coupon-fixed-amount:amount" ], "op" : "*", "input_2" : { "select" : "coupon:coupon-id", "php" : { "code" : "global $user;\r\n$order = commerce_cart_order_load($user-\u003euid);\r\n$quantity = 0;\r\n\r\nif ($order) {\r\n $order_wrapper = entity_metadata_wrapper('commerce_order', $order);\r\n foreach ($order_wrapper-\u003ecommerce_line_items as $line_item_wrapper) {\r\n if ($line_item_wrapper-\u003etype-\u003evalue() == 'product') {\r\n if ($line_item_wrapper-\u003ecommerce_product-\u003esku-\u003evalue() == 'wrist') {\r\n $quantity += $line_item_wrapper-\u003equantity-\u003evalue();\r\n }\r\n }\r\n }\r\n}\r\nreturn $quantity;\r\n" } } }, "PROVIDE" : { "result" : { "total_wrist" : "Total Wristband Discount" } } } }, { "commerce_coupon_action_create_coupon_line_item" : { "USING" : { "commerce_coupon" : [ "coupon" ], "commerce_order" : [ "commerce-order" ], "amount" : [ "total-wrist" ], "currency_code" : [ "coupon:commerce-coupon-fixed-amount:currency-code" ] }, "PROVIDE" : { "commerce_coupon_line_item" : { "commerce_coupon_line_item" : "commerce coupon line item" } } } }, { "commerce_coupon_action_set_granted_amount" : { "commerce_coupon_log" : [ "coupon-log" ], "amount" : [ "total-wrist" ], "currency_code" : [ "coupon:commerce-coupon-fixed-amount:currency-code" ] } } ] } }