Question

How can I show both of addresses on my customer register form on Magento 2.2.4 ? I've added this, but only one address is showing.

This is customer_account_create.xml

   <referenceBlock name="customer_form_register">
            <arguments>
                <argument name="show_address_fields" xsi:type="boolean">true</argument>
                <argument name="show_telephone_field" xsi:type="boolean">true</argument>
            </arguments>
        </referenceBlock>

And register.phtml

    <?php
if ($block->getShowAddressFields()): ?>
    <fieldset class="fieldset address">
        <legend class="legend"><span><?= $block->escapeHtml(__('Address Information')) ?></span></legend><br>
        <input type="hidden" name="create_address" value="1" />

        <?php $_company = $block->getLayout()->createBlock('Magento\Customer\Block\Widget\Company') ?>
        <?php if ($_company->isEnabled()): ?>
            <?= $_company->setCompany($block->getFormData()->getCompany())->toHtml() ?>
        <?php endif ?>

        <?php $_telephone = $block->getLayout()->createBlock('Magento\Customer\Block\Widget\Telephone') ?>
        <?php if ($_telephone->isEnabled()): ?>
            <?= $_telephone->setTelephone($block->getFormData()->getTelephone())->toHtml() ?>
        <?php endif ?>

        <?php $_fax = $block->getLayout()->createBlock('Magento\Customer\Block\Widget\Fax') ?>
        <?php if ($_fax->isEnabled()): ?>
            <?= $_fax->setFax($block->getFormData()->getFax())->toHtml() ?>
        <?php endif ?>


        <div class="field siret required">
            <label for="siret" class="label"><span><?php /* @escapeNotVerified */ echo __('SIRET') ?></span></label>
            <div class="control">
                <input type="text" name="siret" id="siret" title="<?php /* @escapeNotVerified */ echo __('SIRET') ?>" class="input-text" data-validate="{required:true}" autocomplete="off">
            </div>
        </div>


        <?php $_streetValidationClass = $this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('street'); ?>

        <div class="field street required">
            <label for="street_1" class="label"><span><?= $block->escapeHtml(__('Street Address')) ?></span></label>
            <div class="control">
                <input type="text" name="street[]" value="<?= $block->escapeHtmlAttr($block->getFormData()->getStreet(0)) ?>" title="<?= $block->escapeHtmlAttr(__('Street Address')) ?>" id="street_1" class="input-text <?= $block->escapeHtmlAttr($_streetValidationClass) ?>">
                <div class="nested">
                    <?php $_streetValidationClass = trim(str_replace('required-entry', '', $_streetValidationClass)); ?>
                    <?php for ($_i = 2, $_n = $this->helper('Magento\Customer\Helper\Address')->getStreetLines(); $_i <= $_n; $_i++): ?>
                        <div class="field additional">
                            <label class="label" for="street_<?= /* @noEscape */ $_i ?>">
                                <span><?= $block->escapeHtml(__('Address')) ?></span>
                            </label>
                            <div class="control">
                                <input type="text" name="street[]" value="<?= $block->escapeHtml($block->getFormData()->getStreetLine($_i - 1)) ?>" title="<?= $block->escapeHtmlAttr(__('Street Address %1', $_i)) ?>" id="street_<?= /* @noEscape */ $_i ?>" class="input-text <?= $block->escapeHtmlAttr($_streetValidationClass) ?>">
                            </div>
                        </div>
                    <?php endfor; ?>
                </div>
            </div>
        </div>

        <div class="field required">
            <label for="city" class="label"><span><?= $block->escapeHtml(__('City')) ?></span></label>
            <div class="control">
                <input type="text" name="city" value="<?= $block->escapeHtmlAttr($block->getFormData()->getCity()) ?>" title="<?= $block->escapeHtmlAttr(__('City')) ?>" class="input-text <?= $block->escapeHtmlAttr($this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('city')) ?>" id="city">
            </div>
        </div>

        <div class="field region required">
            <label for="region_id" class="label"><span><?= $block->escapeHtml(__('State/Province')) ?></span></label>
            <div class="control">
                <select id="region_id" name="region_id" title="<?= $block->escapeHtmlAttr(__('State/Province')) ?>" class="validate-select" style="display:none;">
                    <option value=""><?= $block->escapeHtml(__('Please select a region, state or province.')) ?></option>
                </select>
                <input type="text" id="region" name="region" value="<?= $block->escapeHtml($block->getRegion()) ?>" title="<?= $block->escapeHtmlAttr(__('State/Province')) ?>" class="input-text <?= $block->escapeHtmlAttr($this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('region')) ?>" style="display:none;">
            </div>
        </div>

        <div class="field zip required">
            <label for="zip" class="label"><span><?= $block->escapeHtml(__('Zip/Postal Code')) ?></span></label>
            <div class="control">
                <input type="text" name="postcode" value="<?= $block->escapeHtmlAttr($block->getFormData()->getPostcode()) ?>" title="<?= $block->escapeHtmlAttr(__('Zip/Postal Code')) ?>" id="zip" class="input-text validate-zip-international <?= $block->escapeHtmlAttr($this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('postcode')) ?>">
            </div>
        </div>

        <div class="field country required">
            <label for="country" class="label"><span><?= $block->escapeHtml(__('Country')) ?></span></label>
            <div class="control">
                <?= $block->getCountryHtmlSelect() ?>
            </div>
        </div>
        <?php $addressAttributes = $block->getChildBlock('customer_form_address_user_attributes');?>
        <?php if ($addressAttributes): ?>
            <?php $addressAttributes->setEntityType('customer_address'); ?>
            <?php $addressAttributes->setFieldIdFormat('address:%1$s')->setFieldNameFormat('address[%1$s]');?>
            <?php $block->restoreSessionData($addressAttributes->getMetadataForm(), 'address');?>
            <?= $addressAttributes->setShowContainer(false)->toHtml() ?>
        <?php endif;?>
        <input type="hidden" name="default_billing" value="1">
        <input type="hidden" name="default_shipping" value="1">
    </fieldset>

<?php endif; ?>

Edit : tried this

$block->setShowAddressFields(true)

But it does the same thing ad customer_account_create...

Was it helpful?

Solution

I've ended up by adding manually the fields for a second address and the I've override the class

/vendor/magento/module-customer/Controller/Account/CreatePost.php

I'm creating a new address and then assign it to the new customer

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top