Question

we restricted only some zip codes for default COD payment method by adding below code in cashondelivery.php :

app/code/local/Mage/Payment/Model/Method/Cashondelivery.php

public function isAvailable($quote = null)
{
if ($quote) {

// Here is the list of restricted Zip Codes
$restrictedZips = array(
'641004'
,'641006'
);

$address = $quote->isVirtual() ? $quote->getBillingAddress() : $quote->getShippingAddress();
$customerZip = $address->getPostcode();
if (!in_array($customerZip, $restrictedZips)) {
return false;
}
}

return parent::isAvailable($quote);
}

Now we are using custom COD & we are using following code :

public function isAvailable($quote = null)
    {
        if($quote && $quote->getBaseGrandTotal()<Mage::getStoreConfig('payment/mpcashondelivery/max_order_total') && $quote->getBaseGrandTotal()>Mage::getStoreConfig('payment/mpcashondelivery/max_order_total')) {
            return false;
        }
        $specificcountry = explode(",",Mage::getStoreConfig('payment/mpcashondelivery/specificcountry'));
        if(Mage::getStoreConfig('payment/mpcashondelivery/allowspecific')!=0 ){
            if(!in_array($quote->getBillingAddress()->getCountry(),$specificcountry)){
                return false;
            }
        }
        $cod_charges = Mage::getModel('mpcashondelivery/pricerules')->getAppliedPriceRules();
        if($cod_charges['error']){            
            return false;
        }
        return parent::isAvailable($quote);
    }

How to restrict only some zip codes for this custom COD ?

Was it helpful?

Solution

add code like below will work for you

if($quote && $quote->getBaseGrandTotal()<Mage::getStoreConfig('payment/mpcashondelivery/max_order_total') && $quote->getBaseGrandTotal()>Mage::getStoreConfig('payment/mpcashondelivery/max_order_total')) {
            return false;
        }
        $specificcountry = explode(",",Mage::getStoreConfig('payment/mpcashondelivery/specificcountry'));
        if(Mage::getStoreConfig('payment/mpcashondelivery/allowspecific')!=0 ){
            if(!in_array($quote->getBillingAddress()->getCountry(),$specificcountry)){
                return false;
            }
        }
         $cod_charges = Mage::getModel('mpcashondelivery/pricerules')->getAppliedPriceRules();
        if($cod_charges['error']){            
            return false;
        }
       $restrictedZips = array(
           '641004'
          ,'641006'
             );

         $address = $quote->isVirtual() ? $quote->getBillingAddress() : $quote->getShippingAddress();
          $customerZip = $address->getPostcode();
      if (!in_array($customerZip, $restrictedZips)) {
         return false;
       }
        }

OTHER TIPS

At magento,if you return value as false (return false;) from isAvailable() from a payment method class then that payment gateway becomes not available for current checkout.

So if you add your code at start of isAvailable(), then will works for you.

public function isAvailable($quote = null)
    {
    if($quote){
        $restrictedZips = array(
        '641004'
        ,'641006'
        );

    $address = $quote->isVirtual() ? $quote->getBillingAddress() : $quote->getShippingAddress();
    $customerZip = $address->getPostcode();
        if (!in_array($customerZip, $restrictedZips)) {
            return false;
            }
        }
        /* add rest of code  */
        *****       
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top