13

I want to display address fields (to save in default billing address) in customer registration form. I know how to do in Magento 1. But I'm struck with Magento 2. Have anybody done this before ..?

Any xml file update like in magento 1,

<customer_account_create>
    <reference name="customer_form_register">
        <action method="setData"><key>show_address_fields</key><value>1</value></action>
    </reference>
</customer_account_create>
  • You will have to pick up the address data through your observer and create the billing address record yourself. Why not to use a ready extension? For instance: itoris.com/… – nico Mar 14 '16 at 1:05
  • @Elavarasan, have you find solution? If Yes please post here. It can useful for others. – Bojjaiah Jul 6 '16 at 9:58
17

Example display address fields using customer_account_create.xml:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_form_register">
            <arguments>
                <argument name="show_address_fields" xsi:type="boolean">true</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>
3

Create this file in your custom theme:

app/design/frontend/YOUR_PACKAGE/YOUR_THEME/Magento_Customer/layout/customer_account_create.xml

So, you need to set the action method called setShowAddressFields like true, like that sample:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_form_register">
            <action method="setShowAddressFields">
                <argument name="show.address.fields" xsi:type="boolean">true</argument>
            </action>
        </referenceBlock>
    </body>
</page>
1

Hi first find magento root folder then vendor\magento\module-customer\view\frontend\layout customer_account_create.xml then go to line no 13 check this code and put billow section on line no 17

<referenceContainer name="content">
            <block class="Magento\Customer\Block\Form\Register" name="customer_form_register" template="form/register.phtml">
                <container name="form.additional.info" as="form_additional_info"/>
                <container name="customer.form.register.fields.before" as="form_fields_before" label="Form Fields Before" htmlTag="div" htmlClass="customer-form-before"/>
                <action method="setShowAddressFields">
                    <argument name="show.address.fields" xsi:type="boolean">true</argument>
                </action>
            </block>
            <block class="Magento\Cookie\Block\RequireCookie" name="require-cookie" template="Magento_Cookie::require_cookie.phtml">
                <arguments>
                    <argument name="triggers" xsi:type="array">
                        <item name="registerSubmitButton" xsi:type="string">.action.submit</item>
                    </argument>
                </arguments>
            </block>
        </referenceContainer>

use only this section

<action method="setShowAddressFields">
                        <argument name="show.address.fields" xsi:type="boolean">true</argument>
                    </action>
1

Never edit vendor files!!

Copy this in your theme and go on for there, i'm in search of the setting (backend) because why else would you code it like that?

If someone has found this let me know

Greets

0

Just open your app/design/frontend/Package/Theme/Magento_Customer/layout and open customer_account_create.xml.

Go to Line num 16 or find below code.

<container name="customer.form.register.fields.before" as="form_fields_before" label="Form Fields Before" htmlTag="div" htmlClass="customer-form-before"/>

After below code just put below code.

<action method="setShowAddressFields">
                    <argument name="show.address.fields" xsi:type="boolean">true</argumen>
          </action>

After this you will see state dropdown in register page.

0

There are two ways for accomplish this,

  1. From template file

  2. From xml

Above explained xml way multiple times, so I'm going to do template file way.

Open template file inside your theme [Do not edit core files], app/design/frontend/[vendor_name]/[theme_name]/Magento_Customer/templates/form/register.phtml

find line which has this code,

getShowAddressFields()

it could be either in,

<?php if($this->getShowAddressFields()): ?>

or

<?php if ($block->getShowAddressFields()): ?>

depending on your magento version.

put this code before that line,

<?php $this->setShowAddressFields(true); ?>

So , final output should looks like this,

<?php $this->setShowAddressFields(true); ?>
<?php if($this->getShowAddressFields()): ?>

or

<?php $this->setShowAddressFields(true); ?>
<?php if ($block->getShowAddressFields()): ?>

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.