Magento2 – Custom Customer Attributes Showing in Customer Grid but Not Admin Account

attributescustomer-attributemagento2magento2.2.6PHP

I'm currently on Magento 2.2.6 and I created a multiple select attribute as well as a yes/no attribute for customers in the admin page, however I'm not seeing them show up and not sure where I went wrong.

../Vendor/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
  <module name="Vendor_Module" schema_version="0.0.1" setup_version="0.0.1" active="true"/>
</config>

Next I have my Setup/InstallData.php which calls my Setup/CustomerSetup.php file

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
{
  public function __construct(EavSetupFactory $eavSetupFactory)
  {
    $this->eavSetupFactory = $eavSetupFactory;
  }

  public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
  {

    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    if (version_compare($context->getVersion(), '1.0.0') < 0){

      $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
      $customerSetup = $objectManager->create('Vendor\Module\Setup\CustomerSetup');
      $customerSetup->installAttributes($customerSetup);
    }
  }
}

CustomerSetup.php

<?php

namespace Vendor\Module\Setup;

use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Setup\Context;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;

class CustomerSetup extends EavSetup {

  protected $eavConfig;

  public function __construct(
    ModuleDataSetupInterface $setup,
    Context $context,
    CacheInterface $cache,
    CollectionFactory $attrGroupCollectionFactory,
    Config $eavConfig
  ) {
    $this->eavConfig = $eavConfig;
    parent :: __construct($setup, $context, $cache, $attrGroupCollectionFactory);
  }

  public function installAttributes($customerSetup) {
    $this->installCustomerAttributes($customerSetup);
    $this->installCustomerAddressAttributes($customerSetup);
  }

  public function installCustomerAttributes($customerSetup) {


    $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY,
    'branding',
    [
      'label' => 'Branding',
      'system' => 0,
      'position' => 100,
      'sort_order' =>100,
      'visible' =>  false,
      'note' => '',
      'is_used_in_grid' => true,
      'is_visible_in_grid' => true,
      'is_filterable_in_grid' => true,
      'is_searchable_in_grid' => true,

      'type' => 'int',
      'input' => 'boolean',
      'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',

    ]
  );

  $customerSetup->getEavConfig()->getAttribute('customer', 'branding')->setData('is_user_defined',0)->setData('is_required',0)->setData('default_value','0')->setData('used_in_forms', ['adminhtml_customer'])->save();



  $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY,
  'branding_type',
  [
    'label' => 'Branding Type',
    'system' => 0,
    'position' => 101,
    'sort_order' =>101,
    'visible' =>  false,
    'note' => '',
    'is_used_in_grid' => true,
    'is_visible_in_grid' => true,
    'is_filterable_in_grid' => true,
    'is_searchable_in_grid' => true,

    'type' => 'varchar',
    'input' => 'multiselect',
    'source' => 'Vendor\Module\Model\Entity\Attribute\Source\Options',
    'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
  ]
);

$customerSetup->getEavConfig()->getAttribute('customer', 'branding_type')->setData('is_user_defined',0)->setData('is_required',0)->setData('default_value','')->setData('used_in_forms', ['adminhtml_customer'])->save();


}

    public function installCustomerAddressAttributes($customerSetup) {

    }

    public function getEavConfig() {
      return $this -> eavConfig;
    }
}

And then lastly, I have my Multi Select options here:
Vendor\Module\Model\Entity\Attribute\Source\Options.php

Options.php

<?php

namespace Vendor\Module\Model\Entity\Attribute\Source;

use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;

class Options extends AbstractSource
{
  public function getAllOptions()
  {
    return [
      'option1' => [
        'label' => 'Custom Model Numbers',
        'value' => 'custom_model_numbers'
      ],
      'option2' => [
        'label' => 'Camera Logo Stamp',
        'value' => 'camera_stamp'
      ],
      'option3' => [
        'label' => 'Camera GUI',
        'value' => 'camera_gui'
      ],
      'option4' => [
        'label' => 'Recorder Faceplate Decals',
        'value' => 'recorder_decals'
      ],
      'option5' => [
        'label' => 'Recorder GUI',
        'value' => 'recorder_gui'
      ],
      'option6' => [
        'label' => '2GIG Faceplate Stamp',
        'value' => '2gig_faceplate'
      ]
    ];
  }
}

After this I ran php bin/magento s:up and php bin/magento s:s:d -f and made sure to clear cache and reindex but nothing is showing up.

Any ideas?

EDIT, I should add that I can actually see my attributes in the admin customer grid, however they are not visible when I view the customer's account in the admin.

Best Answer

Used_in_forms are of these types you can use forms key according to your need

['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address', 'customer_account_create']

In your case, just set customer_account_edit where you already set adminhtml_customer.

$customerSetup->getEavConfig()->getAttribute('customer', 'branding')
    ->setData('is_user_defined', 0)
    ->setData('is_required', 0)
    ->setData('default_value', '0')

    ->setData('used_in_forms', ['adminhtml_customer', 'customer_account_edit'])
    ->save();

CustomerSetup.php

namespace Vendor\Module\Setup;

use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Setup\Context;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;

class CustomerSetup extends EavSetup {

    protected $eavConfig;

    public function __construct(
        ModuleDataSetupInterface $setup,
        Context $context,
        CacheInterface $cache,
        CollectionFactory $attrGroupCollectionFactory,
        Config $eavConfig
    ) {
        $this->eavConfig = $eavConfig;
        parent :: __construct($setup, $context, $cache, $attrGroupCollectionFactory);
    }

    public function installAttributes($customerSetup) {
        $this->installCustomerAttributes($customerSetup);
        $this->installCustomerAddressAttributes($customerSetup);
    }

    public function installCustomerAttributes($customerSetup) {


        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY,
            'branding',
            [
                'label' => 'Branding',
                'system' => 0,
                'position' => 100,
                'sort_order' =>100,
                'visible' =>  false,
                'note' => '',
                'is_used_in_grid' => true,
                'is_visible_in_grid' => true,
                'is_filterable_in_grid' => true,
                'is_searchable_in_grid' => true,

                'type' => 'int',
                'input' => 'boolean',
                'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',

            ]
        );

        $customerSetup->getEavConfig()->getAttribute('customer', 'branding')->setData('is_user_defined',0)->setData('is_required',0)->setData('default_value','0')->setData('used_in_forms', ['adminhtml_customer', 'customer_account_edit'])->save();



        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY,
            'branding_type',
            [
                'label' => 'Branding Type',
                'system' => 0,
                'position' => 101,
                'sort_order' =>101,
                'visible' =>  false,
                'note' => '',
                'is_used_in_grid' => true,
                'is_visible_in_grid' => true,
                'is_filterable_in_grid' => true,
                'is_searchable_in_grid' => true,

                'type' => 'varchar',
                'input' => 'multiselect',
                'source' => 'Vendor\Module\Model\Entity\Attribute\Source\Options',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            ]
        );

        $customerSetup->getEavConfig()->getAttribute('customer', 'branding_type')->setData('is_user_defined',0)->setData('is_required',0)->setData('default_value','')->setData('used_in_forms', ['adminhtml_customer'])->save();


    }

    public function installCustomerAddressAttributes($customerSetup) {

    }

    public function getEavConfig() {
        return $this -> eavConfig;
    }
}
Related Topic