5
Answers
Vote up!
0
Vote down!

How can I compare against a shipping rate in Rules?

I have created a Flat Rate Service with a base rate of 0 €. Using Rules I have created a complex shipping rate that compares some values from the shipping Address Fields.

I have some situations where my Rules are not able to calculate the shipping rate because I have a lot of combinations, so sometimes I the rate remains 0 €. This situation is correct, but I need to explain to the user that we are not able to calculate the shipping cost in this case and I would like to show him a text message.

Hence the question: How may I create a Rules condition to compare against the shipping rate? With rate calculation rules I can modify the shipping rate, but I don't see how to create a condition based on the final rate to show a text message.

Asked by: andrea.brogi
on February 2, 2013

5 Answers

Vote up!
3
Vote down!

It seems like you have a couple of questions wrapped up in here - the one from the title being how to compare against a shipping rate in Rules, but the one unasked being how to present a message to the customer based on the shipping rate amount.

The Shipping modules provides a condition to check and see if a shipping line item exists on an order. Unfortunately, because it's a condition, we cannot automatically make that line item available as a parameter to you for any follow-up conditions.

If I were going to build this entirely through Rules, I'd use that condition to ensure a line item existed, use an action to load the shipping line item that corresponds to the order, and then use a Rules component to look at the unit price of the shipping line item and display the message when appropriate. If you haven't used components, look for tutorials in the Rules Mastery video course on YouTube.

The problem is that because of the way the checkout form and rate calculation works, your message may not show up where you expect it. If I had this same requirement, I'd abandon this approach entirely and either:

  1. Use hook_form_alter() to hook into the checkout form and look at the shipping line items generated during shipping rate calculation. If I saw a 0 € rate, I'd just add a bit of help text to the checkout pane indicating what went on.
  2. If code simply isn't an option, I'd probably just have it not return a rate (i.e. set the rate to NULL - see the default product pricing rule that invalidates disabled products found in shopping carts for an example). The checkout pane can be configured to display a message saying a rate could not be calculated but inviting the customer to proceed through checkout. Assuming your site is in Italian, you could just use the translation interface to change that English string into an Italian message tailored specifically to your site.

Hope that helps!

Ryan Szrama
Answer by: Ryan Szrama
Posted: Feb 2, 2013
Vote up!
1
Vote down!

Hi Ryan,
thanks for your answer.
I like a lot the approach number 2 - not return a rate
So I have look the Rule to invalidate disabled product and I'have seen that the Rule set the "commerce-line-item:commerce-unit-price:amount" to null.

I have tryed to make the same into my Rule to "Calculate a shipping Rate".
I have two condition in this rule to set it correctly:
1 - Data Comparison
"Parameter: Data to compare: [commerce-line-item:type], Data value: Shipping"
2 - Data Comparison
Parameter: Data to compare: [commerce-line-item:commerce-shipping-service, Data value: My Shipping Service"

And like action I have my complex rule to calculate the shipping cost.

Before my rule I have set the rule to not return a rate.
I have try to set:
"commerce-line-item:commerce-unit-price:flat-rate-my-shipping-service:amount" like NULL"
and also
"commerce-line-item:commerce-total:flat-rate-my-shipping-service:amount" like "NULL"
but every time I have a value = 0€

I'm making something wrong but I don't understand where.

Answer by: andrea.brogi
Posted: Feb 4, 2013
Vote up!
0
Vote down!

I have find the correct value but the message (like Ryan have said) compare at the beginning of calculation cost and disappear when the calculation phase is finish if the value is 0€.

This isn't the correct way

Answer by: andrea.brogi
Posted: Feb 8, 2013
Vote up!
0
Vote down!

I have made an error. This the correct test.

.... and disappear when the calculation is finish if the value is different from 0€

Answer by: andrea.brogi
Posted: Feb 8, 2013
Vote up!
-1
Vote down!

Ok, I've solved.
Using in my condition the data comparison 1 and 2 (see my upper post) inside the "Calculate the shipping rate" I have inside only the line item of shipping.
So if I read "commerce-line-item:commerce-unit-price:amount" I find the shipping cost value.
Now I'm able to compare it (with conditional rules inside action - http://drupal.org/project/rules_conditional) and show a message when the value is 0.00 €.

I have not found a way to set the flat rate price like "NULL"

Answer by: andrea.brogi
Posted: Feb 8, 2013