We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    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

    <?php
    MagentoFrameworkComponentComponentRegistrar::register(
    MagentoFrameworkComponentComponentRegistrar::MODULE,
    'Magemonkeys_CodRestrict',
    __DIR__
    );

    3. Create module.xml file in app/code/Magemonkeys/CodRestrict/etc and add below code

    <?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

    <?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

    <?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="MagemonkeysCodRestrictObserverCodAvailable" />
        </event>
    </config>

    5. Create CodAvailable.php file in app/code/Magemonkeys/CodRestrict/Observer and add below code

    <?php
     
    namespace MagemonkeysCodRestrictObserver;
     
    use MagentoFrameworkEventObserverInterface;
     
    class CodAvailable implements ObserverInterface
    {
    	/**
         * payment_method_is_active event handler.
         *
         * @param MagentoFrameworkEventObserver $observer
         */
        public function execute(MagentoFrameworkEventObserver $observer)
        {
            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
    
    		$cartObj = $objectManager->get('MagentoCheckoutModelCart');
    
    		$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('MagentoCatalogModelProduct')->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.

    field_5bfb909c5ccae

      Get a Free Quote