0

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           = $observer->getEvent();
        $method          = $event->getMethodInstance();
        $result          = $event->getResult();
        $isModuleEnable = Mage::getStoreConfig('cod/cod/enable');

        if($isModuleEnable) {

            if($method->getCode() == 'cashondelivery' ){

                $quote = Mage::getSingleton('checkout/cart')->getQuote();
                $add = $quote->getShippingAddress();
                $postcode = $add->getData('postcode');

                $comparisonMode = Mage::getStoreConfig('cod/cod/mode');
                $zipCodes = Mage::getStoreConfig('cod/cod/zipcode');
                $isExist = false;

                if(trim($zipCodes) == '') {             
                    $result->isAvailable = true;
                } else {    

                    if(strpos($zipCodes, $postcode) !==  false) {
                        $isExist = true;
                    }


                    if($isExist != true) {

                        $zipCodesArray = explode(',', nl2br($zipCodes));
                        if(count($elementLineArray) > 1) {
                            foreach($zipCodesArray as $codzipLine) {
                                $elementLineArray = explode('-', $codzipLine);
                                if(count($elementLineArray) == 2 && ( $postcode >= $elementLineArray[0] && $postcode <= $elementLineArray[1] )) {
                                    $isExist = true;
                                    break;
                                } else if($postcode == $codzipLine) {
                                    $isExist = true;
                                    break;
                                }
                            }
                        }
                    }

                    $returnValue = '';
                        $returnValue = ($isExist)?true:false;

                    $result->isAvailable = $returnValue;

                }   

            } 
        }   
    }
}

we are using custom COD payment method, what changes we need to done so that it should work for custom COD.

custom paymentmethod.php

app/code/local/Exam/Mpcashondelivery/Model/Paymentmethod.php   

<?php

class Exam_Mpcashondelivery_Model_PaymentMethod extends Mage_Payment_Model_Method_Abstract
{

    /**
     * Payment Method features
     * @var bool
     */
    protected $_code = 'mpcashondelivery';

    /**
     * Cash On Delivery payment block paths
     *
     * @var string
     */
    protected $_formBlockType = 'mpcashondelivery/form';

    protected $_paymentMethod = 'mpcashondelivery';

    protected $_isGateway = false;
    protected $_canAuthorize = true;
    protected $_isInitializeNeeded      = true;
    protected $_canCapture = false;
    protected $_canCapturePartial = false;
    protected $_canRefund = false;
    protected $_canVoid = true;
    protected $_canUseInternal = true;
    protected $_canUseCheckout = true;
    protected $_canUseForMultishipping = true;


    public function initialize($paymentAction, $stateObject)    {
        $data = Mage::app()->getRequest()->getPost();
        if(isset($data['payment']['mpcod_captcha_code'])){
            $mpcod_captcha_code = $data['payment']['mpcod_captcha_code'];
            if(empty($_SESSION['captcha_code']) || strcasecmp($_SESSION['captcha_code'], $mpcod_captcha_code) != 0){
                Mage::throwException(Mage::helper('payment')->__('Invalid Captcha'));
            }else{
                return $msg=1;
            }
        }
    }

    /**
     * Check whether payment method can be used
     *
     * TODO: payment method instance is not supposed to know about quote
     *
     * @param Mage_Sales_Model_Quote|null $quote
     *
     * @return bool
     */

    public function isAvailable($quote = null)
    {


    if($quote){
        $restrictedZips = array(
        '110001'
        ,'560044'
        );


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

        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);
    }
}

Edit

we uploaded these 3 pin codes in backend : 110001,110002,1234

when i tried : var_dump($zipCodes); die(); i got this result : string(19) "110001,110002,1234,"

1
+50

You need to add your custom shipping method code to the if statement like this:

if (
    $method->getCode() == 'cashondelivery' || // the default shipping method method code
    $method->getCode() == 'mpcashondelivery'  // Or the custom CoD method code
   ) { ... }

Complete code of the observer.php:

<?php
class Mfp_Cod_Model_Observer {

    public function getCashOnDelvery(Varien_Event_Observer $observer) {
        $event           = $observer->getEvent();
        $method          = $event->getMethodInstance();
        $result          = $event->getResult();
        $isModuleEnable = Mage::getStoreConfig('cod/cod/enable');

        if($isModuleEnable) {

            // CHANGES HERE
            if($method->getCode() == 'cashondelivery' || $method->getCode() == 'mpcashondelivery'){

                $quote = Mage::getSingleton('checkout/cart')->getQuote();
                $add = $quote->getShippingAddress();
                $postcode = $add->getData('postcode');

                $comparisonMode = Mage::getStoreConfig('cod/cod/mode');
                $zipCodes = Mage::getStoreConfig('cod/cod/zipcode');
                $isExist = false;

                if(trim($zipCodes) == '') {             
                    $result->isAvailable = true;
                } else {    

                    if(strpos($zipCodes, $postcode) !==  false) {
                        $isExist = true;
                    }


                    if($isExist != true) {

                        $zipCodesArray = explode(',', nl2br($zipCodes));
                        if(count($elementLineArray) > 1) {
                            foreach($zipCodesArray as $codzipLine) {
                                $elementLineArray = explode('-', $codzipLine);
                                if(count($elementLineArray) == 2 && ( $postcode >= $elementLineArray[0] && $postcode <= $elementLineArray[1] )) {
                                    $isExist = true;
                                    break;
                                } else if($postcode == $codzipLine) {
                                    $isExist = true;
                                    break;
                                }
                            }
                        }
                    }

                    $returnValue = '';
                        $returnValue = ($isExist)?true:false;

                    $result->isAvailable = $returnValue;

                }   

            } 
        }   
    }
}

UPDATE Try this:

<?php
class Mfp_Cod_Model_Observer {

    public function getCashOnDelvery(Varien_Event_Observer $observer) {
        $event           = $observer->getEvent();
        $method          = $event->getMethodInstance();
        $result          = $event->getResult();
        $isModuleEnable = Mage::getStoreConfig('cod/cod/enable');

        if($isModuleEnable) {

            // CHANGES HERE
            if($method->getCode() == 'cashondelivery' || $method->getCode() == 'mpcashondelivery') {
                $quote = Mage::getSingleton('checkout/cart')->getQuote();
                $add = $quote->getShippingAddress();
                $postcode = $add->getData('postcode');
                $zipCodes = Mage::getStoreConfig('cod/cod/zipcode');

                if(trim($zipCodes) == '' || strpos($zipCodes, $postcode) ===  false) {
                    $result->isAvailable = false; // CHANGES
                } else {
                    $result->isAvailable = true; // CHANGES
                }                
            }
        }
    }

}
  • now zip codes that we included in paymentmethod.php file , they are not allowing for COD, this is fine. – Baby in Magento Apr 6 '16 at 13:00
  • but zip codes that we saved in backend also not working. please help me to fix this. – Baby in Magento Apr 6 '16 at 13:00
  • @BabyinMagento What exactly doesn’t work? – Siarhey Uchukhlebau Apr 6 '16 at 13:01
  • i updated some zip codes in backend using module , only those zip codes should give an option to select "Custom COD" under payment method. but for those zip codes, its not showing "custom COD" option under payment method. – Baby in Magento Apr 6 '16 at 13:05
  • 1
    @BabyinMagento Please, add after $zipCodes = Mage::getStoreConfig('cod/cod/zipcode'); line var_dump($zipCodes); die(); refresh page and update your question with the result. – Siarhey Uchukhlebau Apr 6 '16 at 13:18

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.