1 Comment

Store zip codes in backend instead of file

Extension is not having new admin url in link (redirect to dashboard)
Buy 1 X, get up to 10xY for free

we are using an extension for finding shipping charges and we wrote the list of supported zip codes in this file.

what we need is instead of updating zip codes through file, we want to save list of zip codes in database. There should be an box in admin panel, what zip codes we enter in that box, only those zip codes should support for placing an order.
enter image description here

class module_Mpperproductshipping_Model_Carrier_LocalDelivery extends Mage_Shipping_Model_Carrier_Abstract
    /*  Use group alias */
    protected $_code = 'mpperproductshipping';

    public function collectRates(Mage_Shipping_Model_Rate_Request $request){        
    $postCode = $request->getDestPostcode();
    $restrictedCodes = array(


); //restricted values. they can come from anywhere
    if (!in_array($postCode, $restrictedCodes)) { 
         return false;


        $result = Mage::getModel('shipping/rate_result');

        /* Edited by vikas_mage */
        /* End Editing by vikas_mage */

        $postcode=str_replace('-', '', $postcode);

        /*  one start */
       /*  one end  */

         /* tt start - ship charges never work  
         tt end */

        foreach($items as $item) {
            if(!$mpassignproductId) {
                foreach($item->getOptions() as $option) {
                    if($temp['mpassignproduct_id']) {
            if($mpassignproductId) {
                $mpassignModel = Mage::getModel('mpassignproduct/mpassignproduct')->load($mpassignproductId);
                $partner = $mpassignModel->getSellerId();
            } else {
                foreach($collection as $temp) {

                foreach($shippingdetail as $itemship){

            return $result;
        /*store shipping in session*/

        $method = Mage::getModel('shipping/rate_result_method');
        /* Use method name */
        return $result; 

public function getShippingPricedetail($shippingdetail,$shippostaldetail) {
        $session = Mage::getSingleton('checkout/session');
        $customerAddress = $session->getQuote()->getShippingAddress();

/* Edited by vikas_boy */
$customerPostCode = $shippostaldetail['postalcode'];
$items = $shippostaldetail['items'];
 /* End Editing by vikas_boy  */

 /* one  */

        foreach($shippingdetail as $shipdetail) {
            $seller = Mage::getModel("customer/customer")->load($shipdetail['seller_id']);
            $sellerAddress = $seller->getPrimaryShippingAddress();
            $distance = $this->getDistanse($sellerAddress->getPostcode(),$customerPostCode);
            // echo "distance ".$distance;die;
            $price = 0;
            foreach($items as $item) {
                if(!$mpassignproductId) {
                    foreach($item->getOptions() as $option) {
                        if($temp['mpassignproduct_id']) {
                 if ($item->getHasChildren()){

                $mpshippingcharge = 0;
                $localDistance = Mage::getStoreConfig('marketplace/mpperproductshipping/local_shipping_distance');
                $regionalDistance = Mage::getStoreConfig('marketplace/mpperproductshipping/regional_shipping_distance');
                $stateDistance = Mage::getStoreConfig('marketplace/mpperproductshipping/state_shipping_distance');
                if(in_array($item->getId(),$itemsarray)) {
                    if($mpassignproductId) {
                        if($distance < $localDistance) {
                        } elseif($distance > $localDistance && $distance < $regionalDistance) {
                        } elseif($distance > $regionalDistance) {
                    } else {
                         // echo "imte ".$item->getProductId();
                        if($distance < $localDistance) {
                            // echo "imte ".$item->getProductId();
                            // echo "ship ".$mpshippingcharge;
                        } elseif($distance > $localDistance && $distance < $regionalDistance) {
                        } elseif($distance > $regionalDistance) {

                    /* tt */
                    // echo "test ".$mpshippingcharge;die;
                        $price=$price+floatval($this->getConfigData('defalt_ship_amount')* floatval($item->getQty()));
                        $price=$price+($mpshippingcharge * floatval($item->getQty()));
                    /* tt end */

                    /* one
                      one end */

            $handling = $handling+$price;
            $submethod = array(array('method'=>Mage::getStoreConfig('carriers/'.$this->_code.'/title'),'cost'=>$price,'error'=>0));
        return array('handlingfee'=>$handling,'shippinginfo'=>$shippinginfo,'errormsg'=>$msg);

 /* one end */

    /* tt start */

    private function getDistanse($origin,$destination) {
        $url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=".$origin.",india&destinations=".$destination.",india&mode=driving&language=en-EN&sensor=false";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $response = curl_exec($ch);
        $response_all = json_decode($response);
        $distance = $response_all->rows[0]->elements[0]->distance->value / 1000;
            $zips = array(
                // ... etc ...

            $geocoded = array();
            $serviceUrl = "http://maps.googleapis.com/maps/api/geocode/json?components=postal_code:%s&sensor=false";
            $curl = curl_init();
            foreach ($zips as $zip) {
                curl_setopt($curl, CURLOPT_URL, sprintf($serviceUrl, urlencode($zip)));
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
                $data = json_decode(curl_exec($curl));
                $info = curl_getinfo($curl);
                if ($info['http_code'] != 200) {
                    // Request failed
                } else if ($data->status !== 'OK') {
                    // Something happened, or there are no results
                } else {
                    $geocoded[$zip] =$data->results[0]->geometry->location;

        return $distance;

    /* tt end * /

/* tt start  */

public function DistAB($lat_a,$lon_a,$lat_b,$lon_b)


        $measure_unit = 'kilometers';

        $measure_state = false;

        $measure = 0;

        $error = '';

          $delta_lat = $lat_b - $lat_a ;
          $delta_lon = $lon_b - $lon_a ;
          $earth_radius = 6372.795477598;

          $alpha    = $delta_lat/2;
          $beta     = $delta_lon/2;
          $a        = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($this->lat_a)) * cos(deg2rad($this->lat_b)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ;
          $c        = asin(min(1, sqrt($a)));
          $distance = 2*$earth_radius * $c;
          $distance = round($distance, 4);

         $measure = $distance;
         return $measure;



Edit :

I created a module that can save all zip codes in database if we entered through backend. Now How i can link this shipping method to database ?

Related Posts

  • Shipping charges working in APP but not working according to the distance (1)
    After I used this answer : Shipping charges are not working now i am getting shipping charges in App. But we are facing some small issue now: Based on distance, we have set the shipping price, Shipping A : less than 100 kms = 50 $ , shipping B : 100 to 200 kms 100 $ […]
  • Send mail with Shopping Cart Items (1)
    I used Below code for send mail if customer enter Unsupport pincode. It's working fine: <?php class Webkul_Mpperproductshipping_Model_Carrier_LocalDelivery extends Mage_Shipping_Model_Carrier_Abstract { /* Use group alias */ protected $_code = 'mpperproductshipping'; […]
  • Validation required for zip codes less than 6 digits (1)
    we have custom module for entering zip codes in backend, only if those zip codes if we enter in shipping address, than it will display COD under payment method , otherwise it will hide COD. Here problem is it is working only for 6 digits. if we enter any below 6 digit zip codes […]
  • update zip codes through backend (1)
    we are using a module that allow me to restrict only some zip codes for default "cash on delivery" payment method. module observer.php <?php class Mfp_Cod_Model_Observer { public function getCashOnDelvery(Varien_Event_Observer $observer) { $event = […]
  • Restrict customer in checkout if zip codes are not from predefined zip code list (1)
    we are restricting the zip codes for custom shipping method.... for now we are facing problem as here : Restrcit zip codes for custom shipping method is not working after hide the shipping method so is there any other way we can allow only some zip codes and restrict other zip […]
  • How to prevent orders being placed based on their shipping address (1)
    We're experiencing a high volume of fraud orders with the same city as the shipping address. If we could block the city, or a list of zip codes we will avoid all this issues. Is there a way to prevent orders being placed, based on the value of the shipping address?
  • Bundle product by category (0)
    I really like the bundle functionality, but there is one thing that would be useful, and I'm wondering if it's possible out of the box, or by extension. Being able to specify a whole group as an option, whether they be a menu, checkbox or radio list, instead of choosing products […]
  • Magento how to add new select box in product view toolbar section without change .phtml file (1)
    Hi i have a toolbar like below image. I want to add new "Show" select box like below image. For this i have added select box code directly into the toolbar.phtml file. But I don't like to update any existing phtml code. I plan to develop like a extension. So Is any way to do […]
  • checkout onepage controller error (2)
    I have created a module to remove the shipping method from the checkout page.. after doing everything i have got this error preParse error: syntax error, unexpected 'public'(T_PUBLIC)inhtdocsappcodelocalPaypalextendedCheckoutcontrollersOnepageController.phpon line 381 these are […]
  • Magento 2 category sidebar configuration (1)
    I'm using magento 2 and I would like to configure my sidebar from the category page , for example , now in my sidebar I have also Compare products and My whish list , and I would like to get rid of them from there. There is any option in the admin panel to do that , or which is the […]
Extension is not having new admin url in link (redirect to dashboard)
Buy 1 X, get up to 10xY for free

1 Comment on this article

  • User November 12, 2016

    The question you should be asking is how to create an extension that allows you save and maintain a list of zipcodes via the admin panel. Once you accomplish this, you can retrieve data from the DB and use it in your module_Mpperproductshipping_Model_Carrier_LocalDelivery.

    Essentially, you need to know how to…

    1. Create a general extension (Google it).
    2. Create a model and resource model to interact with the data. Example
    3. Set up an admin interface (blocks). Example

    Once you learn how to these, you can save a list of zipcode in the DB and use them how you like.


Add a comment

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax