Skip to content

Instantly share code, notes, and snippets.

@croxton
Created November 27, 2017 10:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save croxton/dd5a6c566d33fe4c1c54d7862d9ff8b2 to your computer and use it in GitHub Desktop.
Save croxton/dd5a6c566d33fe4c1c54d7862d9ff8b2 to your computer and use it in GitHub Desktop.
Expresso Store match specific UK postcodes
Allow shipping rules to accept regex
***************************************************************
First, change the `postcode` column in `exp_store_shipping_rules` to type varchar(255).
./Store/src/Service/ShippingService.php
Change this:
/**
* Match postcode glob patterns, for example 123* or 123?
*/
protected function glob_match($pattern, $subject)
{
// convert glob pattern to regex
$regex = '/^'.str_replace(array('\*', '\?'), array('.*', '.'), preg_quote($pattern, '/')).'$/i';
return (bool) preg_match($regex, $subject);
}
To this:
/**
* HACK: Match UK postcode pattern
*/
protected function glob_match($pattern, $subject)
{
// remove spaces
$subject = str_replace(' ', '', $subject);
// remove last 3 characters
$subject = substr($subject, 0, -3);
// search for a match against our shipping rules
return (bool) preg_match($pattern, $subject);
}
Scottish Islands (£18.50) includes postcodes: HS1-9, IV40-51, IV55-56, KA27-28, KW15-17, PA20, PA41-49, PA60-78, PH42-44, ZE
/^HS[1-9]$|^IV4[0-9]$|^IV5[0156]$|^KA2[7-8]$|^KW1[5-7]$|^PA20$|^PA4[1-9]$|^PA6[0-9]$|^PA7[0-8]$|^PH4[2-4]$|^ZE[1-3]$/i
Scottish Highlands (£12.50) includes postcodes: AB30-38, AB44-56, FK17-99, G83, IV1-28, IV30-39, IV52-54, IV63, KW1-14, PA21-33, PA34-40, PH18-26, PH30, PH31-41, PH49-50. PH15 - 18
/^AB3[0-8]$|^AB4[4-9]$|^AB5[0-6]$|^FK1[7-9]$|^FK[2-9][0-9]$|^G83$|^IV[0-9]$|^IV1[0-9]$|^IV2[0-8]$|^IV3[0-9]$|^IV5[2-4]$|^IV63$|^KW[1-9]$|^KW1[0-4]$|^PA2[1-9]$|^PA3[0-9]$|^PA40$|^PH1[5-9]$|^PH2[0-6]$|^PH3[0-9]$|^PH4[019]$|^PH50$/i
Irish Postcodes (northern £11.60) all postcodes begin with BT
/^BT[1-9][0-9]?$/i
Isle of Man (£18.50) postcodes start IM.
/^IM[1-9]$/i
Isle of Wight (£12.50) postcodes: PO30 to PO41 Inclusive
/^PO3[0-9]$|^PO4[01]$/i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment