Magento 2.1.3 Make Customer Attribute [Update]

Magento 2.1.3 How To Make Customer Attribute

We had the post for pre-release Magento 2 – “How to make customer attribute?“. After this time Magento has several updates and we did update of files according to the latest Magento 2.1.3 release. The more detailed information can be found in the previous article.

Update For Magento 2 How To Make Customer Attribute

This article will show updated codebase accordign with Magento 2.1.3 upgrade.
Also, Magento 2.1.x has update to Magento 2 CLI commands, you can look them at out previous article: “The Top 7 Magento 2 CLI Commands You Should Know”

Updates to the files from previous article:

There are 3 files. One more file added. Please don’t forget to make it. It allows extension to be visible for Magento 2.
app/code/Sashas/CustomerAttribute/etc/module.xml

app/code/Sashas/CustomerAttribute/Setup/InstallData.php

app/code/Sashas/CustomerAttribute/registration.php

Updates for Magento 2 CLI commands

The example of commands shown for a store with developer mode enabled. If you are using production mode please perform additional commands for dependency injection, static content deploy, clean cache and reindex indexers. You can find these commands at the Our Magento 2 CLI Article
After you created files you will need to run following commands:
php bin/magento setup:upgrade
php bin/magento cache:flush (if you had cache enabled)
php bin/magento indexer:reindex

In case any of Magento 2 CLI commands have error please check permissions of bin/magento and path to php

Result: New Custom Customer Attribute At Magento 2

As you can see in the code we set that attribute should be used in forms:
adminhtml_customer
Magento 2 How To Make Customer Attribute

If don’t you want to use it at the Customer Edit and Customer Registration Page then you need to remove customer_account_edit and customer_account_create (Line 75)
Instead of [‘adminhtml_customer’, ‘customer_account_edit’, ‘customer_account_create’] it will be: [‘adminhtml_customer’]

In another case if you want to use customer attribute at the Customer Edit and Customer Registration Page then you will need to rewrite .phtml templates for these forms at the:
vendor/magento/module-customer/view/frontend/templates/form/edit.phtml
and
vendor/magento/module-customer/view/frontend/templates/form/register.phtml

To achieve this you need to make folder structure at your theme app/design/frontend/{theme_company}/{theme_name}/Magento_Customer/templates/form/
and copy files above there. After this, you can add fields similar way as Magento have in the default template.
The input field name will be our custom attribute code.

Conclusion

After Magento perform several updates the old article code does not work anymore, but article has explanation of code and video were shown how to make it. As for now when you going to make custom customer attribute for Magento 2.1.x versions please use the code above as well as Magento 2 CLI commands.

I hope this information will help you to create custom attributes without any issues. In case you still have troubles please post it in comments section and I will try to help you.

