Magento2-如何阻止以编程方式将产品添加到购物车?
我想做的是,如果在引号中设置了我的自定义属性,那么我不想在购物车中添加任何产品。我的自定义属性已正确设置。 为了阻止产品添加到购物车,我编写了一个观察该事件的观察者 controller_action_predispatch_checkout_cart_add 我的观察者文件代码: public function execute(\Magento\Framework\Event\Observer $observer) { $addedItemId = $observer->getRequest()->getParam('product'); $quote = $this->_cart->getQuote(); if(!empty($quote)) { $customAttribute = $quote->getData('custom_attribute'); if(!empty($customAttribute)) { $controller = $observer->getControllerAction(); $storeId = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getId(); $product = $this->_productRepository->getById($addedItemId, false, $storeId); $observer->getRequest()->setParam('product', null); $this->_messageManager->addError(__('This product cannot be added to your cart.')); echo false; $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true); $this->redirect->redirect($controller->getResponse(), 'checkout/cart/index'); …