0

Create a product attribute in BE: Engraving Services, input type: Yes/No.

On Checkout - step Payment Method page, check if in cart attribute Engraving Services of valid products: Yes, Then disable Payment Method - COD (Cash On Delivery)

enter image description here enter image description here

https://experienceleague.adobe.com/docs/commerce-admin/catalog/product-attributes/create/attribute-product-create.html

I have followed the devdocs and created the product attribute, but I don't have a solution to disable the COD.

Thanks so much!!!

2 Answers 2

1

Assuming you named Magetu as the vendor name, EngravingServices as the module name, and your custom product attribute named engraving_services.

Declare a new observer in events.xml, and use payment_method_is_active event

File path: app/code/Magetu/EngravingServices/etc/events.xml

<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="disable_cashondelivery_for_engraving_services_product" instance="MageTu\EngravingServices\Observer\DisableCashondeliveryForEngravingServicesProduct" />
    </event>
</config>

Create the Observer class

File path: app/code/Magetu/EngravingServices/Observer/DisableCashondeliveryForEngravingServicesProduct.php

<?php
namespace Magetu\EngravingServices\Observer;

use Magento\Framework\DataObject;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\OfflinePayments\Model\Cashondelivery;
use Magento\Payment\Model\Method\AbstractMethod;
use Magento\Quote\Api\Data\CartInterface;

class DisableCashondeliveryForEngravingServicesProduct implements ObserverInterface
{
    /**
     * Disable Cash On Delivery payment method if the cart contains Engraving Services product
     *
     * @param Observer $observer
     * @throws LocalizedException
     */
    public function execute(Observer $observer)
    {
        /** @var DataObject $result */
        $result = $observer->getResult();
        /** @var AbstractMethod $methodInstance */
        $methodInstance = $observer->getMethodInstance();
        /** @var CartInterface $quote */
        $quote = $observer->getQuote();
        $paymentCode = $methodInstance->getCode();

        if ($paymentCode === Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE) {
            $allItems = $quote->getAllItems();
            /** @var $item \Magento\Quote\Model\Quote\Item */
            foreach ($allItems as $item) {
                if ($item->getProduct()->getData('engraving_services')) {
                    $result->setData('is_available', false);
                    break;
                }
            }
        }
    }
}

2
  • Thanks so much !!!
    – Nam Cong
    Feb 8, 2023 at 8:56
  • 1
    I'm glad to help!
    – Tu Van
    Feb 8, 2023 at 10:52
1

Vendor/Module/etc/events.xml

<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="payment_restrictions" instance="Vendor\Module\Observer\PaymentMethodActiveObserver" />
    </event>
</config>

Vendor/Module/Observer/PaymentMethodActiveObserver.php

namespace Vendor\Module\Observer;

use Magento\Framework\DataObject;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Payment\Helper\Data as paymentData;
use Magento\Payment\Model\Method\AbstractMethod;
use Magento\Quote\Api\Data\CartInterface;

class PaymentMethodActiveObserver implements ObserverInterface
{

    /**
     * @var Data
     */
    private $helperData;
    /**
     * @var paymentData
     */
    private $paymentData;

    /**
     * PaymentMethodActiveObserver constructor.
     * @param Data $helperData
     * @param paymentData $paymentData
     */
    public function __construct(
        Data $helperData,
        paymentData $paymentData
    ) {
        $this->helperData = $helperData;
        $this->paymentData = $paymentData;
    }

    /**
     * @param Observer $observer
     * @throws LocalizedException
     */
    public function execute(Observer $observer)
    {

        /** @var DataObject $result */
        $result = $observer->getResult();
        /** @var AbstractMethod $methodInstance */
        $methodInstance = $observer->getMethodInstance();
        /** @var CartInterface $quote */
        $quote = $observer->getQuote();
        $paymentCode = $methodInstance->getCode();

        $storeId = $quote ? $quote->getStoreId() : null;

        if ($paymentCode == 'cashondelivery') {
            $notAllow = [] 
            foreach ($quote->getAllItems() as $item) {
                $engravingServices = $item->getProduct()->getEngravingServices(); 
                if ($engravingServices) {
                   $notAllow[] = $engravingServices;
                }
            }

            if (!empty($notAllow)) {
               $result->setIsAvailable(false);
            }
        }

    }
}
2
  • Thanks so much !!!
    – Nam Cong
    Feb 8, 2023 at 8:56
  • 1
    Welcome.......:) Feb 8, 2023 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.