0

Scenario

I'm trying to implement a custom attribute for Magento Customer, that should accept boolean values (True/False, Yes/No...).
I'm using Magento CE 2.2.4.
This is part of a custom Module under /app/code/TheVendor_TheModule/.
The other components of the Module are working correctly.


Expected Result

  • The attribute must be represented with a switch input or checkbox in back-end Customer form.
  • The attribute and its values must appear in Customers Grid
  • The attribute must appear in the Filter with selectable options (Yes/No or True/False or Is/Is not, any Boolean-like value works fine)

Actual Result

  • [OK] A switch shows in the back-end within the Customer form as expected.
  • [OK] Changing the switch value to on or off + saving works fine.
  • [Issue] The attribute's Label shows in the Customer Grid but the values are missing.
  • [Issue] The attribute's input in the Filter shows but does not contain options.

Screens

Customer Form View in back-end

Customer Form View

Customer Grid and Filter View

Customer Grid and Filter View


Code

Content of app/code/TheVendor/TheModule/Setup/InstallData.php

<?php
namespace TheVendor\TheModule\Setup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface {
 const ATTRIBUTE_APPROVED = 'attribute_approved';
 protected $customerSetupFactory;
 private $eavSetupFactory;
 private $eavConfig;
 private $attributeResource;
 public function __construct(
 CustomerSetupFactory $customerSetupFactory, 
 EavSetupFactory $eavSetupFactory, 
 Config $eavConfig, 
 \Magento\Customer\Model\ResourceModel\Attribute $attributeResource
 ){
 $this->eavSetupFactory = $eavSetupFactory;
 $this->eavConfig = $eavConfig;
 $this->customerSetupFactory = $customerSetupFactory;
 $this->attributeResource = $attributeResource;
 }
 public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
 {
 $setup->startSetup();
 /** @var CustomerSetup $customerSetup */
 $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
 $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, self::ATTRIBUTE_APPROVED, [
 'type' => 'int',
 'label' => 'Attribute Approved',
 'input' => 'boolean',
 'required' => false,
 'visible' => true,
 'system' => false,
 'position' => 9,
 'sort_order' => 9,
 'is_used_in_grid' => true,
 'is_visible_in_grid' => true,
 'is_filterable_in_grid' => true,
 'is_searchable_in_grid' => true,
 //'user_defined' => true, //commented because causing attribute fail on module install
 //'searchable' => true,
 'filterable' => true,
 'comparable' => true,
 'default' => '0',
 //'unique' => 0,
 ]);
 $myAttribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, self::ATTRIBUTE_APPROVED);
 $myAttribute->setData('used_in_forms', ['adminhtml_customer']);
 $this->attributeResource->save($myAttribute);
 $setup->endSetup();
 }
}

Attempts and Tests

I tried the following:

  • Lookup solution in Magento Dev Docs
  • Lookup solution on StackExchange
  • Lookup solution on other forums
  • Tweaking $customerSetup->addAttribute(...) options:
    • Set 'user_defined' => true. When used, this one is causing the attribute setup to fail without errors.
    • Set 'default' => 0 and 'default' => '0'
    • Set 'searchable' => true
  • Checked logs for errors, none found.
  • Removed the Module folder and create it again before reinstall
  • Executed php bin/magento setup:di:compile
  • Executed php bin/magento setup:static-content:deploy -f

Testing Routine

For every test that I made I followed these steps to ensure Module is being installed correctly:

  • Execute php bin/magento module:disable TheVendor_TheModule
  • Remove records from database:
    • Delete Module record in mage_setup_module
    • Delete EAV record in mage_eav_attribute
  • Make sure Module is disabled in app/etc/config.php
  • Pull updated code
  • Execute php bin/magento module:enable TheVendor_TheModule
  • Execute php bin/magento setup:upgrade
  • Execute php bin/magento indexer:reindex
  • Execute php bin/magento cache:clean

Question

Anyone with suggestions on how to handle this or how to detect where the problem is coming from?

asked Jul 17, 2018 at 23:32

1 Answer 1

0

Problem Solved

Solution:

Edit addAttribute(...) options in app/code/TheVendor/TheModule/Setup/InstallData.php

Use source model 'Magento\Eav\Model\Entity\Attribute\Source\Boolean' with input select

...
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, self::ATTRIBUTE_APPROVED, [
 'type' => 'int',
 'label' => 'Attribute Approved',
 /** [Solution] Changed from 'boolean' to 'select' */
 'input' => 'select', 
 /** [Solution] Use source model Boolean */
 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean', 
 'default' => '0',
 'required' => false,
 'visible' => true,
 'system' => false,
 'position' => 9,
 'sort_order' => 9,
 //'user_defined' => true,
 //'searchable' => true,
 'filterable' => true,
 'comparable' => true,
 'is_used_in_grid' => true,
 'is_visible_in_grid' => true,
 'is_filterable_in_grid' => true,
 'is_searchable_in_grid' => true,
 //'unique' => 0,
 ]); 
...

Screen

Success Screen

Hope this helps!

answered Jul 18, 2018 at 11:47
1
  • I am trying to create in magento 2.3.5, but its not working. Commented Mar 27, 2021 at 21:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.