Magento Expert Forum - Improve your Magento experience

onet connect
Results 1 to 4 of 4

How To Add Customer Attribute In Magento 2

  1. #1
    [ Contributor ] Wajid Hussain's Avatar
    Join Date
    Nov 2014
    Posts
    197
    Thanks
    3
    Thanked 6 Times in 6 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
    34
    Thanks
    0
    Thanked 0 Times in 0 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
    88
    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;
    }

    /**
    * [email protected]}
    * @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.
    Optomize your OnPage and Get High Rank Google With - this toolkit - Serplify Pro Review

    Igloo Page Builder Create your Landing page and Sales Page for Page Builder - Buy it and Get Special Gift
    Feedback software for Small business- Convertifire Review - Bonus Free

  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();
    
    
        }

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
  •