How to disable Cash On Delivery on specific product type
-
13-12-2019 - |
Question
Basically I need to disable cash on delivery on specific product type. Basically i m using store credit of mage store. This is for wallet in magento. on payment option its showing cash on delivery . its a not a physical or virtual product. So i need to disable cash on delivery on this product type.
Solution
You can do this by Magento Event/Observer
First,using payment_method_is_active
disable on depends current quote product type.
Code for this:
Module config.xml code:
<global>
<events>
<payment_method_is_active>
<observers>
<paymentfilter_payment_method_is_active>
<type>singleton</type>
<class>yourmodel/observer</class>
<method>filterpaymentmethod</method>
</paymentfilter_payment_method_is_active>
</observers>
</payment_method_is_active>
</events>
</global>
Observer code is:
<?php class YOURNANESPACE_YOURMODULE_Model_Observer { public function filterpaymentmethod(Varien_Event_Observer $observer) { /* call get payment method */ $method = $observer->getEvent()->getMethodInstance(); /* get Quote */ $quote = $observer->getEvent()->getQuote(); $result = $observer->getEvent()->getResult(); if (empty($quote) || (null === $quote)) { return $this; } /* Disable Your payment method for adminStore */ if ($method->getCode() == 'YOUR_PAYMENT_METHOD_CODE') { foreach ($quote->getAllItems() as $item) { // get Cart item product Type // if ($item->getProductType() == 'YourProductType'): $result->isAvailable = false; endif; } } } }
OTHER TIPS
for that you need to use magento payment_method_is_active
event
I am assuming you know how to develop magento extension[If not then please check this answer you get whole module
Write this code in your config.xml
from app>code>codepool>namespace>package>etc
<global>
...
<events>
<payment_method_is_active>
<observers>
<paymentfilter_payment_method_is_active>
<type>singleton</type>
<class>PaymentFilter_Model_Observer</class>
<method>paymentMethodIsActive</method>
</paymentfilter_payment_method_is_active>
</observers>
</payment_method_is_active>
</events>
...
</global>
Create observer file app/code/codepool>namespace>package/Model/Observer.php
class namespace_package_Model_Observer {
public function paymentMethodIsActive(Varien_Event_Observer $observer) {
$event = $observer->getEvent();
$method = $event->getMethodInstance();
$result = $event->getResult();
$currencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
if($someTrueConditionGoesHere){
$result->isAvailable = true;
}else{
$result->isAvailable = false;
}
}
}
Have you look this I think this would bee solve your issue you just need to modify the methods.phtml file andfrom where you can got any quotes info and do the validations or conditions as well from the module by event - observer or by the modification in phtml file ?
https://stackoverflow.com/questions/29819037/disable-cash-on-delivery-for-specific-products
and