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


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




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


<?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"/>

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


<?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"

now create CartPlugin.php in the following path.



  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.

  • 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
    Commented Jun 16, 2022 at 7:47
  • I didn't understand your words. are you want to add your code in the core file? Commented Jun 16, 2022 at 8:09
  • no, I think will be fine to create a new module, but how I can setup the rule?
    – Robert
    Commented Jun 16, 2022 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. Commented Jun 16, 2022 at 8:18
  • yes will help me a lot if you can help me with that module ... thank you
    – Robert
    Commented Jun 16, 2022 at 8:37

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.