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