23 thoughts on “Magento 2.1.3 Make Customer Attribute [Update]

  1. Sergio

    About works in frontend??????
    This plugin is good, but it is incomplete this way.

    ‘used_in_forms’ => [‘customer_account_create’,’adminhtml_checkout’,’adminhtml_customer’]

    Its does not work alone.
    Someone now hoe can we do this?

    Reply
  2. Arpeggiatus

    I am getting the following error for Magento 2.2.1:

    [:error] [pid 3429] [client ::1:53036] PHP Fatal error: Uncaught TypeError: Argument 2 passed to Magento\\Framework\\View\\Element\\UiComponentFactory::argumentsResolver() must be of the type array, null given, called in /var/www/html/magento2/vendor/magento/framework/View/Element/UiComponentFactory.php on line 212 and defined in /var/www/html/magento2/vendor/magento/framework/View/Element/UiComponentFactory.php:164\nStack trace:\n#0 /var/www/html/magento2/vendor/magento/framework/View/Element/UiComponentFactory.php(212): Magento\\Framework\\View\\Element\\UiComponentFactory->argumentsResolver(‘dni_usuario’, NULL)\n#1 /var/www/html/magento2/vendor/magento/module-ui/Component/Form/Field.php(84): Magento\\Framework\\View\\Element\\UiComponentFactory->create(‘dni_usuario’, ‘integer’, Array)\n#2 /var/www/html/magentoto2/vendor/magento/framework/View/Layout/Generator/UiComponent.php(159): Magento\\Ui\\Component\\Form\\Field->prepare()\n#3 /var/www/html/magento/framework/View/Layout/Generator/UiComponent.php(156): Magento\\Framework\\View\\Layout\\Generator\\UiComponent->prepareCom in /var/www/html/magento2/vendor/magento/framework/View/Element/UiComponentFactory.php on line 164, referer: http://localhost/admin/customer/index/index/key/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/

    Help please!

    Reply
  3. Artur

    Hello,

    I followed the instruction and I can see module being installed and Enabled, but I don’t see new attributes in Admin or front-end. Any ideas why? I am using Magento 2.1.7. Thanks!

    Reply
  4. David

    Hello,
    Great code, thank you.
    It worked fine, but not anymore since I updated from 2.1.7 to 2.1.9.

    The form is not working anymore.

    The page reloads, but nothing happens, no error or success message and customer is not being registered.

    When I delete theme app/design/frontend/{theme_company}/{theme_name}/Magento_Customer/templates/form/register.phtml, the registration works fine.

    I also checked the new vendor/magento/module-customer/view/frontend/templates/form/register.phtml file, and saw no change in latest version.

    The problem does not seem to come from theme app/design/frontend/{theme_company}/{theme_name}/Magento_Customer/templates/form/register.phtml.

    If I only copy
    vendor/magento/module-customer/view/frontend/templates/form/register.phtml
    to
    theme app/design/frontend/{theme_company}/{theme_name}/Magento_Customer/templates/form/register.phtml
    without making any changes, I have the same problem.

    Looks like the file does not « connect » with files located in app/code/Sashas/CustomerAttribute/ anymore .. ?

    I tried to setup:upgrade and setup:di:compile after Magento update, but no change.

    I can’t seem to find any changes made in customer registration files in versions 2.1.8 or 2.1.9.

    Any idea on how to fix this ?

    Best regards,
    David

    Reply
  5. Baovic

    I add customer attribute and show front-end.
    $magentoUsernameAttribute->addData([
    ‘attribute_set_id’ => $attributeSetId,
    ‘attribute_group_id’ => $attributeGroupId,
    ‘used_in_forms’ => [‘adminhtml_customer_address’, ‘customer_address_edit’,’customer_address_form’],
    ]);
    design/frontend/{theme}/{theme}/Magento_Customer/templates/address/edit.html
    I add new field input:

    <input type="text" name="baovic" value="escapeHtml($block->getAddress()->getBaovic()) ?>” title=”” class=”input-text helper(‘Magento\Customer\Helper\Address’)->getAttributeValidationClass(‘baovic’) ?>” id=”baovic”>

    But this code is not working: $block->getAddress()->getBaovic()
    Please help. Thank you so much.

    Reply
  6. Vic

    Hello,
    I have a problem like this.
    I applied the front-end display:
    ————————-
    $magentoUsernameAttribute->addData([
    ‘attribute_set_id’ => $attributeSetId,
    ‘attribute_group_id’ => $attributeGroupId,
    ‘used_in_forms’ => [‘adminhtml_customer_address’, ‘customer_address_edit’,’customer_address_form’],
    ]);
    ————————-
    app/design/frontend/{theme_company}/{theme_name}/Magento_Customer/templates/address/edit.html, I add new field:
    ——————-
    <input type="text" name="baovic" value="escapeHtml($block->getAddress()->getBaovic()) ?>” title=”” class=”input-text helper(‘Magento\Customer\Helper\Address’)->getAttributeValidationClass(‘baovic’) ?>” id=”baovic”>
    ——————
    But this code is not working: $block->getAddress()->getBaovic()

    Please help.
    Thank you so much.

    Reply
  7. baovic

    I click save customer but show errow, please help me:
    “The value of attribute “magento_username” must be set”

    Thank you!

    Reply
  8. Tanmay Mishra

    Hi I tried including my custom attribute in the customer account edit form but it is not showing up, the custom attribute is visible in the admin form in customer module.

    I am sending you te code that I used for it. Please advice !!!

    1 . \app\code\Ct\Avatar\view\frontend\Magento\luma\Magento_Customer\layout\customer_account_edit.xml

    2. \app\code\Ct\Avatar\view\frontend\Magento\luma\Magento_Customer\template\additionalinfocustomer.phtml

    <input type="file" name="avatar" id="customer-avatar" title="” class=”input-text” autocomplete=”off” value=”getCustomer()->getAvatar() ?>”>

    Regards
    Tanmay

    Reply
    1. Tanmay Mishra

      Your comment is awaiting moderation.

      Hi I tried including my custom attribute in the customer account edit form but it is not showing up, the custom attribute is visible in the admin form in customer module.

      I am sending you te code that I used for it. Please advice !!!

      1 . \app\code\Ct\Avatar\view\frontend\Magento\luma\Magento_Customer\layout\customer_account_edit.xml

      2. \app\code\Ct\Avatar\view\frontend\Magento\luma\Magento_Customer\template\additionalinfocustomer.phtml

      <input type="file" name="avatar" id="customer-avatar" title="” class=”input-text” autocomplete=”off” value=”getCustomer()->getAvatar() ?>”>

      Regards
      Tanmay

      Reply
    1. Christian Freihoff

      Hi,
      I am fighting the full Sunday with your code. Am am using Magento 2.1.9 with PHP 7.0.22.
      Your code is doing NOTHING. If I but a type in it, I get an error. Therefore I know it is executed.
      I turned on every possible debug and found NO entries.
      Absolutely nothing happens. I looked into the DB and NO attribute shows up.
      Please help!

      Reply
  9. FrankTheTank

    Hi there,

    the above code adds the attribute to the adminhtml form. BUT when I try to save a value in the field, it does not save the value – the field is empty again after save.

    Reply
  10. Pingback: Magento 2: How To Make Customer Attribute ? | Magento Extensions

Leave a Reply

Your email address will not be published. Required fields are marked *