0

I am creating order from admin end.

My requirement is to restrict few products to place order only from admin.

I need to show some message when those particular product selected and added to Order, I tried for event "checkout_cart_product_add_after" but that is not firing here.

I need to restrict it based on attribute value or sku.

Can anyone suggest me how this can be achieved? Thanks!!

3
  • Add more information & code if possible in question, to help understand question better. Aug 27, 2020 at 8:07
  • @HamendraSunthwal, Updated my question, please let me know if anything not clear. Thanks
    – Manjunath
    Aug 27, 2020 at 8:10
  • Have you check the log file or console? Aug 27, 2020 at 9:56

2 Answers 2

2

You can also apply inline error showing as Magento has a bug that using the above solution, it will not remove the exception message, when you add correct product to cart.

In that case, you have to allow that product to be added to cart, with showing an inline error message.

Apply following solution in that case.

etc/adminhtml/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\Quote\Item\AbstractItem">
        <plugin name="restrict_specific_product_addtocart_inline_error" type="Yash\SampleModule\Plugin\Magento\Quote\Model\Quote\Item\AbstractItem" sortOrder="499" />
    </type>
</config>

Plugin\Magento\Quote\Model\Quote\Item\AbstractItem.php

<?php

namespace Yash\SampleModule\Plugin\Magento\Quote\Model\Quote\Item;

class AbstractItem
{
    public function afterCheckData($subject, $result)
    {
        if (in_array($subject->getProduct()->getId(), ['1'])) {
            $subject->setHasError(true);
            $subject->setMessage('Your error message');
            $subject->getQuote()->setHasError(true)->addMessage('Your error message');
        }

        return $result;
    }
}
1
+50

You can create an event observer to achieve this.

etc/adminhtml/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="adminhtml_sales_order_create_process_data_before">
        <observer name="restrict_product_addtocart_adminorder" instance="Yash\SampleModule\Observer\RestrictProductAddtocartAdminOrder" />
    </event>
</config>

Observer/RestrictProductAddtocartAdminOrder.php

<?php

namespace Yash\SampleModule\Observer;

class RestrictProductAddtocartAdminOrder implements \Magento\Framework\Event\ObserverInterface
{
    public function __construct(
        \Magento\Framework\Message\ManagerInterface $messageManager
    ) {
        $this->messageManager = $messageManager;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $items = $observer->getRequestModel()->getPost('item');

        if ($items) {
            $productIds = array_keys($items);

            if (in_array(1, $productIds)) {
                throw new \Exception('You can\'t add product with id 1');
            }
        }
        
    }
}
4
  • hi @Yash, $items will have the product object here?
    – Manjunath
    Sep 17, 2020 at 12:02
  • yes, keys of that array contains product ids while inner elemtns are other variables like configuration options and all ..
    – Yash Shah
    Sep 17, 2020 at 12:06
  • you can log this object to see its value in a log file
    – Yash Shah
    Sep 17, 2020 at 12:07
  • okay Thanks I will check that.
    – Manjunath
    Sep 17, 2020 at 12:09

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.