Creating module PHPCodez_Customattribute to add a new attribute to list all the customer groups including ‘NOT LOGGED IN’.
1) Declare the 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_Customattribute" setup_version="0.0.1" /> </config>
2) Register the module.
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'PHPCodez_Customattribute', __DIR__ );
3) Create Install script.
<?php namespace PHPCodez\Customattribute\Setup; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; class installData implements InstallDataInterface { private $eavSetupFactory; protected $logger; public function __construct(EavSetupFactory $eavSetupFactory,\Psr\Log\LoggerInterface $logger) { $this->eavSetupFactory = $eavSetupFactory; $this->logger = $logger; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $this->logger->info("System - upgrading module - Creating product attribute - product_customer_group"); $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute( \Magento\Catalog\Model\Product::ENTITY, 'product_customer_group',[ 'type' => 'text', 'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend', 'frontend' => '', 'label' => 'Hide Product For', 'input' => 'multiselect', 'class' => '', 'source' => 'PHPCodez\Customattribute\Model\Config\Source\Group\Multiselect', 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL, 'visible' => true, 'required' => true, 'user_defined' => false, 'default' => '', 'searchable' => false, 'filterable' => false, 'comparable' => false, 'visible_on_front' => false, 'used_in_product_listing' => true, 'unique' => false, 'apply_to' => '' ] ); } }
4) Create source model.
<?php namespace PHPCodez\Customattribute\Model\Config\Source\Group; use \Magento\Customer\Model\ResourceModel\Group\Collection; class Multiselect extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource 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; } public function getAllOptions() { if (!$this->_options) { $this->_options = $this->_customerGroup->toOptionArray(); } return $this->_options; } } 5) Run 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