1

I am using two rules. The first one works like that:

  1. 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

  2. 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.

enter image description here

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!

2
  • 1
    looks like your second rule should be firing on "Product added to cart" event and not on [ "commerce_line_item_view" + "commerce_product_calculate_sell_price"] . Try that.
    – oksana-c
    Commented Jan 18, 2016 at 22:58
  • @joshmiller I tried that but it didn't work. I don't think the "Product added to cart" event is relevant to the second rule, which only checks if a product exists in the cart whenever the price for product-A2 is calculated. Commented Jan 18, 2016 at 23:45

1 Answer 1

1

What's your rule component for checking if the PDF has been added to the cart? Are you sure that the data is getting set properly? You can use the Display a system message to ensure that the data values are getting set.

Rules is a bit picky when it comes to passing data between the various components. If you take a look at my answer here: Exclude Items from Flat Rate Shipping Calculation you'll see that I'm actually not using the dual purpose variables, but instead setting new values.

Also, this is unnecessary as it will get reset during the next order refresh anyway:

  { "data_set" : {
      "data" : [ "commerce-line-item:order:commerce-order-total:amount" ],
      "value" : [ "commerce-line-item:order:commerce-order-total:amount" ]
    }
  }
4
  • @nvahalic "Also, this is unnecessary as it will get reset during the next order refresh anyway". Yes, you are right, its is redundant too, but it was a desperate effort to "force" the correct total into the blocks or whatever. I have tried system messages and everything seems fine! The correct prices are returned. Is it possible that this has to do with the bulid time of the views and that the rules are evaluated afterwards or sth? And why is it that after a refresh or another page load everything is returned as they should be? Commented Jan 19, 2016 at 16:17
  • Have you disabled the "when a product is viewed" event from the second rule?
    – nvahalik
    Commented Jan 19, 2016 at 16:57
  • You mean the "commerce_line_item_view" event right (there is no "when a product is viewed" event)? I deleted it and tried again. The same thing. I also tried setting the unit price of the line item after adding it to the cart through the first rule, but still the cart blocks show the original price. Commented Jan 19, 2016 at 21:18
  • @nvahalic I added an edit which I believe is a step towards the solving of this. Please, take a look and let me know your thoughts. Commented Jan 19, 2016 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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