0

I am using below code for adding customer attribute in admin form with some required value.

    $installer = $setup;
    $installer->startSetup();
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    $entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);

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

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

    $company_name = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'company_name');
    $used_in_forms[]="adminhtml_customer";
    $company_name->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_name->save();


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

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

    $company_registration = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY,'company_registration');
    $used_in_forms  = array(); 
    $used_in_forms[]="adminhtml_customer";
    $company_registration->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", 110);
    $company_registration->save();


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

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

    $gst_no = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY,'gst_no');
    $used_in_forms  = array(); 
    $used_in_forms[]="adminhtml_customer";
    $gst_no->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", 110);
    $gst_no->save();


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

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

    $company_address = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY,'company_address');
    $used_in_forms  = array(); 
    $used_in_forms[]="adminhtml_customer";
    $company_address->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", 110);
    $company_address->save();



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

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

    $contact_number = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY,'contact_number');
    $used_in_forms  = array(); 
    $used_in_forms[]="adminhtml_customer";
    $contact_number->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", 110);
    $contact_number->save();

    $installer->endSetup();

But It gives me error of required values in registration form. I don't want these fields in registration forms.

Please help me to solve this.

1 Answer 1

0

You have to use setvisible to 0

->setData("is_visible", 0)

Try this

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


$installer->addAttribute("customer", "facebook",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Facebook",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => ""

    ));

        $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "facebook");


$used_in_forms=array();

$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";
        $attribute->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", 0)
        ->setData("sort_order", 100)
        ;
        $attribute->save();



$installer->endSetup();
2
  • Thanks for your reply. Can you please let me know what is diffrence between this script ? Commented Jun 7, 2017 at 9:52
  • i have updated my answer.please mark correct to my answer if it is useful for you.thanks
    – Ashvini K
    Commented Jun 7, 2017 at 10:13

Your Answer

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

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