3

In registration form I want to ask the user if he wants to use a different address for shipping address. If that is true, then he can input the fields for the shipping Address. I have tried to override and modify register.phtml form that is located in the path

app/design/frontend/Vendor/Theme/Magento_Customer/templates/form/ register.phtml

I tried to copy the whole form for address and put <input type="hidden" name="default_shipping" value="1"> in the form for shipping Address, however when I submit the form it copies the data from company form into shipping address form. It duplicates the info and I get an error Delete operation . This is the field set for shipping Address

<fieldset id = "shipping-address-register" class="fieldset address">
        <legend class="legend"><span><?= $block->escapeHtml(__('Business Shipping Address')) ?></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 ?>

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

        <div class="field street required">
            <label for="street_1" class="label"><span><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('street') ?></span></label>
            <div class="control">
                <input type="text" name="street[]" value="<?= $block->escapeHtmlAttr($block->getFormData()->getStreet(0)) ?>" title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('street') ?>" 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><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('city') ?></span></label>
            <div class="control">
                <input type="text" name="city" value="<?= $block->escapeHtmlAttr($block->getFormData()->getCity()) ?>" title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('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><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?></span></label>
            <div class="control">
                <select id="region_id" name="region_id" title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?>" 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="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?>" 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><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('postcode') ?></span></label>
            <div class="control">
                <input type="text" name="postcode" value="<?= $block->escapeHtmlAttr($block->getFormData()->getPostcode()) ?>" title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('postcode') ?>" 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><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('country_id') ?></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_shipping" value="1">

    </fieldset>

This is the fieldset for General Company address

  <?php if ($block->getShowAddressFields()): ?>
    <fieldset class="fieldset address">
        <legend class="legend"><span><?= $block->escapeHtml(__('Business 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 ?>

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

        <div class="field street required">
            <label for="street_1" class="label"><span><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('street') ?></span></label>
            <div class="control">
                <input type="text" name="street[]" value="<?= $block->escapeHtmlAttr($block->getFormData()->getStreet(0)) ?>" title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('street') ?>" 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><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('city') ?></span></label>
            <div class="control">
                <input type="text" name="city" value="<?= $block->escapeHtmlAttr($block->getFormData()->getCity()) ?>" title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('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><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?></span></label>
            <div class="control">
                <select id="region_id" name="region_id" title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?>" 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="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?>" 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><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('postcode') ?></span></label>
            <div class="control">
                <input type="text" name="postcode" value="<?= $block->escapeHtmlAttr($block->getFormData()->getPostcode()) ?>" title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('postcode') ?>" 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><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('country_id') ?></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">


    </fieldset>

Thanks and greetings!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.