0

We are trying to make the billing address checkbox labeled "Saved in address box" checked by default when the page loads.

We are aware of the post: backend order create: default billing = shipping address + add to addressbook which is related but it does not work for us.

We have created the custom module and we are able to override the core file that we believe should be changed in our custom app:

The file is vendor/Magento/module-sales/view/adminhtml/web/order/create/form/address.phtml.

        <?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * @var \Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address|\Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address $block
 */
if ($block->getIsShipping()):
    $_fieldsContainerId = 'order-shipping_address_fields';
    $_addressChoiceContainerId = 'order-shipping_address_choice';
    ?>
    <script>
    require(["Magento_Sales/order/create/form"], function(){

        order.shippingAddressContainer = '<?= /* @escapeNotVerified */ $_fieldsContainerId ?>';
        order.setAddresses(<?= /* @escapeNotVerified */ $block->getAddressCollectionJson() ?>);

    });
    </script>
    <?php
else:
    $_fieldsContainerId = 'order-billing_address_fields';
    $_addressChoiceContainerId = 'order-billing_address_choice';
    ?>
    <script>
        require(["Magento_Sales/order/create/form"], function(){
            order.billingAddressContainer = '<?= /* @escapeNotVerified */ $_fieldsContainerId ?>';
        });
    </script>
    <?php
endif; ?>

<fieldset class="admin__fieldset">
    <legend class="admin__legend <?= /* @escapeNotVerified */ $block->getHeaderCssClass() ?>">
        <span><?= /* @escapeNotVerified */ $block->getHeaderText() ?></span>
    </legend><br>

    <fieldset id="<?= /* @escapeNotVerified */ $_addressChoiceContainerId ?>" class="admin__fieldset order-choose-address">
    <?php if ($block->getIsShipping()): ?>
        <div class="admin__field admin__field-option admin__field-shipping-same-as-billing">
            <input type="checkbox" id="order-shipping_same_as_billing" name="shipping_same_as_billing"
                   onclick="order.setShippingAsBilling(this.checked)" class="admin__control-checkbox"
                   <?php if ($block->getIsAsBilling()): ?>checked<?php endif; ?> />
            <label for="order-shipping_same_as_billing" class="admin__field-label">
                <?= /* @escapeNotVerified */ __('Same As Billing Address') ?>
            </label>
        </div>
    <?php endif; ?>
        <div class="admin__field admin__field-select-from-existing-address">
            <label class="admin__field-label"><?= /* @escapeNotVerified */ __('Select from existing customer addresses:') ?></label>
            <?php $_id = $block->getForm()->getHtmlIdPrefix() . 'customer_address_id' ?>
            <div class="admin__field-control">
                <select id="<?= /* @escapeNotVerified */ $_id ?>"
                        name="<?= $block->getForm()->getHtmlNamePrefix() ?>[customer_address_id]"
                        onchange="order.selectAddress(this, '<?= /* @escapeNotVerified */ $_fieldsContainerId ?>')"
                        class="admin__control-select">
                    <option value=""><?= /* @escapeNotVerified */ __('Add New Address') ?></option>
                    <?php foreach ($block->getAddressCollection() as $_address): ?>
                        <?php //if($block->getAddressAsString($_address)!=$block->getAddressAsString($block->getAddress())): ?>
                        <option
                            value="<?= /* @escapeNotVerified */ $_address->getId() ?>"<?php if ($_address->getId() == $block->getAddressId()): ?> selected="selected"<?php endif; ?>>
                            <?= /* @escapeNotVerified */ $block->getAddressAsString($_address) ?>
                        </option>
                        <?php //endif; ?>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
    </fieldset>

    <div class="order-address admin__fieldset" id="<?= /* @escapeNotVerified */ $_fieldsContainerId ?>">
        <?= $block->getForm()->toHtml() ?>

        <div class="admin__field admin__field-option order-save-in-address-book">
            <input name="<?= $block->getForm()->getHtmlNamePrefix() ?>[save_in_address_book]" type="checkbox"
                   id="<?= $block->getForm()->getHtmlIdPrefix() ?>save_in_address_book"
                   value="1"
                   <?php if (!$block->getDontSaveInAddressBook() && $block->getAddress()->getSaveInAddressBook()): ?> checked="checked"<?php endif; ?>
                   class="admin__control-checkbox"/>
            <label for="<?= $block->getForm()->getHtmlIdPrefix() ?>save_in_address_book"
                   class="admin__field-label"><?= /* @escapeNotVerified */ __('Save in address book') ?></label>
        </div>
    </div>
    <?php $hideElement = 'address-' . ($block->getIsShipping() ? 'shipping' : 'billing') . '-overlay'; ?>
    <div style="display: none;" id="<?= /* @escapeNotVerified */ $hideElement ?>" class="order-methods-overlay">
        <span><?= /* @escapeNotVerified */ __('You don\'t need to select a shipping address.') ?></span>
    </div>

    <script>
        require(["Magento_Sales/order/create/form"], function(){
                order.bindAddressFields('<?= /* @escapeNotVerified */ $_fieldsContainerId ?>');
                order.bindAddressFields('<?= /* @escapeNotVerified */ $_addressChoiceContainerId ?>');
                <?php if ($block->getIsShipping() && $block->getIsAsBilling()): ?>
                order.disableShippingAddress(true);
                <?php endif; ?>
        });
    </script>
