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

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();
    }
}
| improve this answer | |
  • 1
    Not working. And you're creating a new attribute, but i need to use the existing one... – Radosav Aug 31 '16 at 10:44
  • Is there a working solution now? – Kevin Krieger Dec 9 '16 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. – Zeeshan Khuwaja Aug 6 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.

| improve this answer | |
  • 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. – Mohit Kumar Arora Feb 25 '19 at 6:02

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.