I am using two rules. The first one works like that:
IF product-A1 is added to the cart (through the add to cart form) THEN also add product-A2 to the cart.
The second rule does
IF product-A2 is in cart together with product-A1 THEN set the price of product-A2 to zero.
The events I use for the second rule are:
Commerce Line item is viewed
Calculating the sell price of a product
When I click on the add to cart button with my cart empty, then both products are added to the cart as per the first rule. Also, the price of product-A2 displays as zero in the page I am returned (the product listing page from where I clicked the add to cart button), as per the second rule. However, the shopping_cart block (and generally any shopping cart block) displays product-A2 with its original price. If I then refresh the page, or move to another page, eg the shopping cart form, the price becomes zero in those blocks as well.
In the above screenshot, both the shopping_cart block (provided by the kickstart_commerce_product_ui module) and the Shopping cart block (provided by the commerce/cart submodule) are set to display at the User Bar Second region of the default kickstart theme.
The product I added manually to the cart was "234234 - Download" (which displays as 234234 (PDF) in the cart block). The 234234 (HTML) product was added automatically as per the first rule above. As you can see, the price of the latter displays as zero in the product listing, yet in the shopping cart blocks it displays as 34.00€.
Here are the exports for the two rules: First rule:
{ "rules_add_html_variation_to_cart_if_pdf_version_is_added" : {
"LABEL" : "Add Online view to cart too if Download is added",
"PLUGIN" : "reaction rule",
"WEIGHT" : "3",
"OWNER" : "rules",
"TAGS" : [ "Cart", "Online Giveaway" ],
"REQUIRES" : [ "commerce_order", "rules", "commerce_cart" ],
"ON" : { "commerce_cart_product_add" : [] },
"IF" : [
{ "commerce_order_contains_product_type" : {
"commerce_order" : [ "commerce_order" ],
"product_type" : { "value" : { "book_product" : "book_product" } },
"operator" : "\u003E=",
"value" : "1"
}
},
{ "list_count_is" : {
"list" : [ "commerce-line-item:order:commerce-line-items" ],
"value" : [ "commerce-line-item:order:commerce-order-quantity" ]
}
}
],
"DO" : [
{ "LOOP" : {
"USING" : { "list" : [ "commerce-order:commerce-line-items" ] },
"ITEM" : { "current_line_item" : "Current line item" },
"DO" : [
{ "component_rules_add_html_variation_to_cart" : { "line_item" : [ "current-line-item" ] } }
]
}
}
]
}
}
Second rule:
{ "rules_set_hmtl_variation_price_to_zero_if_pdf_variation_also_in_" : {
"LABEL" : "Set Online view variation price to zero if pdf variation also in cart",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"TAGS" : [ "Cart", "Online Giveaway" ],
"REQUIRES" : [ "commerce_discount", "rules", "entity", "commerce_product_reference" ],
"ON" : {
"commerce_line_item_view" : [],
"commerce_product_calculate_sell_price" : []
},
"IF" : [
{ "commerce_product_has_type" : {
"commerce_line_item" : [ "commerce_line_item" ],
"type" : "book_product"
}
},
{ "entity_has_field" : { "entity" : [ "commerce-line-item" ], "field" : "commerce_product" } },
{ "entity_has_field" : {
"entity" : [ "commerce-line-item:commerce-product" ],
"field" : "field_book_product_type"
}
},
{ "data_is" : {
"data" : [ "commerce-line-item:commerce-product:field-book-product-type" ],
"value" : "HTML"
}
},
{ "entity_has_field" : {
"entity" : [ "commerce-line-item:commerce-product:field-product-node:0" ],
"field" : "field_product"
}
}
],
"DO" : [
{ "LOOP" : {
"USING" : { "list" : [
"commerce-line-item:commerce-product:field-product-node:0:field-product"
]
},
"ITEM" : { "current_product_variation" : "Current product_variation" },
"DO" : [
{ "component_rules_check_if_pdf_version_also_in_cart" : {
"USING" : {
"order" : [ "commerce-line-item:order" ],
"pdf_product" : [ "current-product-variation" ]
},
"PROVIDE" : { "bool_pdf_included" : { "bool_pdf_included" : "Pdf is included" } }
}
},
{ "component_rules_set_html_variation_price_to_zero" : {
"bool_proceed" : [ "bool-pdf-included" ],
"online_view_html_item" : [ "commerce-line-item" ]
}
}
]
}
},
{ "data_set" : {
"data" : [ "commerce-line-item:order:commerce-order-total:amount" ],
"value" : [ "commerce-line-item:order:commerce-order-total:amount" ]
}
}
]
}
}
And the "component_rules_set_html_variation_price_to_zero":
{ "rules_set_html_variation_price_to_zero" : {
"LABEL" : "(Bool) =\u003E {set_Line-Item_price_to_zero}",
"PLUGIN" : "rule",
"OWNER" : "rules",
"TAGS" : [ "Online Giveaway" ],
"REQUIRES" : [ "rules", "commerce_line_item" ],
"USES VARIABLES" : {
"bool_proceed" : { "label" : "Proceed", "type" : "boolean" },
"online_view_html_item" : { "label" : "Online view line item", "type" : "commerce_line_item" }
},
"IF" : [ { "data_is" : { "data" : [ "bool-proceed" ], "value" : "1" } } ],
"DO" : [
{ "commerce_line_item_unit_price_amount" : {
"commerce_line_item" : [ "online-view-html-item" ],
"amount" : "0",
"component_name" : "base_price",
"round_mode" : "0"
}
}
]
}
}
Am I doing sth wrong? Is there a workaround to this? I originally posted this in drupal.org.
EDIT:
I did a clean install of drupal commerce and imported my rules. The only thing that I had to change to make them work on the clean kickstart installation was to add a select field named field_law_product_type (with options "HTML" and "PDF") to an existing product type (I chose Bags & Cases) and change the two rules to check for this product type instead. I also disabled the commerce_add_to_cart_confirmation module that comes with kickstart. I also added an action to the second rule to display some system message for debugging.
Now, this is really weird. I did some testing and I added the "Download|PDF" variation to the cart through the product's page. After clicking the add to cart button, the product page was reloaded (and as per the 1st rule, the "Online|HTML" variation was added to the cart as well). The cart block showed the two added products, again with the wrong price for the HTML variation. However, the system message used for debugging did not show! Meaning that the rule did not run at all. The events I used to try and trigger the rule were:
Calculating the sell price of a product
Commerce Line item is viewed
Shouldn't at least the first one ("Calculating the sell price of a product") fire while building the cart block in any of the two views provided by kickstart? If I refresh the page it sure does, since the HTML variation is shown with a zero price!