LuFFy
LuFFy

Reputation: 9297

Set Payment Method "Cash On Delivery" on Particular State Only

I want to set "Cash On Delivery" for the site owner's State only

I know how to set For "Specified Country"

I am using Magento 1.8

How can I achieve this ?

Upvotes: 2

Views: 2849

Answers (1)

Amit Bera
Amit Bera

Reputation: 7611

LuFFy,you doing this using magento event observer:

Create an extension and here the step:

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

Upvotes: 2

Related Questions