3

I'm still a novice at Rules. I'm using Drupal Commerce and have the Flat Rate shipping service enabled. I'm using Rules to calculate a surcharge to the flat rate based on the number of items in the shopping cart. So far this rule works.

However, I now want to modify the rule so that it only calculates the surcharge based on the number of products with a specific SKU. All other line items in the cart, which may be other products, should be ignored for the purposes of this calculation.

If I was just coding this, I would put an if statement in the loop and look at whether the current line item has a specific SKU. But I totally don't understand how to do that with Rules.

Thanks for any help.

Here is my current rule:

{ "rules_createspace_flat_rate_calculator_expedited_" : {
    "LABEL" : "CreateSpace Flat Rate Calculator (Expedited - US)",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "commerce_order", "commerce_line_item",     "commerce_shipping" ],
    "ON" : { "commerce_shipping_calculate_rate" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "commerce-line-item:type" ], "value"  : "shipping" } },
      { "data_is" : {
          "data" : [ "commerce-line-item:commerce-shipping-service" ],
          "value" : "ship_manuals_to_you_expedited_"
        }
      },
      { "commerce_order_compare_address" : {
          "commerce_order" : [ "commerce-line-item:order" ],
          "address_field" :     "commerce_customer_shipping|commerce_customer_address",
          "address_component" : "country",
          "value" : "US"
        }
      }
    ],
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "commerce-line-item:order:commerce-line-items" ] },
          "ITEM" : { "order_line_items" : "Order line items" },
          "DO" : [
            { "data_calc" : {
                "USING" : {
                  "input_1" : [ "order-line-items:quantity" ],
                  "op" : "*",
                  "input_2" : "250"
                },
                "PROVIDE" : { "result" : { "shipping_volume_surcharge" : "Shipping Volume Surcharge" } }
              }
            },
            { "commerce_line_item_unit_price_add" : {
                "commerce_line_item" : [ "commerce_line_item" ],
                "amount" : [ "shipping-volume-surcharge" ],
                "component_name" :    "flat_rate_ship_manuals_to_you_expedited_",
                "round_mode" : "1"
              }
            }
          ]
        }
      },
      { "commerce_line_item_unit_price_add" : {
          "commerce_line_item" : [ "commerce_line_item" ],
          "amount" : "1000",
          "component_name" : "flat_rate_ship_manuals_to_you_expedited_",
          "round_mode" : "1"
        }
      }
    ]
  }
}

1 Answer 1

2

Your real challenge appears to be this part of your question:

... put an if statement in the loop and look at whether the current line item has a specific SKU ...

Here is how to get that to work:

Easy, no?

PS: I'm assuming this question is about D7 ...

4
  • Thanks. I will take a look at Conditional Rules. Honestly I find Rules to be incredibly challenging. I guess I just haven't spent enough time with them. Commented Feb 2, 2017 at 7:43
  • 1
    @richardsplayground merci for the feedback! Don't forget to let us know how it turned out (I bet you will get it to work with that). About "challenging": I'd rather say "amazing", though it takes a while to understand its full power (I'm still learning / discovering things about Rules too). Are you familiar with "the" typical Rules tutorials you MUST have seen? Commented Feb 2, 2017 at 7:52
  • Hi. Yeah...I totally agree that they look amazing. It's definitely my own lack of spending time with Rules. I haven't given any tutorials some time so if you have pointers to some good ones I'd be grateful. And for sure I will post my solution here. Merci beaucoup! Commented Feb 2, 2017 at 16:13
  • Hi just to update everyone. Using the Conditional Rules module was absolutely the key. Once the module was enabled, it became very easy in the Rules editor GUI to add an if condition, select the condition "Line item is a specific product" and then provide the SKU of the product. It also was easy in the Rules editor GUI to then pull my calculation rules underneath the if statement so that they would only fire if the line item was of that SKU. Thanks so much for the help! Commented Feb 3, 2017 at 2:27

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.