0

i have created custom customer attributes, it will working fine in customer registration form but not shown at checkout register page in Magento 2

0

customer attributes are display in the customer registration page, but address fields are display in the both customer registration and checkout page.

Customer attribute feature is a default feature in magento 2 EE. Stores -> Atrributes

enter image description here

For magento 2 CE have to create attributes using custom module, please follow bellow url https://www.mageplaza.com/magento-2-module-development/magento-2-add-customer-attribute-programmatically.html

as a resource I have added tutorial content here, this for create customer attribute but question is why some attributes didn't display in checkout.


This article will guide you how to create a add customer attribute in Magento 2 programatically. 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.

Overview of Adding Customer Attribute Programmatically

  • Step 1: Create setup file InstallData.php
  • Step 2: Define the install() method
  • Step 3: Create custom attribute

Step 1: Create setup file InstallData.php

Firstly, we will create the InstallData.php file:

File: app/code/Mageplaza/HelloWorld/Setup/InstallData.php

<?php
namespace Mageplaza\HelloWorld\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

}

In this class, we define the EAV setup model which will be use to interact with Magento 2 attribute.

Step 2: Define the install() method

After that, we have to define the install() method and create eav setup model:

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    }
    // Next, we will use eavSetup object to add attribute:
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'sample_attribute',
            [
                'type'         => 'varchar',
                'label'        => 'Sample Attribute',
                'input'        => 'text',
                'required'     => false,
                'visible'      => true,
                'user_defined' => true,
                'position'     => 999,
                'system'       => 0,
            ]
        );
}

Step 3: Create custom attribute

Finally, we need to set the forms in which the attributes will be used. In this step, we need define the eavConfig object which allow us to call the attribute back and set the data for it. And the full code to create customer attribute is:

File: app/code/Mageplaza/HelloWorld/Setup/InstallData.php

<?php

namespace Mageplaza\HelloWorld\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;
use Magento\Customer\Model\Customer;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig       = $eavConfig;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'sample_attribute',
            [
                'type'         => 'varchar',
                'label'        => 'Sample Attribute',
                'input'        => 'text',
                'required'     => false,
                'visible'      => true,
                'user_defined' => true,
                'position'     => 999,
                'system'       => 0,
            ]
        );
        $sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'sample_attribute');

        // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
        $sampleAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer']

        );
        $sampleAttribute->save();
    }
}

Now, let run comman line to install the module:

bin/magento setup:upgrade

and

php bin/magento setup:static-content:deploy

Then check the result. It will show like this:

enter image description here

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review – 7ochem May 15 '18 at 9:26
  • @7ochem - Your opinion is add the tutorial code here ? – Lasantha May 15 '18 at 9:32
  • always include any relevant code and explanations. The website you are referring to might be taken offline, or replaced or the article itself might be removed. If that will happen, then your answer has no value anymore for future readers. – 7ochem May 15 '18 at 9:36
  • @7ochem - I have added the tutorial here.it'll help to developers who want to create custom customer attributes programatically. – Lasantha May 15 '18 at 9:46
  • I also have a same issue if u get any solution for this – divya sekar 2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.