Magento – How to we display custom drop down in registration page

customcustomer-attributemagento2register

enter image description here

I created Custom customer attribute:

 $customerSetup->addAttribute(Customer::ENTITY, 'customer_type', [

        'type'          => 'int',
        'label'         => 'Corporation Or Proproetorship',
        'input'         => 'select',
        'source'        => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
        'required'      => false,
        'visible'       => true,
        'system'        => false,
        'position'      => 210,
        'system' => false,
        'option'         => ['values' => ['Corporation', 'Proproetorship']],
    ]);

    $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_type')
    ->addData(['used_in_forms' => [
            'adminhtml_customer',
            'adminhtml_checkout',
            'customer_account_create',
            'customer_account_edit'
]]);

    $attribute->save();

Now I want to display in registration form; could you help me how can I do?

Best Answer

1) app/code/Vendor/Module/Setup/UpgradeData.php

<?php

namespace Vendor\Module\Setup;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements  UpgradeDataInterface
{
    private $customerSetupFactory;

    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context){
        $setup->startSetup();

        if (version_compare($context->getVersion(), '1.0.1', '<')) {

            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
            $customerSetup->addAttribute(
                Customer::ENTITY,
                'corporation_proproetorship',
                [
                    'type' => 'int',
                    'label' => 'Corporation Or Proproetorship',
                    'input' => 'select',
                    'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
                    'required' => true,
                    'default' => '0',
                    'sort_order' => 160,
                    'system' => false,
                    'position' => 160,
                    'adminhtml_only' => 1,
                    'option' =>
                        array (
                            'values' =>
                                array (
                                    0 => 'Corporation',
                                    1 => 'Proproetorship',
                                ),
                        ),
                ]
            );
            $myAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'corporation_proproetorship');

            // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
            $myAttribute->setData(
                'used_in_forms', ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']);
            $myAttribute->save();

        }


        $setup->endSetup();
    }
}

2) app/design/frontend/{vendor}/{theme}/Magento_Customer/templates/form/register.phtml

Copy the content from Vendor/Magento/module-customer/view/frontend/templates/form/registration.phtml then add this:

<div class="field required">
    <label for="extra1" class="label"><span><?= $block->escapeHtml(__('Corporation Or Proproetorship')) ?></span></label>
        <div class="control">
            <select name="corporation_proproetorship" id="corporation_proproetorship" title="<?= $block->escapeHtmlAttr(__('Corporation or Proproetorship')) ?>" class="input-select">
                <option disabled selected value> -- select an option -- </option>
                <option value="0">Corporation</option>
                <option value="1">Proproetorship</option>
            </select>
        </div>
    </div>
</div>