Magento 2 Unable To Unselect All Multiselect Values

Add the tag <can_be_empty>1</can_be_empty> with the field in system.xml file and now you should be enable to Unselect the multi select values.

Ex:

<field id="hideprice" translate="label" type="multiselect" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Hide Price For</label>
<source_model>PHPCodez\Permission\Model\Config\Source\Group\Multiselect</source_model>
<can_be_empty>1</can_be_empty>
</field>

Magento 2 Hide Price

Basically we have to override class ‘Magento\Catalog\Pricing\Render\FinalPriceBox ‘ to achieve this. Here I am creating an extension ‘PHPCodez_Hideprice’ that will allow you to hide price from certain user groups and diaplay a text inplace of price.

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

2) Register a module. Continue reading Magento 2 Hide Price

Magento 2 Source Model All Customer Groups

You can use the source model <source_model>Magento\Customer\Model\Config\Source\Group\Multiselect</source_model> to get the customer groups.

But its does not return the group ‘NOT LOGGED IN’ .so I have created new model to have ‘NOT LOGGED IN’ in the list.

<source_model>PHPCodez\Hideprice\Model\Config\Source\Group\Multiselect</source_model>

<?php
namespace PHPCodez\Hideprice\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;
}
}

Current Customer Group Magento 2

<?php
namespace PHPCodez\Disableaddtocart\Helper;

use \Magento\Framework\App\Helper\AbstractHelper;
use \Magento\Customer\Model\Session;

class Data extends AbstractHelper {

protected $_customerSession;

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

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


}

System Configuration Values Helpers Magento 2

<?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;
}


}

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. Continue reading Magento 2 Disable Add To Cart Customer Group