If you want to Enable COD Payment Method for Particular Products, Follow below steps
You need to create an extension for this,
Here I have created extension : Magemonkeys_CodRestrict
1. Add new YES/NO product attribute i.e enable_cod_payment and set in Attribute Set which you are using for products
2. Create registration.php file in app/code/Magemonkeys/CodRestrict and add below code
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magemonkeys_CodRestrict', __DIR__ ); |
3. Create module.xml file in app/code/Magemonkeys/CodRestrict/etc and add below code
1 2 3 4 5 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Magemonkeys_CodRestrict" setup_version="1.0"> </module> </config> |
3. Create module.xml file in app/code/Magemonkeys/CodRestrict/etc and add below code
1 2 3 4 5 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Magemonkeys_CodRestrict" setup_version="1.0"> </module> </config> |
4. Create events.xml file in app/code/Magemonkeys/CodRestrict/etc and add below code
1 2 3 4 5 6 |
<?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="codpayment" instance="Magemonkeys\CodRestrict\Observer\CodAvailable" /> </event> </config> |
5. Create CodAvailable.php file in app/code/Magemonkeys/CodRestrict/Observer and add below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?php namespace Magemonkeys\CodRestrict\Observer; use Magento\Framework\Event\ObserverInterface; class CodAvailable implements ObserverInterface { /** * payment_method_is_active event handler. * * @param \Magento\Framework\Event\Observer $observer */ public function execute(\Magento\Framework\Event\Observer $observer) { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cartObj = $objectManager->get('\Magento\Checkout\Model\Cart'); $cartItemsCollection = $cartObj->getQuote()->getItemsCollection(); $cartItemsVisible = $cartObj->getQuote()->getAllVisibleItems(); $itemsArray = $cartObj->getQuote()->getAllItems(); $enable_cod_payment = array(); foreach($itemsArray as $item) { $productid = $item->getProductId(); $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productid); $enable_cod_payment[] = $product->getData('enable_cod_payment'); } if($observer->getEvent()->getMethodInstance()->getCode()=="cashondelivery"){ if(in_array(0, $enable_cod_payment)){ $checkResult = $observer->getEvent()->getResult(); $checkResult->setData('is_available', false); } } } } |
You can change payment method condition as per your need.
[crayon-63e0a3c9788c3134390434/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.