For ecommerce website design call 020 3544 8132

Magento table rates for UK delivery

magento-UK

Set up Magento table rates for UK Delivery

We had been working on a Magento ecommerce website this week which called for the use of Magento table rates for UK delivery.  The client wanted to be able to set different prices for the different postcode areas in the UK.  Our postcode area table is just 127 lines, covering the first 2 characters of the postcode (not the 3000 line table covering every possible postcode area).  Using this short version makes it easy for our client to handle and quick and easy for us to set-up.

The problem is, Magento table rates is set-up to check the postcode exactly against the table rates database. For example, our database has the “CM” postcode area listed, but if a customer enter theirs full UK postcode against this it will not match and therefore they will not be able to select the shipping option.

We have worked out a way around this, changing just one line of code in the Magento code folder.  What’s more, we are giving you our magento tablerates CSV file for free to help you get your magento table rates shipping set up for the UK postcode areas. I know, we are nice, feel free to send us gifts and flowers to the usual office address!Magento table rates for UK

Here is what you need to do:

1) Download our tablerates CSV file here

2) Using FTP locate this file in your magento install – app/code/core/Mage/shipping/Model/resource/carrier/tablerate.php

3) Open this file in your favorite editor and scroll down to line 123

4) Change this code - ‘:postcode’ =>$request->getDestPostcode() to this – ‘:postcode’ => (substr($request->getDestPostcode(),0,2))

5) Upload the updated file

6) Go to your admin panel,  Configuration / Shipping Methods / Table Rates

7) Enable Table Rates and select the file you have downloaded above in the Import box.

8) Press save and your good to go!!

We are using Magento 1.7 for this and it has worked perfectly, I have not tested this on older versions.
I hope this is of some help, so you are not all sat searching for hours trying to find a fix for this problem.
If you have a better way of doing this or improvement on our method, please let us know below….

  • There is a module you can purchase that allows you to break postcode areas down in the way you mention above, I think its only about £50.00, I’ll try and dig out the link for it. We used this one for of our client who needed more control of the areas and it worked a treat. The above is more of a generalized basic option.

  • Thanks for the solution but its not appropriate for matching postcodes of varying lengths, for instance when shipping to Islands off the coast of England, the postcode TR covers parts of Cornwall but also the Isles of Scilly and the same is true with the highlands of Scotland, some areas are more expensive to ship to especially the Orkney and Shetland Isles, yet these places all share the first 2 character postcode prefix with a less expensive mainland area.

    The solution I came up with, I haven’t tested this thoroughly as of yet so it is offered as is:

    Locate:
    app/code/core/Mage/shipping/Model/resource/carrier/Tablerate.php

    (1)
    Find the following line: (Around line 128)
    ->order(array(‘dest_country_id DESC’, ‘dest_region_id DESC’, ‘dest_zip DESC’))
    change to:
    ->order(array(‘dest_country_id DESC’, ‘dest_region_id DESC’, ‘dest_zip DESC’, ‘price DESC’))
    (2)
    Find the following lines: (Around line 132)
    $orWhere = ‘(‘ . implode(‘) OR (‘, array(
    “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 = ””,
    // 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 = ‘*’”,
    “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 = ‘*’”,
    )) . ‘)’;

    **change all “dest_zip=:postcode” to “INSTR(:postcode, dest_zip) = 1″ like so:

    $orWhere = ‘(‘ . implode(‘) OR (‘, array(
    “dest_country_id = :country_id AND dest_region_id = :region_id AND INSTR(:postcode, dest_zip) = 1″,
    “dest_country_id = :country_id AND dest_region_id = :region_id 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 = ‘*’”,
    “dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = ””,
    “dest_country_id = :country_id AND dest_region_id = 0 AND INSTR(:postcode, dest_zip) = 1″,
    “dest_country_id = :country_id AND dest_region_id = 0 AND dest_zip = ‘*’”,
    )) . ‘)’;

    This should now match any partial postcode, as I say I haven’t tested this thoroughly but at the moment it appears to be working as expected. Also worth mentioning you shouldn’t edit any Magento core files, it’s best to create a copy of the files in local and override them.

  • Hi

    The Magento table rates solution that you provide for free is great but is there a solution for when you want to split the postal areas. I’m particularly thinking of the PA range where some are mainland and delivery cost less than the mainland PA areas. We would be looking to make PA20+ the higher shipping rates. Is there a solution for this?

    Thanks, Paul

So what do you think?

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>