1

What I need to do is to display different payment methods based on billing regions. Magento has built in the option to display different payment methods based on the country. So my question is this can be made to display different methods of payment depending on the region of the country. For example, if I have country x with payment methods pay1 pay2 pay3, for the region y of this country I want to be only pay1 and pay2.

It would be better if I didn't modify any core files.

1 Answer 1

2

Yii ,Do this using event observer

Create an event on payment_method_is_active and depends region on disable and enable payment method:

See at Implementing payment method per currency

and just need change observer

create config.xml under :app/code/community/Devamitbera/Statewisecod/etc/config.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
@Author Amit Bera
@Email [email protected]
@ Website: www.amitbera.com
*/-->
<config>
    <modules>
        <Devamitbera_Statewisecod>
            <version>1.0.0</version>
        </Devamitbera_Statewisecod>
    </modules>
    <global>
        <models>
            <statewisecod>
                <class>Devamitbera_Statewisecod_Model</class>
            </statewisecod>
        </models>
    </global>
    <frontend> <!--  run observer  event for frontend -->
        <events>
            <payment_method_is_active>
                <observers>
                    <enable_cod_for_some_state>
                        <class>statewisecod/observer</class>
                        <method>EnableCod</method>
                    </enable_cod_for_some_state>
                </observers>
            </payment_method_is_active>
        </events>
    </frontend>
</config>

Create observer file under Observer.php under app/code/community/Devamitbera/Statewisecod/Model

Code of this file:

  <?php
class Devamitbera_Statewisecod_Model_Observer
{
    public function EnableCod($observer){
        $result=$observer->getEvent()->getResult();
        $MethodInstance=$observer->getEvent()->getMethodInstance();
        $quote=$observer->getEvent()->getQuote();

        if($quote && $quote->getId()):
        /* If Payment method is  cashondelivery  then  conitnue  */
            if($MethodInstance->getCode()=='cashondelivery'){
                #Mage::log('Payment is Cod',null,'Cod.log',true);
                $ShippingAddress=$quote->getShippingAddress();

                /* region_id is working when country have  
                * drop state/regions.
                */
                /* Here i  have put USA coutry new work & Washinton redion */

                #Mage::log('redion'.$ShippingAddress->getRegionId(),null,'redion.log',true);

                $CodEnableRegionIds=array(62,43);
                if(in_array($ShippingAddress->getRegionId(),$CodEnableRegionIds)):
                      $result->isAvailable=true;
                elseif(is_null($ShippingAddress->getRegionId()) && !is_null($ShippingAddress->getRegion())):
                /* This section working when State/region is not dropdown 
                 and state is dropdown
                */
                $textListRegionName=array('West bengal','Delhi');
                    if(in_array($ShippingAddress->getRegion(),$textListRegionName)){
                          $result->isAvailable=true;
                    }else{
                          $result->isAvailable=false;
                    }
                else:
                  $result->isAvailable=false;
                endif;              


                return   $result->isAvailable;
            }

        endif;


    }
}

create module file Devamitbera_Statewisecod.xml under app/etc/modules

<?xml version="1.0" encoding="utf-8"?>
<!--
@Author Amit Bera
@Email [email protected]
@ Website: www.amitbera.com
-->
<config>
    <modules>
    <Devamitbera_Statewisecod>
        <codePool>community</codePool>
        <active>true</active>
        <depends><Mage_Payment/></depends>
    </Devamitbera_Statewisecod>
    </modules>
</config>

Here cashondelivery is payment method code of cash on delivery.... which is saved in database.

- Edited:

region_id is working when country have drop state/regions list.

if(in_array($ShippingAddress->getRegionId(),$CodEnableRegionIds)):
                      $result->isAvailable=true;

If State/region is not dropdown then Below logic is work

elseif(is_null($ShippingAddress->getRegionId()) && !is_null($ShippingAddress->getRegion())):
$textListRegionName=array('West bengal','Delhi');
    if(in_array($ShippingAddress->getRegion(),$textListRegionName)){
      $result->isAvailable=true;
    }else{
      $result->isAvailable=false;
    }
4
  • Thank you very much, i tried it but nothing happens, how can i try if it is implemented ok? i made several var_dump but nothing shows up..
    – Ylli
    Commented Nov 13, 2014 at 14:10
  • have you follow magento.stackexchange.com/questions/24447/… link to create extension?
    – Amit Bera
    Commented Nov 13, 2014 at 14:12
  • Does this work for one page checkout ?
    – Ylli
    Commented Nov 13, 2014 at 14:19
  • of course.. it will work
    – Amit Bera
    Commented Nov 13, 2014 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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