Removing Specific Payment Options Based on Shipping Method Selection in Magento
As a senior developer, I often encounter situations where clients require granular control over their e-commerce platform’s payment options. One common requirement is to remove or disable certain payment methods based on the selected shipping option.
Let me walk you through how to achieve this in your Magento store by providing a cash payment option only if “Free International Shipping” isn’t selected.
Step 1: Identify the Payment and Shipping Methods
First, you need to identify the internal IDs of both the payment method (in this case, Cash On Delivery) and the shipping method (here, Free International Shipping).
You can find these in your Magento admin panel under Stores > Settings > Configuration > Sales > Payment Methods for the payment id, and Stores > Settings > Configuration > Sales > Shipping Methods for the shipping id.
For example:
- Payment Method ID: cashondelivery
- Shipping Method ID: freeshipping_fixed
Step 2: Create a Custom Plugin
To modify the behavior of payment methods based on shipping options, we’ll create a custom plugin. Magento uses plugins and events to allow developers to modify core functionality without modifying core code directly.
Create a new file called di.xml in your custom module’s configuration directory (usually under app/code/Vendor/Module/etc/di.xml).
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Model\ShippingInformationValidator">
<plugin name="remove_cash_on_delivery_if_free_shipping_selected"
type="Vendor\Module\Model\Plugin\ShippingInformationValidator"/>
</type>
</config>
Step 3: Create the Plugin Class
Now, you need to create the actual plugin class that will handle the removal of the payment option.
Create a new file named ShippingInformationValidator.php in your custom module’s Model directory (usually under app/code/Vendor/Module/Model).
<?php
namespace Vendor\Module\Model\Plugin;
use Magento\Framework\App\RequestInterface;
use Magento\Quote\Model\Quote;
use Magento\Checkout\Model\ShippingInformationValidator as Subject;
class ShippingInformationValidator
{
private $request;
private $quoteRepository;
public function __construct(
RequestInterface $request,
\Magento\Quote\Api\Data\QuoteManagementInterface $quoteRepository
) {
$this->request = $request;
$this->quoteRepository = $quoteRepository;
}
public function aroundValidate(Subject $subject, callable $proceed)
{
$result = $proceed();
if ($result === true) {
$shippingMethod = $this->request->getParam('shipping_method');
if ($shippingMethod === 'freeshipping_fixed') {
$quoteId = $this->request->getParam('quote_id');
$quote = $this->quoteRepository->get($quoteId);
$paymentMethods = $quote->getPayment()->getMethodInstance();
unset($paymentMethods['cashondelivery']);
}
}
return $result;
}
}
Step 4: Enable and Apply the Plugin
To apply this plugin, you need to enable your custom module in Magento.
Go to Stores > App Setup > Developer Tools > Modules and find your Vendor_Module entry. Set the Enabled toggle to Yes.
After enabling, clear your cache using a command like php bin/magento cache:clean.
Conclusion
Removing payment options based on shipping methods can be achieved by creating a custom plugin in Magento. By following this guide, you should now be able to restrict certain payment methods when specific shipping options are selected, providing a more streamlined and secure checkout experience for your customers.
Remember to replace Vendor with the actual vendor name of your custom module and keep the code clean and structured according to best practices.
For more in-depth tutorials on Magento development, feel free to explore our blog at LaravelCompany. We cover a wide range of topics to help you master e-commerce development.