Magento Expert Forum - Improve your Magento experience

Results 1 to 8 of 8

How To Add Customer Attribute In Magento 2

  1. #1
    [ Contributor ] Wajid Hussain's Avatar
    Join Date
    Nov 2014
    Posts
    204
    Thanks
    3
    Thanked 14 Times in 12 Posts

    Default How To Add Customer Attribute In Magento 2

    Many of us are facing problems in adding custom attributes to customer profile in Magento 2. Here is how to add a custom field in Magento 2. Thanks to sashas extension for providing easy solution on adding a custom field.

    There are many ways to add custom field for customers in Magento 2. Today we will focus on extension for Magento 2 which adds attribute to customer edit page at admin.

    To begin with you will need Magento 2 installed with demo data. For more information you can see the Magento 2 installation guide.

    Our step by step instruction allow you to create a successful attribute of Customer in admin panel.

    Read Complete Tutorial Here: http://arpatech.com/blog/add-custome...e-magento-2-x/

    View more threads in the same category:


  2. #2
    Junior Member
    Join Date
    May 2015
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default

    When Magento 2 was about to release, we realized that how the custom attribute adding process would have been revamped. With such a particular thing in the mind, we have taken some initiate actions, and now guiding you by creating a Magento 2 extension that will positively add custom customer attribute into customer edit page at admin.

    First of all, make sure that you have installed Magento 2 with Demo data. You can take installation guidance from Magento 2 Developer Documentation (http://devdocs.magento.com/guides/v2.../continue.html).

    Start to Create Magento 2 Extension Framework
    At the very first moment, the extension files will be accessible at the app/code/ {Company_Name}/{Extension_Name} site. Where {Company_Name} and {Extension_Name} our values. Similarly, the way it was in Magento 1.x.p>

    In our extension, we are using company name Eecom and Extension name CustomAttribute. Once, after creating the folders, our extension can be accessed through this path, app/code/Eecom/CustomAttribute/

    The structure of the folder contained by extension folder is extremely similar to Magento 1.x. Folders like Controller, Helper, etc, Block and Model all are quite the same. However, in our case, the final look of our folder for our Magento 2 extension will seem like below:

    As we move on to the next, we need to make module.xml file for our extension in order to declare version as well as to make it observable for Magento 2.

    In Magento 1.x., the module file at app/etc/modules folder and now in new version, it has been replaced with the module.xml file. Now, all files associated with our extension, have been displayed in a single folder.

    Magento 2 Custom Customer Attribute Module Files
    The very first file, we require to create is – app/code/Eecom/CustomAttribute/etc/module.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <!--

    /**

    * @author Eecom

    * @category Eecom

    * @package Eecom_CustomAttribute

    * @copyright Copyright (c) 2016 Eecom IT Support Inc. (https://www.envisionecommerce.com/store/)

    */

    -->

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">

    <module name="Eecom_CustomAttribute" setup_version="1.0.0">

    <sequence>

    <module name="Magento_Customer"/>

    </sequence>

    </module>

    </config>


    For more details, check out this blog: https://www.envisionecommerce.com/ho...e-in-magento-2/

  3. #3
    Junior Member
    Join Date
    Oct 2016
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I added a customer custom attribute as customer_address type and it runs correctly in the admin and in onepagecheckout, both in shipping & billing address.

    I created: my_namespace/my_module/etc/module.xml

    my_namespace/my_module/Setup/InstallData.php

    namespace Namespace\Module\Setup;

    use Magento\Framework\Module\Setup\Migration;
    use Magento\Framework\Setup\InstallDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;

    /**
    * @codeCoverageIgnore
    */
    class InstallData implements InstallDataInterface
    {
    /**
    * Customer setup factory
    *
    * @var CustomerSetupFactory
    */
    private $customerSetupFactory;

    /**
    * Init
    *
    * @param CustomerSetupFactory $customerSetupFactory
    */
    public function __construct(\Magento\Customer\Setup\CustomerSetupF actory $customerSetupFactory)
    {
    $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
    * {@inheritdoc}
    * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
    */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
    /** @var CustomerSetup $customerSetup */
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    $setup->startSetup();

    // insert attribute
    $customerSetup->addAttribute('customer_address', 'attr_code', [
    'label' => 'My attribute',
    'type' => 'varchar',
    'input' => 'text',
    'position' => 45,
    'visible' => true,
    'required' => false,
    'system' => 0
    ]);

    $MyAttribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'attr_code');
    $MyAttribute->setData(
    'used_in_forms',
    ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
    );
    $MyAttribute->save();

    $setup->endSetup();
    }
    }
    also added, in my module base directory, registration.php and composer.json files.

    Now I need to add my attribute field in the customer add and edit address form that are related to the file magento_customer/view/frontend/templates/address/edit.phtml

    I added the field but i'm not able to get and save the value of that attribute.

    An help will be really appreciated.

  4. #4
    Junior Member
    Join Date
    Dec 2016
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How To Add Customer Attribute In Magento 2?

    The main part is DataInstall::install method

    Code:
    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, '{attributeCode}', [
                'type' => 'varchar',
                'label' => '{attributeLabel}',
                'input' => 'text',
                'required' => false,
                'visible' => true,
                'user_defined' => true,
                'sort_order' => 1000,
                'position' => 1000,
                'system' => 0,
            ]);
            //add attribute to attribute set
            $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'magento_username')
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer'],
            ]);
    
            $attribute->save();
    
    
        }

  5. #5
    Senior Member
    Join Date
    Aug 2018
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default

    This article will guide you how to create a add customer attribute in Magento 2 programmatically. Please follow our previous article to create a simple module which we will use to demo coding for this lesson and how to create the setup script classes. In this article, we will use the sample module Mageplaza_HelloWorld and the InstallDataclass.

  6. #6
    Junior Member
    Join Date
    Nov 2019
    Posts
    1,083
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default

    Hello, flocks!

    You can use Customer Attribute Magento 2 Extension that allows adding additional attributes to collect more information about customers.

    With the help of this tool, the store owners can create additional fields on the customer account register page, customer account edit page, and admin manage customers. Using this fabulous tool you can easily collect more customers' data which you can utilize in building a strong marketing strategy.

    Highlighted Features:
    • Easy To Install
    • Fully Responsive
    • Manage All Customer Attributes
    • Enable/Disable From Backend
    • Create Unlimited Additional Fields
    • Multiple Options For Attribute Display
    • Set Input Validation For Attributes
    • Default Value For The Attributes
    • Attribute Information On The Grid



    Name:  Customer-Attribute-Edit-Account-Information.png
Views: 1155
Size:  60.4 KB

  7. #7
    Junior Member
    Join Date
    Sep 2018
    Location
    United Kingdom
    Posts
    228
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default

    Use Magento 2 Customer Attributes to adding extra attribute fields to collect valuable customer information on registration or account page.
    ...
    Step 1: Create setup file InstallData. php. ...
    Step 2: Define the install() method. ...
    Step 3: Create custom attribute.

  8. #8
    Junior Member
    Join Date
    Aug 2018
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Customer Attribute Magento 2 extension displays additional attribute fields on the registration page to collect important customer data. As much information as you need can be effectively gathered by creating customer attributes of various types.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •