2

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.

2
  • are you able to see those attribute in admin ?
    – Pawan
    Dec 14, 2018 at 17:46
  • I can see them in the admin customer grid, however when I click into the customer I do not see them anywhere
    – AJK
    Dec 14, 2018 at 17:48

2 Answers 2

0

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;
    }
}
6
  • Mind explaining what issue you found? I edited my original question -- I'm actually able to see my attributes in the admin customer grid, but not when I click into a customer's account.
    – AJK
    Dec 14, 2018 at 17:56
  • Find my updated answer. Dec 14, 2018 at 18:03
  • Thanks for your reply, I actually have that line in my code. I want these only visible in the admin account area and not on the front-end. However they are not showing my admin account area.
    – AJK
    Dec 14, 2018 at 18:08
  • You did not set customer_account_edit in used_in_forms. It will display created attribute in admin area. Dec 14, 2018 at 18:23
  • Thanks! I tried updating that line.. I removed Vender_Module from the setup_module in the db and re-ran bin/magento s:up cleared cache and all that but still not showing in the admin customer pages
    – AJK
    Dec 14, 2018 at 21:47
0

ended up solving this by just needing to set 'visible' => false to true on my attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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