We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

Please follow the below steps if you want to disable add to cart for a particular customer group.

Step 1: Create a plugin and define in Magemonkeys/Customerrestriction/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- Disable Add to Cart -->
    <type name="MagentoCatalogModelProduct">
        <plugin name="IsSalablePluginAfter" type="MagemonkeysCustomerrestrictionPluginIsSalablePlugin"/>
    </type>
</config>

Step 2: Create a configuration field to select a customer group so that you can disable add to cart for that particular group in Magemonkeys/Customerrestriction/etc/adminhtml/system.xml.

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Magento/Config/etc/system_file.xsd">
    <system>
        <section id="catalog">
            <group id="customer_group" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <field id="disable_cutomer_group" translate="label" type="multiselect" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Disbale Add to Cart</label>
                    <source_model>MagemonkeysCustomerrestrictionModelConfigSourceCustomerGroup</source_model>
                    <can_be_empty>1</can_be_empty>
                    <comment>Please slect only one group</comment>
                </field>
            </group>
        </section>
    </system>
</config>

Step 3: Create a source model on this path Magemonkeys/CustomerrestrictionModel/Config/Source/Customer/Group.php

<?php
namespace MagemonkeysCustomerrestrictionModelConfigSourceCustomer;
 
class Group implements MagentoFrameworkOptionArrayInterface
{

    /**
     * @var MagentoCustomerModelResourceModelGroupCollectionFactory
     */
    private $_groupCollectionFactory;

    private $_options;

    public function __construct(MagentoCustomerModelResourceModelGroupCollectionFactory $groupCollectionFactory)
    {
        $this->_groupCollectionFactory = $groupCollectionFactory;
    }
 
    /**
     * @return array
     */
    public function toOptionArray()
    {
        if (!$this->_options) {
            $this->_options = $this->_groupCollectionFactory->create()->loadData()->toOptionArray();
        }
        return $this->_options;
    }
}

Step 4: Set default value for the disable add to cart configuration field. You have to add the below code

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <catalog>
            <customer_group>
                <disable_cutomer_group>0</active>
            </wkmodule>
        </payment>
    </default>
</config>

on this path: Magemonkeys/CustomerrestrictionModel/etc/config.xml

Step 5: Now create a plugin for disable add to cart for a particular customer group :

<?php

declare(strict_types=1);

namespace MagemonkeysCustomerrestrictionPlugin;

use MagentoFrameworkAppHttpContext;
use MagentoCustomerModelContext as CustomerContext;
use MagentoFrameworkAppConfigScopeConfigInterface;
use MagentoStoreModelScopeInterface;

/**
 * Class IsSalablePlugin
 *
 * @category Plugin
 */
class IsSalablePlugin
{
    /**
     * Scope config
     *
     * @var ScopeConfigInterface
     */
    protected $scopeConfig;

    /**
     * HTTP Context
     * Customer session is not initialized yet
     *
     * @var Context
     */
    protected $context;

    /**
     * @var MagentoCustomerModelSession
     */
    protected $_customerSession;

    /**
     * SalablePlugin constructor.
     *
     * @param ScopeConfigInterface $scopeConfig ScopeConfigInterface
     * @param Context              $context     Context
     */
    public function __construct(
        ScopeConfigInterface $scopeConfig,
        MagentoCustomerModelSession $customerSession,
        Context $context
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->context = $context;
        $this->_customerSession = $customerSession;
    }

    /**
     * Check if customer group for disable add to cart functionality
     *
     * @return bool
     */
    public function afterIsSalable(): bool
    {
        $groupId = $this->scopeConfig->getValue('catalog/customer_group/disable_cutomer_group',MagentoFrameworkAppConfigScopeConfigInterface::SCOPE_TYPE_DEFAULT);
        $customerGroup = $this->_customerSession->getCustomer()->getGroupId();
        if($customerGroup == $groupId)
        {
            return false;
        }
        
        return true;
    }
}

You have to follow the above steps to achieve the functionality of disabling add to cart for a particular customer group.

field_5bfb909c5ccae

    Recent Articles
    Get a Free Quote

      Full Name

      Email Address

      Mobile Number

      Message

      Let’s initiate a discussion!!

      With Only Agency that provides a 24/7 emergency support.

        Full Name

        Email Address

        Mobile Number

        Message