0

In customer group "Not Logged In" option alone not saved in db

app/code/Vendor/Module/Setup/InstallData.php

<?php

namespace Vendor\Module\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;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->_eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $groupName = 'Hide Price';
        $eavSetup = $this->_eavSetupFactory->create(array('setup' => $setup));


         $eavSetup->addAttribute(
             \Magento\Catalog\Model\Product::ENTITY,
             'is_hide_price',
             [
                'type' => 'int',
                'input' => 'boolean',
                'label' => 'Hide Price Mode',
                'required' => false,
                'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
                'default' => '0',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'user_defined' => true,
                'sort_order' => 3,
                'group' => $groupName
             ]
         );

         $eavSetup->addAttribute(
             \Magento\Catalog\Model\Product::ENTITY,
             'is_customer_group',
             [
                'type' => 'text',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'frontend' => '',
                'input' => 'multiselect',
                'label' => 'Use Current Price Mode By Customer Group',
                'required' => false,
                'source' =>'Vendor\Module\Model\Config\Source\CustomerGroup',
                'default' => '0',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'user_defined' => true,
                'sort_order' => 4,
                'group' => $groupName
             ]
         );

        $entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
        $attributeSetId = $eavSetup->getAttributeSetId($entityTypeId, 'Default');

        $eavSetup->addAttributeToGroup($entityTypeId, $attributeSetId, $groupName, 'is_hide_price');
        $eavSetup->addAttributeToGroup($entityTypeId, $attributeSetId, $groupName, 'is_customer_group');

    }
}

app/code/Vendor/Module/Model/Config/Source/CustomerGroup.php

<?php

namespace Vendor\Module\Model\Config\Source;

use \Magento\Customer\Model\ResourceModel\Group\Collection;

class customerGroup 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;
    }
}

"NOT LOGGED IN" option is selected before saved

enter image description here

After saved:

enter image description here

Why "NOT LOGGED IN" option value '0' not saved in db

  1. Product id ----> https://prnt.sc/rbe3wu
  2. product attribute id of customer group ---> https://prnt.sc/rbdzuz
  3. saved customer group value ---> https://prnt.sc/rbe3hh
1

Temporary Solution :-

public function toOptionArray()
    {
        $this->_options = [];
        $customerGroup = $this->_customerGroup;
        foreach ($customerGroup as $group){
            if($group->getCustomerGroupId() == 0){
                $group->setCustomerGroupId('999999');
            }
            $this->_options[] = [
                'label' =>$group->getCustomerGroupCode(),
                'value' => $group->getCustomerGroupId()
            ];
        }
        return $this->_options;

    }

You Can use "Not Logged In" Value As 999999

| improve this answer | |
1

Follow the below steps :

1st way :

  • Go to your database eav_attribute
  • Go to is_customer_group attribute
  • Set backend_model NULL

2nd way :

  • Go to your database eav_attribute
  • Go to is_customer_group attribute
  • Set backend_type => varchar

Reindex and clean cache.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.