</fieldset>  

Here is the section that we think needs to be modified

<?php /* This is the code block that runs the Save in Address book section for billing address */  ?>
        <div class="admin__field admin__field-option order-save-in-address-book">
            <input name="<?= $block->getForm()->getHtmlNamePrefix() ?>[save_in_address_book]" type="checkbox"
                   id="<?= $block->getForm()->getHtmlIdPrefix() ?>save_in_address_book"
                   value="1" 
                   <?php if (!$block->getDontSaveInAddressBook() && $block->getAddress()->getSaveInAddressBook()): ?> checked="checked"<?php endif; ?>
                   class="admin__control-checkbox"/>
            <label for="<?= $block->getForm()->getHtmlIdPrefix() ?>save_in_address_book"
                   class="admin__field-label"><?= /* @escapeNotVerified */ __('Save in address book') ?></label>
        </div>

Thanks in Advance for the support!

1 Answer 1

0

Try following way:

app/code/VendorName/ModuleName/etc/adminhtml/di.xml


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address">
        <plugin name="sales_order_billing_address" type="VendorName\ModuleName\Plugin\Order\Create\Billing\Address" sortOrder="1"/>
    </type>
    <type name="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address">
        <plugin name="sales_order_shipping_address" type="VendorName\ModuleName\Plugin\Order\Create\Shipping\Address" sortOrder="1"/>
    </type>
</config>

Now create two plugin class.

app/code/VendorName/ModuleName//Plugin/Order/Create/Billing/Address.php


namespace VendorName\ModuleName\Plugin\Order\Create\Billing;

class Address
{
    /**
     * Get relevant path to template
     *
     * @return string
     */
    public function aroundGetTemplate(
        \Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address $subject,
        \Closure $proceed
    ) {
        return 'VendorName_ModuleName::order/create/form/address.phtml';
    }
}

app/code/VendorName/ModuleName//Plugin/Order/Create/Shipping/Address.php


namespace VendorName\ModuleName\Plugin\Order\Create\Shipping;

class Address
{
    /**
     * Get relevant path to template
     *
     * @return string
     */
    public function aroundGetTemplate(
        \Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address $subject,
        \Closure $proceed
    ) {
        return 'VendorName_ModuleName::order/create/form/address.phtml';
    }
}

Now copy core file inside your module [app/code/VendorName/ModuleName/view/adminhtml/templates/order/create/form/address.phtml] And modify following section:


<div class="admin__field admin__field-option order-save-in-address-book">
    <input name="<?= $block->getForm()->getHtmlNamePrefix() ?>[save_in_address_book]" type="checkbox"
           id="<?= $block->getForm()->getHtmlIdPrefix() ?>save_in_address_book"
           value="1"
           checked="checked"
           class="admin__control-checkbox"/>
    <label for="<?= $block->getForm()->getHtmlIdPrefix() ?>save_in_address_book"
           class="admin__field-label"><?= /* @escapeNotVerified */ __('Save in address book') ?></label>
</div>

Also your module should be loaded after Magento_Sales module so add following code inside your module.xml


<sequence>
    <module name="Magento_Sales"/>
</sequence>

Run setup:upgrade command and clear cache.

1
  • Sohel, We understand the first part but we are stuck on the part that we need to modify the code in the address.phtml. It is not working for us. The code shows checked= checked by default does not mean that it is checked by default. We are not sure how to modify this part of the code. Commented Feb 10, 2018 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.