User personal disounts. Cant find out how to create a rule
I`m trying to add personal discount for users. Personal discounts are assigned by administration to the special field in user profile.
For this I`ve added new field to the user profile (called 'field_personal_discount' - text field).
If this field is filled - when user should get a price with discount. But this discount shouldn`t summed with 'commerce_saleprice' (if product hasn`t sale - when we can assign a personal discount).
One more thing - if field 'field_personal_discount' has sign '%' - when its a percentage. In other case - its an amount (which we simplу subtract from price).
But I can`t create a proper rule for this.
I was trying to do it with this:
component:
{ "rules_user_has_personal_discount" : {
"LABEL" : "User has personal discount",
"PLUGIN" : "rule",
"REQUIRES" : [ "rules", "php", "commerce_line_item" ],
"USES VARIABLES" : {
"line_item" : { "label" : "Line item", "type" : "commerce_line_item" },
"user" : { "label" : "User", "type" : "user" }
},
"IF" : [
{ "entity_has_field" : { "entity" : [ "line-item" ], "field" : "commerce_product" } },
{ "entity_has_field" : {
"entity" : [ "line-item:commerce-product" ],
"field" : "field_commerce_saleprice_on_sale"
}
},
{ "entity_has_field" : {
"entity" : [ "line-item:commerce-product" ],
"field" : "field_commerce_saleprice"
}
},
{ "entity_has_field" : { "entity" : [ "user" ], "field" : "field_personal_discount" } },
{ "data_is_empty" : { "data" : [ "line-item:commerce-product:field-commerce-saleprice:amount" ] } },
{ "NOT data_is_empty" : { "data" : [ "user:field-personal-discount" ] } }
],
"DO" : [
{ "commerce_line_item_unit_price_amount" : {
"commerce_line_item" : [ "line-item" ],
"amount" : {
"select" : "line-item:line-item-id",
"php" : { "code" : "if ( isset($value) ) {\r\n\r\n$line_item = commerce_line_item_load($value);\r\n\r\nglobal $user;\r\nif ( $user-\u003Euid != 0 ) {\r\n $user_profile = user_load($user-\u003Euid);\r\n if ( isset($user_profile-\u003Efield_personal_discount[\u0027und\u0027]) ) {\r\n\r\n $price = $line_item-\u003Ecommerce_unit_price[\u0027und\u0027][0][\u0027amount\u0027] \/ 100;\r\n\r\n $sale = $user_profile-\u003Efield_personal_discount[\u0027und\u0027];\r\n if ( isset($sale[0]) \u0026\u0026 !empty($sale[0]) ) {\r\n if ( substr_count($sale[0][\u0027safe_value\u0027], \u0027%\u0027) \u003E 0 ) {\r\n $minus = trim(str_replace(\u0027%\u0027, \u0027\u0027, $sale[0][\u0027safe_value\u0027]));\r\n $new_price = round( $price * ((100 - $minus) \/ 100) );\r\n } else {\r\n $minus = trim($sale[0][\u0027safe_value\u0027]);\r\n $new_price = round($price - $minus);\r\n } \r\n $new_price = $new_price * 100;\r\n return $new_price;\r\n }\r\n }\r\n}\r\n\r\n}" }
},
"component_name" : "discount",
"round_mode" : "1"
}
}
]
}
}
and pricing rule:
{ "rules_personal_sale" : {
"LABEL" : "Personal sale",
"PLUGIN" : "reaction rule",
"ACTIVE" : false,
"REQUIRES" : [ "rules", "commerce_product_reference" ],
"ON" : [ "commerce_product_calculate_sell_price" ],
"DO" : [
{ "component_rules_user_has_personal_discount" : {
"line_item" : [ "commerce-line-item" ],
"user" : [ "site:current-user" ]
}
}
]
}
}
But its doesn`t work as planned.
What I`m doing wrong? Will be thankful for help, cause I`m out of ideas by now.