0

I have a custom customer select attribute and want to include it in my customer registration form on the frontend. Can anybody help with the frontend part? Thanks!

0

Try following code. Maybe that'll help you. I have found this solution from Link.

<?php

namespace CoolBlueWeb\Example\Setup;

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

class InstallData implements InstallDataInterface {

    private $_eavSetupFactory;
    private $_attributeRepository;

    public function __construct(
        \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
        \Magento\Eav\Model\AttributeRepository $attributeRepository,
    )
    {
        $this->_eavSetupFactory = $eavSetupFactory;
        $this->_attributeRepository = $attributeRepository;
    }

    public function install( ModuleDataSetupInterface $setup, ModuleContextInterface $context )
    {

        $eavSetup = $this->_eavSetupFactory->create(['setup' => $setup]);

        // add customer_attribute to customer
        $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer_attribute');
        $eavSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY, 'customer_attribute', [
            'type' => 'varchar',
            'label' => 'Customer Attribute',
            'input' => 'text',
            'required' => false,
            'system' => 0,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'sort_order' => '200'
        ]
    );

    // allow customer_attribute attribute to be saved in the specific areas
    $attribute = $this->_attributeRepository->get('customer', 'customer_attribute');
    $setup->getConnection()
    ->insertOnDuplicate(
        $setup->getTable('customer_form_attribute'),
        [
            ['form_code' => 'adminhtml_customer', 'attribute_id' => $attribute->getId()],
            ['form_code' => 'customer_account_create', 'attribute_id' => $attribute->getId()],
            ['form_code' => 'customer_account_edit', 'attribute_id' => $attribute->getId()],
        ]
    );
    }
}

Check ['form_code' => 'customer_account_create', 'attribute_id' => $attribute->getId()], in above code.

I hope it'll help you.

0

You can do it by following these steps

let's say your attribute code is [custom_att]

in your module add this file to the layout folder

app/code/{your-vendor}/{your-module}/view/frontend/layout/customer_account_create.xml

in this file [customer_account_create.xml], add this code

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="form.additional.info">
            <block class="Magento\Framework\View\Element\Template" name="form_additional_info_customer1">
                <action method="setTemplate">
            <argument name="template" xsi:type="string">{your-vendor}_{your-module}::additional_customer_attribute.phtml</argument>
            </action>
            </block> 
        </referenceContainer>
    </body>
</page>

in your module templates add this file

app/code/{your-vendor}/{your-module}/view/frontend/templates/additional_customer_attribute.phtml

the code in this file is [additional_customer_attribute.phtml]

<div class="field custom_att">
    <label for="custom_att" class="label"><span><?php /* @escapeNotVerified */ echo __('Custom Att') ?></span></label>
    <div class="control">
        <select id="custom_att" name="custom_att" title="<?php /* @escapeNotVerified */ echo __('Custom Att') ?>" class="validate-select">
        <option value="1">Option1</option>
        <option value="2">Option2</option>
        <option value="3">Option3</option>
        </select>
    </div>
</div>

You can modify the options are you need

after this execute these two commands in magento root

php bin/magento setup:upgrade

php bin/magento cache:clean

php bin/magento cache:flush

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.