Dears, how can I disable the purchase of Online e-gift card
once the end user selects the method of payment as: CASH ON DELIVERY?
I don't want to let the users purchase an Online E-GIFT card and make the payment upon delivery.
First you will need to create events.xml file under app/code/Company/Module/etc/. Then write “payment_method_is_active” event in it. This is the event which hits on checkout page for payment method availability.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="payment_method_is_active">
<observer name="custom_payment" instance="Company\Module\Observer\PaymentMethodAvailable" />
</event>
</config>
Now create PaymentMethodAvailable.php under Company/Module/Observer/ and write following code in the file. I am disabling the CASH ON DELIVERY payment method when there is gift card in your cart (You can get the CheckoutSession and check if there is any gift card in cart)
<?php
namespace Company\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
class PaymentMethodAvailable implements ObserverInterface
{
/**
* payment_method_is_active event handler.
*
* @param \Magento\Framework\Event\Observer $observer
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$giftCardFlag = false;
if(//checkforcondition if giftCard in cart) {
$giftCardFlag = true;
}
// you can replace "checkmo" with your required payment method code
if($observer->getEvent()->getMethodInstance()->getCode()=="youPaymentCode" && giftCardFlag) {
$checkResult = $observer->getEvent()->getResult();
$checkResult->setData('is_available', false); //this is disabling the payment method at checkout page
}
}
}
Now the payment method CASH ON DELIVERY is disabled from checkout page .
-
can't we have a simple answer? in addition, can i disable some products in my store to have the checkout selected with COD payment? – Marcos Mar 22 at 11:38
-
-
Simpler in which way, this is best implementation that i can think of – Vishwas Bhatnagar Mar 26 at 12:02
-
Hello dear, yes I'm ready for the coding level, but i didn't found this path: app/code/Company/Module/etc/ also inside the XML file i couldn't find this path for the instance instance="Company\Module\Observer\PaymentMethodAvailable" , so can you support please? my path and template are as follows: public_html/app/etc/modules and public_html/app/design/frontend/tv_arion_package/tv_arion – Marcos Mar 27 at 13:05
-
We are going to create a simple module say Pawan_FilterPaymentMethod
Create following files:
app/etc/modules/Pawan_FilterPaymentMethod.xml
<?xml version="1.0"?>
<config>
<modules>
<Pawan_FilterPaymentMethod>
<active>true</active>
<codePool>local</codePool>
</Pawan_FilterPaymentMethod>
</modules>
</config>
app/code/local/Pawan/FilterPaymentMethod/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Pawan_FilterPaymentMethod>
<version>0.0.1</version>
</Pawan_FilterPaymentMethod>
</modules>
<global>
<models>
<filterpaymentmethod>
<class>Pawan_FilterPaymentMethod_Model</class>
</filterpaymentmethod>
</models>
<events>
<payment_method_is_active>
<observers>
<paymentfilter_payment_method_is_active>
<class>filterpaymentmethod/observer</class>
<method>filterpaymentmethod</method>
</paymentfilter_payment_method_is_active>
</observers>
</payment_method_is_active>
</events>
</global>
</config>
app/code/local/Pawan/FilterPaymentMethod/Model/Observer.php
<?php
class Pawan_FilterPaymentMethod_Model_Observer {
public function filterpaymentmethod(Varien_Event_Observer $observer) {
$method = $observer->getEvent()->getMethodInstance();
$quote = $observer->getEvent()->getQuote();
$result = $observer->getEvent()->getResult();
if($method->getCode()=='cashondelivery' ){ // payment method code
foreach ($quote->getAllItems() as $item) {
if($item->getProductType()=='simple')://check product type
$result->isAvailable = false;
endif;
}
}
}
}
Note:
e-gift card
products. – Pawan Mar 19 at 3:20