0

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.

enter image description here

Here problem is it is working only for 6 digits.

if we enter any below 6 digit zip codes in shipping address , its displaying COD. This is problem.

ex: 2,24, 246, 2468, 12345

but if we enter any 6 digit zip codes other than zipcodes which is present backend, its not displaying COD. This is fine.

ex: 123456, 333999, 666666

as validation is working fine for 6 digits we want for other digits also.

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;

                }   

            } 
        }   
    }
}

config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Mfp_Cod>
      <version>1.0.0</version>
    </Mfp_Cod>
  </modules>
  <global>
          <models>
            <cod>
                <class>Mfp_Cod_Model</class>
            </cod>
        </models>

    <helpers>
      <cod>
        <class>Mfp_Cod_Helper</class>
      </cod>
    </helpers>
  </global>
    <frontend>
        <events>
            <payment_method_is_active>
                <observers>
                    <paymentfilter_payment_method_is_active>
                        <type>singleton</type>
                        <class>Mfp_Cod_Model_Observer</class>
                        <method>getCashOnDelvery</method>
                    </paymentfilter_payment_method_is_active>
                </observers>
            </payment_method_is_active>
        </events>
    </frontend>
</config> 
1

Try this code in 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');
        $returnValue = false;
        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(strlen($postcode) >= 6) {
                    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 = ($isExist)?true:false;
                        $result->isAvailable = $returnValue;
                    }
                }
                $result->isAvailable = $returnValue;   

            } 
        }   
    }
}
  • now its not allowing for zip codes i entered in backend also. – Baby in Magento Jun 29 '16 at 5:44
  • updated ans pls check now – Abdul Jun 29 '16 at 5:52
  • this is class, whenever and all i am in some big problem you will come & help me, Thanks a lot buddy. you are my life saver :-) – Baby in Magento Jun 29 '16 at 5:57

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.