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.
If you want restrict customer to checkout based on your...
Sometime we need to set html data without load or...
If you want get query string params in controller file,...
Create di.xml and add the below code Magemonkey/Redirect/etc/frontend/di.xml [crayon-6287740eb44a5364269516/] Create...
You can try below code to change local date to...