Skip to content

Commit

Permalink
New feature: added partial postcode match to table rate shipping (#1504)
Browse files Browse the repository at this point in the history
Co-authored-by: Fabrizio Balliano <[email protected]>
  • Loading branch information
AlterWeb and fballiano authored Feb 21, 2024
1 parent 1834708 commit ef34421
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions app/code/core/Mage/Shipping/Model/Resource/Carrier/Tablerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,38 @@ public function getRate(Mage_Shipping_Model_Rate_Request $request)
$select = $adapter->select()
->from($this->getMainTable())
->where('website_id = :website_id')
->order(['dest_country_id DESC', 'dest_region_id DESC', 'dest_zip DESC', 'condition_value DESC'])
->order(['dest_country_id DESC', 'dest_region_id DESC', 'LENGTH(dest_zip) DESC', 'dest_zip DESC', 'condition_value DESC'])
->limit(1);

// Render destination condition
$orWhere = '(' . implode(') OR (', [
$conditions = [
"dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = :postcode",
"dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = ''",
"dest_country_id = :country_id AND dest_region_id = '0' AND dest_zip = :postcode",
"dest_country_id = '0' AND dest_region_id = :region_id AND dest_zip = :postcode",
"dest_country_id = '0' AND dest_region_id = '0' AND dest_zip = :postcode",
"dest_country_id = :country_id AND dest_region_id = '0' AND dest_zip = ''"
];

// Handle asterix in dest_zip field
"dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = '*'",
"dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = '*'",
"dest_country_id = '0' AND dest_region_id = :region_id AND dest_zip = '*'",
"dest_country_id = '0' AND dest_region_id = 0 AND dest_zip = '*'",
// Handle asterix in dest_zip field
$conditions[] = "dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = '*'";
$conditions[] = "dest_country_id = :country_id AND dest_region_id = '0' AND dest_zip = '*'";
$conditions[] = "dest_country_id = '0' AND dest_region_id = :region_id AND dest_zip = '*'";
$conditions[] = "dest_country_id = '0' AND dest_region_id = '0' AND dest_zip = '*'";

$i = 0;
$postcode = $request->getDestPostcode();
while (strlen($postcode) > 1) {
$i++;
$postcode = substr($postcode, 0, -1);
$bind[':wildcard_postcode_' . $i] = "{$postcode}*";
$conditions[] = "dest_country_id = :country_id AND dest_region_id = :region_id AND dest_zip = :wildcard_postcode_{$i}";
$conditions[] = "dest_country_id = :country_id AND dest_region_id = '0' AND dest_zip = :wildcard_postcode_{$i}";
$conditions[] = "dest_country_id = '0' AND dest_region_id = :region_id AND dest_zip = :wildcard_postcode_{$i}";
$conditions[] = "dest_country_id = '0' AND dest_region_id = '0' AND dest_zip = :wildcard_postcode_{$i}";
}

"dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = ''",
"dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = :postcode",
"dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = '*'",
]) . ')';
// Render destination condition
$orWhere = '(' . implode(') OR (', $conditions) . ')';
$select->where($orWhere);

// Render condition by condition name
Expand Down

0 comments on commit ef34421

Please sign in to comment.