5

I want to add a company field to the customer registration form, but i can not find the backend option for it. Is there a way i can do it programmatically?

enter image description here

2 Answers 2

3

Try this script.

<?php
namespace Mynamespace\Modulename\Setup;

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

class InstallData implements InstallDataInterface
{
    /**
     * Customer setup factory
     *
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;
    /**
     * Init
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }
    /**
     * Installs DB schema for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        $installer = $setup;
        $installer->startSetup();

        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);
        $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "company");

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "company",  array(
            "type"     => "varchar",
            "backend"  => "",
            "label"    => "company",
            "input"    => "text",
            "source"   => "",
            "visible"  => true,
            "required" => true,
            "default" => "",
            "frontend" => "",
            "unique"     => false,
            "note"       => ""

        ));

        $company   = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "company");

        $company = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'company');
        $used_in_forms[]="adminhtml_customer";
        $used_in_forms[]="checkout_register";
        $used_in_forms[]="customer_account_create";
        $used_in_forms[]="customer_account_edit";
        $used_in_forms[]="adminhtml_checkout";
        $company->setData("used_in_forms", $used_in_forms)
            ->setData("is_used_for_customer_segment", true)
            ->setData("is_system", 0)
            ->setData("is_user_defined", 1)
            ->setData("is_visible", 1)
            ->setData("sort_order", 100);

        $company->save();



        $installer->endSetup();
    }
}
3
  • 1
    Not working. And you're creating a new attribute, but i need to use the existing one...
    – Radosav
    Aug 31, 2016 at 10:44
  • Is there a working solution now? Dec 9, 2016 at 8:08
  • @Radosav there is no out of the box solution for it, Company field is only added to the customer_address entity type not customer, so you have to create custom attribute for it to work on customer entity type. Aug 6, 2020 at 6:03
-1

This should be an easy and taken as a serious inquiry. Additional customer registration data is key for sorting and marketing to different customer groups.

1
  • This does not provide an answer to the question. I would request to please explain a bit more so that the answer can benefit someone. Feb 25, 2019 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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