Tags/topics: 
7
Answers
Vote up!
0
Vote down!

Filter available shipping type by a long list of zip codes

Hi,

My customer is shipping frozen food, so I have 2 shipping types – Flat Rate for zip codes within 2 days, and UPS air for others within the US.

In the rules for those 2 types, I want to add a condition that will check that the Postal Code "Begins with" [00, 01, 02, etc.] (I have a list of ~120 postal "prefixes" of 2-3 characters, representing 1000s of postal codes from UPS).

It works fine if I add these numbers manually, using "Or" and creating a new condition for each code. But I'd prefer not to have 120 conditions for flat rate, and the same 120 conditions negated for UPS (especially since these codes occasionally change).

Sorry if this is more of a rules question, but is there any way to use multiple values within one condition? I'm not a php expert, but can php be used to insert an array of values that will make this work?

Thanks!

Asked by: endcom
on July 12, 2012

7 Answers

Vote up!
1
Vote down!

Here's how you can do this... To test this I'd start with a new rule that has the action of "Show a message on the site". Once you've verified that it's doing proper text comparisons you can move the conditions into the Flat Rate rule.

Events
Collecting shipping rates for an order

Conditions
Entity has field: commerce_order
Value: commerce_customer_shipping

Entity has field: commerce-order:commerce-customer-shipping
Value: commerce_customer_address

Text Comparison: commerce-order:commerce-customer-shipping:commerce-customer-address:postal-code
Value: ^[01] ( for postal codes starting with 0 or 1)
Comparison Operation: Regular Expression

Hopefully this helps!

Andy Giles
Answer by: Andy Giles
Posted: Jul 16, 2012
Vote up!
1
Vote down!

Hi,

Without making code, you could create a condition component with a regular expression check against all the zip codes (available in the text comparaison condition)
You could then call it from the 2 shipping rules (by negating the condition on the UPS one).
You would have only one list to manage (the regular expression).

Answer by: magalie
Posted: Jul 16, 2012

Comments

Thank you so much for your help, magalie. Though I don't quite understand yet.

So in the rule, would I have two conditions – the first a text comparison and the second an order address comparison? Then I would reference the text comparison with the order address comparison?

I tried to set up a text comparison condition, but can't figure out which data selector to use. Once that's set up, I'm not sure how I would reference those text values in the order address comparison.

Thanks again so much for your help, and sorry if I'm muddying the waters – this sounds like the way to go.

- endcom on July 16, 2012

Thanks so much! This is very helpful. I don't totally understand how to do this, but I understand the concept, and this is just the direction I needed to figure it out.

- endcom on July 16, 2012
Vote up!
0
Vote down!

Thanks magalie!

I think I understand the concept, but I'm having trouble figuring out:

1) What Data selector to use for the text comparison, and
2) how to reference that component (text comparison) with the shipping rule

Could you possible elaborate on this? Thanks again – this sounds like an ideal solution.

Answer by: endcom
Posted: Jul 16, 2012
Vote up!
0
Vote down!

Yes!
andyg5000, this worked perfectly. I am very much in your debt.

To clarify, since the zip code prefixes I had were "130,131,132,14," etc., the regular expression used is

^130|131|132|14|…

You have no idea how much time you just saved me. Thanks again to you and magalie for your very generous help.

Answer by: endcom
Posted: Jul 17, 2012
Vote up!
0
Vote down!

A minor correction – "^" wasn't working right in the regex (I'm a total noob with these, so may just have the syntax wrong). "^130" would validate 13099, but also 99130.

I replaced "130" with "130[0-9][0-9], so the "Matching text" value in the Text comparison condition was:

130[0-9][0-9]|131[0-9][0-9]|132[0-9][0-9], etc.

Then it worked perfectly.

Answer by: endcom
Posted: Jul 17, 2012
Vote up!
0
Vote down!

Hi,
Sorry - I'm not getting this - can someone help me out?

I have 3000 postcodes I need to split into 10 dilivery zones based on mileage. I have them all listed in a spreadsheet - but I'm struggling to add the correct condition to rules.

EG

I want my free shipping selected for 'EX11' and 'TA3'.

So I tried adding a condition based on order address, but can't seem to add multiples.

Then I tried instead adding a text comparison, but cannot find anything which references the customer postcode element of the address.

Many thanks for your help, Joe

Answer by: joecanti
Posted: Sep 5, 2013
Vote up!
0
Vote down!

Ok so it's working for me after following this:

https://drupal.org/node/1976480

The problem is that when someoe has a postcode that starts BS1 ..., they also get the shipping rate for BS12 ... Still, thats just with the British postcode system.

I ave nearly 2000 postcodes added, and the majority work great.

Cheers, Joe

Answer by: joecanti
Posted: Sep 7, 2013