Magento 2 customer attribute not working in Magento 2.3.3
-
14-04-2021 - |
문제
I have created a module for adding a customer attribute and show it in create account form on frontend and account edit in backend, the thing is that it worked in Magento 2.2 but not on Magento 2.3.3, I don't know if it changes.
Here is my code.
InstallData.php
<?php
namespace Kage\RegisterForm\Setup;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
/**
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* @var \Magento\Eav\Model\Config
*/
private $eavConfig;
public function __construct(
EavSetupFactory $eavSetupFactory,
\Magento\Eav\Model\Config $eavConfig
) {
$this->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
}
/**
* Installs data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var \Magento\Eav\Setup\EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$setup->startSetup();
$attributeCode = 'register_zip_code';
$eavSetup->addAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'register_zip_code', [
'type' => 'text',
'label' => 'Zip Code',
'input' => 'text',
'required' => true,
'visible' => true,
'source' => '',
'backend' => '',
'user_defined' => true,
'is_user_defined' => false,
'sort_order' => 1000,
'is_used_in_grid' => false,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => false,
'is_searchable_in_grid' => false,
'position' => 1000,
'default' => 0,
'system' => 0,
]);
$eavSetup->addAttributeToSet(
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
null,
$attributeCode);
$registerZipCode= $this->eavConfig->getAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributeCode);
$registerZipCode->setData('used_in_forms', [
'adminhtml_customer',
'customer_account_create',
'customer_account_edit'
]);
$registerZipCode->getResource()->save($registerZipCode);
$setup->endSetup();
}
}
view/frontend/layout/customer_account_create.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="Magento\Framework\View\Element\Template" name="register_zip_code" template="Kage_RegisterForm::register.phtml"/>
</referenceContainer>
</body>
</page>
view/templates/register.phtml
<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>">
<legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Additional Information') ?></span></legend>
<p>
<div class="field required regulation">
<label for="regulation" class="label"><span><?php /* @escapeNotVerified */
echo __('Zip Code') ?></span></label>
<div class="control">
<input type="text" name="register_zip_code" id="register_zip_code" title="<?php /* @escapeNotVerified */ echo __('Zip Code') ?>" class="input-text" data-validate="{required:true}">
</div>
</div>
</p>
</fieldset>
해결책
I had same requirement for the barcode in magento 2.3.3 and its working.
Here is the code.
Module Name : Multiunique_Stackoverflow.
Create an InstallData.php at app/code/Multiunique/Stackoverflow/Setup/ and add below code.
<?php namespace Multiunique\Stackoverflow\Setup; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Customer\Setup\CustomerSetupFactory; use Magento\Customer\Model\Customer; use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet; use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; use Magento\Customer\Setup\CustomerSetup; class InstallData implements InstallDataInterface { /** * @var CustomerSetupFactory */ protected $customerSetupFactory; /** * @var AttributeSetFactory */ private $attributeSetFactory; /** * InstallData constructor. * * @param CustomerSetupFactory $customerSetupFactory * @param AttributeSetFactory $attributeSetFactory */ public function __construct( CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory ) { $this->customerSetupFactory = $customerSetupFactory; $this->attributeSetFactory = $attributeSetFactory; } /** * Install * * @param ModuleDataSetupInterface $setup * @param ModuleContextInterface $context * @throws \Magento\Framework\Exception\LocalizedException * @throws \Zend_Validate_Exception * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { /** @var CustomerSetup $customerSetup */ $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer'); $attributeSetId = $customerEntity->getDefaultAttributeSetId(); /** @var $attributeSet AttributeSet */ $attributeSet = $this->attributeSetFactory->create(); $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); $customerSetup->addAttribute( Customer::ENTITY, 'barcode', [ 'type' => 'varchar', 'label' => 'Barcode Customer', 'input' => 'text', 'required' => false, 'visible' => true, 'user_defined' => true, 'position' => 999, 'system' => 0, 'is_used_in_grid' => 1, 'is_visible_in_grid' => 1, 'is_filterable_in_grid' => 1, 'is_searchable_in_grid' => 1 ] ); $attribute = $customerSetup ->getEavConfig() ->getAttribute(Customer::ENTITY, 'barcode') ->addData( [ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId, 'used_in_forms' => ['adminhtml_customer', 'customer_account_edit','customer_account_create'] ] ); $attribute->save(); }
}
Create customer_account_create.xml at app/code/Multiunique/Stackoverflow/view/frontend/layout and add below code
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="form.additional.info"> <block class="Magento\Framework\View\Element\Template" name="customer_barcode" template="Multiunique_Stackoverflow::register.phtml"/> </referenceContainer> </body> </page>
add register.hml at app/code/Multiunique/Stackoverflow/view/frontend/templates and add below code
<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>"> <legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Additional Information') ?></span></legend> <p> <div class="field required barcode"> <label for="barcode" class="label"><span><?php /* @escapeNotVerified */ echo __('Barcode') ?></span></label> <div class="control"> <input type="text" name="barcode" id="barcode" title="<?php /* @escapeNotVerified */ echo __('Customer Barcode') ?>" class="input-text" data-validate="{required:true}"> </div> </div> </p> </fieldset>
let me know if it helps else i will share my custom module.
Thanks Amit