1

I created the customer attribute in Magneto but it is not showing in Customer grid i need to show it without deleting the current attribute. How can i do it.

My code InstallData.php

namespace Sm\CutomerNumberAttribute\Setup;

use Magento\Customer\Model\Customer;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements \Magento\Framework\Setup\InstallDataInterface
{
    private $eavSetupFactory;

    private $eavConfig;

    private $attributeResource;

    public function __construct(
        \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
        \Magento\Eav\Model\Config $eavConfig,
        \Magento\Customer\Model\ResourceModel\Attribute $attributeResource
    ) {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
        $this->attributeResource = $attributeResource;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->removeAttribute(Customer::ENTITY, "customer_number");

        $attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
        $attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);

        $eavSetup->addAttribute(Customer::ENTITY, 'customer_number', [
            // Attribute parameters
            'type' => 'varchar',
            'label' => 'Customer Number',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 500,
            'position' => 500,
            'system' => 0,
        ]);

        $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'customer_number');
        $attribute->setData('attribute_set_id', $attributeSetId);
        $attribute->setData('attribute_group_id', $attributeGroupId);

        /*
        //You can use this attribute in the following forms
        adminhtml_checkout
        adminhtml_customer
        adminhtml_customer_address
        customer_account_create
        customer_account_edit
        customer_address_edit
        customer_register_address
        */


        $attribute->setData('used_in_forms', [
            'adminhtml_customer',
            'customer_account_create',
            'customer_account_edit'
        ]);

        $this->attributeResource->save($attribute);
    }
}
?>

Thanks in Advance :)

1

1 Answer 1

1

Check below code

InstallData.php:- sets up data when the module is installed.

File:-Vendor/Module/Setup/InstallData.php

<?php

namespace Vendor\Module\Setup;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Customer\Model\CustomerFactory;

/**
 * Class InstallData
 */
class InstallData implements InstallDataInterface
{
    protected $customerFactory;
    protected $customerSetupFactory;
    protected $indexerRegistry;

    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        IndexerRegistry $indexerRegistry,
        CustomerFactory $customerFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->indexerRegistry = $indexerRegistry;
        $this->customerFactory = $customerFactory;
    }
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(Customer::ENTITY, 'customer_number', [
            'group' => 'General',
            'type' => 'static',
            'label' => 'Customer Number',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 100,
            'position' => 100,
            'used_in_grid' => true,
            'visible_in_grid' => true,
            'searchable_in_grid' => true,
            'filterable_in_grid' => true,
            'system' => 0,
        ]);

        $custNumberAttr = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_number');
        $custNumberAttr->setData(
            'used_in_forms',
            [
                'adminhtml_customer',
                'checkout_register',
                'customer_account_create',
                'customer_account_edit',
                'adminhtml_checkout'
            ]
        )->setData("is_used_for_customer_segment", true)
            ->setData("is_system", 0)
            ->setData("is_user_defined", 1)
            ->setData("is_visible", 1);

        $custNumberAttr->setData('is_used_in_grid', true);
        $custNumberAttr->setData('is_visible_in_grid', true);
        $custNumberAttr->setData('is_filterable_in_grid', true);
        $custNumberAttr->save();
        $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
        $indexer->invalidate();
    }
}

UpgradeData.php:- modifies data after the module is installed when the module version is upgraded

File:- Vendor/Module/Setup/UpgradeData.php

<?php

namespace Vendor\Module\Setup;

use Magento\Customer\Model\Customer;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Customer\Model\CustomerFactory;

/**
 * Class UpgradeData
 */
class UpgradeData implements UpgradeDataInterface
{
    protected $customerFactory;
    protected $customerSetupFactory;
    protected $indexerRegistry;

    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        IndexerRegistry $indexerRegistry,
        CustomerFactory $customerFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->indexerRegistry = $indexerRegistry;
        $this->customerFactory = $customerFactory;
    }

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        if ($context->getVersion()
            && version_compare($context->getVersion(), '2.0.1') < 0
        ) {
            $customerSetup->addAttribute(Customer::ENTITY, 'customer_number', [
                'type' => 'static',
                'label' => 'Customer Number',
                'input' => 'text',
                'required' => false,
                'visible' => true,
                'user_defined' => true,
                'sort_order' => 100,
                'position' => 100,
                'used_in_grid' => true,
                'visible_in_grid' => true,
                'searchable_in_grid' => true,
                'filterable_in_grid' => true,
                'system' => 0,
            ]);

            $custNumberAttr = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_number');
            $custNumberAttr->setData(
                'used_in_forms',
                ['adminhtml_customer', 'checkout_register', 'customer_account_create', 'customer_account_edit', 'adminhtml_checkout']
            )->setData("is_used_for_customer_segment", true)
                ->setData("is_system", 0)
                ->setData("is_user_defined", 1)
                ->setData("is_visible", 1);

            $custNumberAttr->setData('is_used_in_grid',true);
            $custNumberAttr->setData('is_visible_in_grid',true);
            $custNumberAttr->setData('is_filterable_in_grid',true);
            $custNumberAttr->save();
        }

        $setup->endSetup();
    }
}

output:-

enter image description here

I hope this will help you

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.