I build a custom shipping method and i want to hide "cash on delivery" payment method based on my shipping method selection. I am assuming that one possible way to write the code in observer and set the "is_available"= false for that payment method. But i am not sure whether is there any event get called when user choose the shipping method on checkout
Magento – Magento 2: Which event get called when selecting shipping method on checkout
event-observermagento2
Related Solutions
I've had a similar problem (removing dynamically payment methods) and solved it by extending the _canUseMethod($method)
function in Mage_Checkout_Block_Onepage_Payment_Methods
.
There you can return false for specific payment methods which should not be shown in the payment selection.
Example:
class Company_Module_Block_Methods extends Mage_Checkout_Block_Onepage_Payment_Methods {
protected function _canUseMethod($method) {
if ($method != null && $method == 'checkmo') {
$carriers = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingMethod();
if ($carriers == 'flatrate_flatrate') {
return parent::_canUseMethod($method);
} else {
return false;
}
}
return parent::_canUseMethod($method);
}
}
The config XML would look like:
<config>
<global>
<blocks>
<checkout>
<rewrite>
<onepage_payment_methods>Company_Module_Block_Methods</onepage_payment_methods>
</rewrite>
</checkout>
</blocks>
</global>
</config>
That way I could remove payment methods right before they get shown in the one page checkout.
Hope that helps anyone.
I tried DRAJAs code, but this does not work for me. The Shipping Method title is always NULL. I found another solution using the deprecated Cart Model. I also tried the new Quote model that should replace the Cart Model, but again this returned NULL... so I know it’s not according to Magento Standards, but it does do the trick for now.
The below example is used to hide the Klara payment method when a pick-up location is selected.
app/code/Company/Module/etc/events.xml
<?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>
Company/Module/Observer/PaymentMethodAvailable.php
<?php
namespace Company\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use \Magento\Checkout\Model\Cart;
class PaymentMethodAvailable implements ObserverInterface
{
/**
* @var Cart
*/
protected $cart;
/**
* PaymentMethodAvailable constructor.
* @param Cart $cart
*/
public function __construct(
Cart $cart ){
$this->cart = $cart;
}
/**
* payment_method_is_active event handler.
*
* @param \Magento\Framework\Event\Observer $observer
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$shippingMethod = $this->cart->getQuote()->getShippingAddress()->getShippingMethod();
$paymentMethod = $observer->getEvent()->getMethodInstance()->getCode();
if ($paymentMethod == "klarna_kp" && $shippingMethod == 'tablerate_pickup') {
$checkResult = $observer->getEvent()->getResult();
$checkResult->setData('is_available', false);
}
}
}
Best Answer
SR/Stackexchange/etc/di.xml
SR/Stackexchange/Plugin/Model/MethodList.php