0

I am using Magento 2.3.0

I have some products in my website which allows custom printing and these products are assigned in category "Printing Products". Now I want to customize add to cart functionality for these custom printing products. I don't want to add any other category products with these custom printing products. For Ex., Let's say When there is custom printing product is already added to a product and if I try to add any products other than "Printing product" category then this product must not be added to cart and an error message should be displayed.

In short, Custom Printing category products are not allowed to shop with other products. So, How can I do this?

EDIT : When the cart is empty or cart is having other product from "Printing Products" category then I should be able to add printing product into the cart and in this situation, if I try to add products other than "Printing Products" category then the error message should be shown.

1

You can try the step below to restrict add to cart from diffrient categories.

Assume you are using a custom module name "Company_MyModule".

STEPP 1)

Create di.xml under YOUR-MAGENTO-ROOT/app/code/Company/MyModule/etc/

File: YOUR-MAGENTO-ROOT/app/code/Company/MyModule/etc/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\Checkout\Model\Cart">
        <plugin name="restrictAddTocart"
                type="Company\MyModule\Model\Plugin\Cart"/>
    </type>
</config>

Step 2)

Create Cart.php under YOUR-MAGENTO-ROOT/app/code/Company/MyModule/Model/Plugin

File: YOUR-MAGENTO-ROOT/app/code/Company/MyModule/Model/Plugin/Cart.php

<?php

namespace Company\MyModule\Model\Plugin;

<?php

namespace Company\MyModule\Model\Plugin;

class Cart
{
    protected $productRepository;
    protected $storeManager;
    protected $checkoutSession;
    protected $logger;

    const PRINT_CATEGORY_ID = 4; // set print category ID

    public function __construct( \Magento\Catalog\Api\ProductRepositoryInterface $productrepositoryInterface, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Session\SessionManagerInterface $checkoutSession, \Psr\Log\LoggerInterface $loggerInterface )
    {
        $this->productRepository = $productrepositoryInterface;
        $this->storeManager      = $storeManager;
        $this->checkoutSession   = $checkoutSession;
        $this->logger            = $loggerInterface;
    }
    public function beforeAddProduct( \Magento\Checkout\Model\Cart $subject, $productInfo, $requestInfo = null )
    {
    $productId =  $productInfo->getData('entity_id');
        $type_id = $productInfo->getData('type_id');
        $product = $this->productRepository->getById($productId);        
        $categoryIds = $product->getCategoryIds();
        $this->logger->info("Current Product Category IDs",$categoryIds);

        if ( is_array($categoryIds) && !in_array(self::PRINT_CATEGORY_ID,$categoryIds) ) {            
             throw new \Magento\Framework\Exception\LocalizedException(__('Since Print product exist in your cart, You only order from Print Category products'));
                return $this;
        }

        return array(
             $productInfo,
            $requestInfo 
        );
    }
}
| improve this answer | |
  • I have created the module as you explained but when I ran upgrade command in SSH then it gave me an error "There are no commands defined in the "setup" namespace.". what can be the issue? – Chintan Kaneriya Jun 15 '19 at 11:04
  • Now, this extension is working but I am not able to add printing products into cart even though the cart is empty. please check my edited question. – Chintan Kaneriya Jun 17 '19 at 4:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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