Magento – Magento 2 : Default billing address not selected by default on checkout page

billing-addresscheckoutmagento2shipping-address

I am using Magento 2.0.7 and my default billing and shipping address are different in My account Address section.

When I am going on checkout page my default shipping address selected by default and I move on payment section but on payment section My shipping address by default set as billing address.

I already mention default billing address then how we get that address selected by default ?

Best Answer

You could do the following:

  • overwrite a default js file checkout-data-resolver.js
  • add extra lines to the applyBillingAddress function to check if default billing address exists and if true to use it

step 1. Overwritting a default magento checkout-data-resolver.js

  • 1.1 Create your module. YourCompany\Checkout

  • 1.2 Add file \app\code\YourCompany\Checkout\view\frontend\requirejs-config.js

with following content

var config = {
map: {
    '*': {
        'Magento_Checkout/js/model/checkout-data-resolver': 
    'YourCompany_Checkout/js/model/checkout-data-resolver'        
    }
}

};
  • 1.3 Copy magento file \vendor\magento\module-checkout\view\frontend\web\js\model\checkout-data-resolver.js to the destination \app\code\YourCompany\Checkout\view\frontend\web\js\model\checkout-data-resolver.js

step 2. Modify applyBillingAddress function

2.1 in new checkout-data-resolver.js file find applyBillingAddress. Insert following cod after var shippingAddress; and before if (quote.billingAddress())

....
var isBillingAddressInitialized;
isBillingAddressInitialized = addressList.some(function (addressFromList) {
  if (addressFromList.isDefaultBilling()) {
     selectBillingAddress(addressFromList);
              return true;
    }

  return false;
 });

if(isBillingAddressInitialized){
                 return;
             }  
....

This code actually goes through customer addresses and checks if any of them has defaultBilling set to true. If such an address exists it will be used as the billing address. If not the script will fall back to the default magento behavior

Related Topic