I tried to override COD payment method.
I had to show COD option only if billing zipcode/postcode is one among the list I have in a table in my DB.
I had to modify the following to accomplish my task.
Mage_Payment_Block_Form_Container --> function getMethods() Mage_Payment_Model_Method_Cashondelivery --> function getCODPincodes()
[getCODPincodes() is a custom method to check that pincode table and form an array to check with billing postcode]
It worked!!!
but when I try to create a module with this.
my config.xml
<?xml version="1.0"?>
<config>
<modules>
<Royal_Payment>
<version>1.0.0</version>
</Royal_Payment>
</modules>
<global>
<models>
<payment>
<rewrite>
<method_cashondelivery>Royal_Payment_Model_Method_Cashondelivery</method_cashondelivery>
</rewrite>
</payment>
</models>
<blocks>
<payment>
<rewrite>
<form_container>Royal_Payment_Block_Form_Container</form_container>
</rewrite>
</payment>
</blocks>
</global>
my folder structure
app\code\local\Royal\Payment\Block\Form\Container.php
<?php
class Royal_Payment_Block_Form_Container extends Mage_Payment_Block_Form_Container
{
public function getMethods()
{
$methods = $this->getData('methods');
if (is_null($methods)) {
$quote = $this->getQuote();
$store = $quote ? $quote->getStoreId() : null;
$methods = $this->helper('payment')->getStoreMethods($store, $quote);
$total = $quote->getBaseSubtotal() + $quote->getShippingAddress()->getBaseShippingAmount();
foreach ($methods as $key => $method) {
$allowCOD = 1;
if($method->getCode() == "cashondelivery"){
$postcodes = Mage::getModel('payment/method_cashondelivery')->getCODPincodes();
$shippingPincode = $this->getQuote()->getBillingAddress()->getData('postcode');
if(!in_array($shippingPincode, $postcodes)){
$allowCOD = 0; // don't allow cod method -- it goes to else part in next if... else... block
}
}
if ($this->_canUseMethod($method)
&& ($total != 0
|| $method->getCode() == 'free'
|| ($quote->hasRecurringItems() && $method->canManageRecurringProfiles())) && ($allowCOD == 1)) {
$this->_assignMethod($method);
} else {
unset($methods[$key]);
}
}
$this->setData('methods', $methods);
}
return $methods;
}
}
app\code\local\Royal\Payment\Model\Method\Cashondelivery.php
<?php
class Royal_Payment_Model_Method_Cashondelivery extends Mage_Payment_Model_Method_Cashondelivery
{
public function getCODPincodes(){
$write = Mage::getSingleton('core/resource')->getConnection('core_read');
$query = "select pincode from `pincode`";
$results = $write->fetchAll($query);
foreach($results as $result){
$postcodes[] = $result['pincode'];
}
return $postcodes;
}
}
My overriden Model is working fine. But block method is not invoked
Where am I going wrong? Can anyone help please?