0

I created a new module to add a extra customer attribute.. but it only appear in admin site(customers/all customers/edit/Account information). But i want it into customer registration form... how i can do it ? im using: frontend_customer and adminhtml_customer

Socialme/IdInput/Setup/InstallData.php

<?php

namespace Socialme\IdInput\Setup;


use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;



/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{

    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }


    /**
     * {@inheritdoc}
     */
    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, 'id_number', [
            'type' => 'varchar',
            'label' => 'ID Number',
            'input' => 'text',
            'required' => true,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'id_number')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['frontend_customer','adminhtml_customer'],
        ]);

        $attribute->save();


    }
}

Socialme/IdInput/view/frontend/templates/customer/form/register.phtml

<div class="field required">
            <label for="id_number" class="label"><span><?php echo __('ID Number')?></span></label>
            <input type="text" name="id_number" id="id_number" title="id_number" class="input-text"  value="<?php echo $block->escapeHtml($block->getFormData()->getIdNumber()) ?>" required">
        </div>

2 Answers 2

1

You can try with used_in_forms value,

customer_account_create

You have to change in your php file with below value,

'used_in_forms' => ['customer_account_create','adminhtml_checkout','adminhtml_customer'],

Thanks.

3
  • hi, i added customer_account_create to my InstallData.php... i used magento cmd line bin/magento setup:upgrade to upgrade the new module value... but nothing happened.. can you help me with that?
    – user41058
    Jun 21, 2016 at 16:16
  • other cuestion... how can i display it in the checkout onepage went the user need to fill out the checkout form?
    – user41058
    Jul 15, 2016 at 20:47
  • error for me : Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ')' Feb 2, 2018 at 9:51
1

Could you replace the following code with your code

// add attribute to form
    /** @var  $attribute */
    $attribute = $customerSetup->getEavConfig()->getAttribute('customer',
        'id_number');

    $usedinform[] = "adminhtml_customer";
    $usedinform[] = "checkout_register";
    $usedinform[] = "customer_account_create";
    $usedinform[] = "customer_account_edit";
    $usedinform[] = "adminhtml_checkout";
    $attribute->setData("used_in_forms", $usedinform)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 1)
        ->setData("sort_order", 100);

    $attribute->save();

    $setup->endSetup();

Hope it works for you :)

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.