I am looking for a solution to modify Magento 1.9.3 in order to calculate the shipping cost according to City when checkout. Could anyone give me some pointers?

  • Which shipping method used ? – Suresh Chikani Nov 10 '16 at 17:58
  • It depends on shipping method you use – Gopal Patel Nov 10 '16 at 18:00
  • are you using onepage checkout ? – Ahsan Horani Nov 11 '16 at 6:13

UPS and FedEx shipping methods both calculate the cost of the shipping based on the zip code.

If you are using flat rate or matrix shipping, you would need to calculate this yourself.


You can use event/observers approach for this.

  1. Create a hook on the event sales_quote_address_collect_totals_after e.g.

  2. Below function is the example to set shipping price based on any logic you apply, write this function in your observer model class:

    public function setShippingCost($observer)
        $events = $observer->getEvent();
        $address = $events->getQuoteAddress();
        if ($address->getAddressType() == 'shipping') {
            //Here you can get all shipping information on checkout 
            $shippingInfo = $events->getQuoteAddress()->getData();
            $price = 0;
            // Find if our shipping has been included.
            $rates = $address->collectShippingRates()
            foreach ($rates as $carrier) {
                foreach ($carrier as $rate) {
                    // print_r($rate->getPrice());

You can use default shipping method "Table Rate" for this.

In this shipping method you can import CSV file for that zip codes. and set your rates and shipping label there.


You can use web shop matrix shipping method for this. Both are same in functionality of working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.