0

how do I can set up a rule in Magento 2 by category, If need to do something like this: I one product that is from one category is added to the cart then the customer can't add any product from another category in the cart?

Thank you

1 Answer 1

1

The first step is to register your module with registration.php. The file location will be

app/code/Softi/CartProduct/registration.php

<?php

 \Magento\Framework\Component\ComponentRegistrar::register(
 \Magento\Framework\Component\ComponentRegistrar::MODULE,
 'Softi_CartProduct',
  __DIR__
 );

Create module.xml file. In this file, you will define your module name and version. The file location will be

app/code/Softi/CartProduct/etc/module.xml

<?xml version="1.0"?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
     <module name="Softi_CartProduct" schema_version="0.0.1" setup_version="0.0.1"/>
 </config>

Create a di.xml file to override the cart class. The file location will be

app/code/Softi/CartProduct/etc/frontend/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="interceptAddingProductToCart"
            type="Softi\CartProduct\Plugin\CartPlugin"
            sortOrder="10"
            disabled="false"/>
    </type>
 </config>

now create CartPlugin.php in the following path.

app/code/Softi/CartProduct/Plugin/CartPlugin.php

<?php

  namespace Softi\CartProduct\Plugin;

   class CartPlugin
 {



public function __construct(
  
    \Magento\Catalog\Model\ProductCategoryList $productCategory,
    \Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory $itemCollection,
    \Magento\Framework\Message\ManagerInterface $messageManager,
    \Magento\Checkout\Model\Cart $cart

) {
    $this->itemCollection = $itemCollection;
    $this->messageManager = $messageManager;
    $this->productCategory = $productCategory;
    $this->cart = $cart;



}

public function beforeAddProduct($subject, $productInfo, $requestInfo = null)
{

    $productcatId = $productInfo->getId();

    $categoryIds = $this->productCategory->getCategoryIds($productcatId);
    $category = [];
    if ($categoryIds) {
        $category = array_unique($categoryIds);
    }

    $items = $this->cart->getQuote()->getAllItems();
    if($items) {

        foreach ($items as $item) {
            $categoryId = $this->productCategory->getCategoryIds($item->getProductId());
            $CartProductcategory = [];

              if ($categoryId) {
                   $CartProductcategory = array_unique($categoryId);
               }
           }
       
           if( $category === $CartProductcategory){
                return [$productInfo, $requestInfo];
            } else {
                messageManager->addError(__('add same category product'));
            }

        } else {
            return [$productInfo, $requestInfo];
     }
  }
}

Run the following command:

sudo bin/magento setup:upgrade sudo bin/magento setup:di:compile sudo bin/magento cache:flush

here vendor_name = 'Softi' and module_name = 'CartProduct' you can change it according to your requirment.

I hope it would be helpful for you.

8
  • Hi, thank you, I'm not so good on modules, can you tell me where I can add the categories ID or product ID?
    – Robert
    Jun 16 at 7:47
  • I didn't understand your words. are you want to add your code in the core file? Jun 16 at 8:09
  • no, I think will be fine to create a new module, but how I can setup the rule?
    – Robert
    Jun 16 at 8:12
  • cleck the following link to learn to make the module. blog.qaisarsatti.com/magento_2/… however, if you don't understand then tell me I will edit the code completely. Jun 16 at 8:18
  • yes will help me a lot if you can help me with that module ... thank you
    – Robert
    Jun 16 at 8:37

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.