Magento 2 Disable Add To Cart Customer Group

I am creating an extension that will allow you to hide/disable add to cart button for certain customer groups.

1) Declare a module.

<?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="PHPCodez_Disableaddtocart" setup_version="0.0.1" />
</config>

2) Register a module.

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'PHPCodez_Disableaddtocart',
__DIR__
);

3) Create system.xml file

It allows website admin to seleted customer groups.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="phpcodez" translate="label" sortOrder="200">
<label>PHPCodez</label>
</tab>
<section id="phpcodez" translate="label" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class> 
<label>Hide Cart</label>
<tab>phpcodez</tab>
<resource>PHPCodez_Disableaddtocart::config_disableaddtocart</resource>
<group id="parameters" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Settings</label>
<hide_in_single_store_mode>1</hide_in_single_store_mode>
<field id="disableaddtocart" translate="label" type="multiselect" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Hide Add To Cart Option For</label>
<source_model>PHPCodez\Disableaddtocart\Model\Config\Source\Group\Multiselect</source_model>
</field>
</group>
</section>
</system>
</config>

4) Create 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">
<preference for="Magento\Catalog\Model\Product" type="PHPCodez\Disableaddtocart\Model\Product" />
</config>

5)Create source model.

I am creating this source model to collect all the customer groups.

<?php
namespace PHPCodez\Disableaddtocart\Model\Config\Source\Group;
use \Magento\Customer\Model\ResourceModel\Group\Collection;

class Multiselect implements \Magento\Framework\Option\ArrayInterface {

protected $_customerGroup;

protected $_options;

public function __construct( Collection $customerGroup ) {
$this->_customerGroup = $customerGroup; 
}

public function toOptionArray() {
if (!$this->_options) {
$this->_options = $this->_customerGroup->toOptionArray();
}
return $this->_options;
}
}

6) Create Model.

<?php
namespace PHPCodez\Disableaddtocart\Model;

class Product extends \Magento\Catalog\Model\Product {

public function isSalable() {

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$disableCartHelper = $objectManager->create('\PHPCodez\Disableaddtocart\Helper\Data');

$disabledGroups = explode(',',$disableCartHelper->getDisabledGroups());
$customerGroup = $disableCartHelper->getCustomerGroup();

if(in_array($customerGroup,$disabledGroups)) {
return false;
} else {
return parent::isSalable();
}

}
}

7) Create Helper

<?php
namespace PHPCodez\Disableaddtocart\Helper;

use \Magento\Framework\App\Helper\AbstractHelper;
use \Magento\Framework\App\Config\ScopeConfigInterface;
use \Magento\Store\Model\ScopeInterface;
use \Magento\Customer\Model\Session;

class Data extends AbstractHelper {

protected $_scopeConfig;
protected $_customerSession;

public function __construct(ScopeConfigInterface $_scopeConfig,Session $_customerSession) {
$this->_scopeConfig = $_scopeConfig;
$this->_customerSession = $_customerSession;
}

public function getDisabledGroups($scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT) {
$disabledGroups = $this->_scopeConfig->getValue('phpcodez/parameters/disableaddtocart',ScopeInterface::SCOPE_STORE);
return $disabledGroups;
}

public function getCustomerGroup(){
return $this->_customerSession->isLoggedIn()?$this->_customerSession->getCustomer()->getGroupId():0;
}


}

8) Issue the following commands.

$ php bin/magento indexer:reindex
$ php bin/magento setup:upgrade
$ php bin/magento setup:di:compile
$ php bin/magento setup:static-content:deploy -f
$ php bin/magento cache:flush
Share

Leave a Reply

Your email address will not be published. Required fields are marked *