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
1 2 3 4 5 6 7 |
<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="Magento\Catalog\Model\Product"> <plugin name="IsSalablePluginAfter" type="Magemonkeys\Customerrestriction\Plugin\IsSalablePlugin"/> </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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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>Magemonkeys\Customerrestriction\Model\Config\Source\Customer\Group</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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php namespace Magemonkeys\Customerrestriction\Model\Config\Source\Customer; class Group implements \Magento\Framework\Option\ArrayInterface { /** * @var \Magento\Customer\Model\ResourceModel\Group\CollectionFactory */ private $_groupCollectionFactory; private $_options; public function __construct(\Magento\Customer\Model\ResourceModel\Group\CollectionFactory $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
1 2 3 4 5 6 7 8 9 10 |
<?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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<?php declare(strict_types=1); namespace Magemonkeys\Customerrestriction\Plugin; use Magento\Framework\App\Http\Context; use Magento\Customer\Model\Context as CustomerContext; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Store\Model\ScopeInterface; /** * 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 \Magento\Customer\Model\Session */ protected $_customerSession; /** * SalablePlugin constructor. * * @param ScopeConfigInterface $scopeConfig ScopeConfigInterface * @param Context $context Context */ public function __construct( ScopeConfigInterface $scopeConfig, \Magento\Customer\Model\Session $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',\Magento\Framework\App\Config\ScopeConfigInterface::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.
[crayon-63d3e26d9358d768460493/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.