Magento 2 Shipping address selection changes to drop down form radio options. I want to display default shipping address on page load but it's always showing first shipping address as a selected. In Magento_Checkout/template/shipping-address/list.html :
<div class="field addresses">
<div class="control">
<div class="shipping-address-items">
<select class="select" name="billing_address_id" data-bind="
options: shippingAddressOptions,
optionsText: shippingAddressOptionsText,
value: selectedShippingAddress,
selectedOptions: chosenCountries,
event: {change: onShippingAddressChange(selectedShippingAddress())};
"></select>
</div>
</div>
</div>
In js/view/shipping.js file:
define(
[
'jquery',
"underscore",
'Magento_Ui/js/form/form',
'ko',
'Magento_Customer/js/model/customer',
'Magento_Customer/js/model/address-list',
'Magento_Checkout/js/model/address-converter',
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/action/create-shipping-address',
'Magento_Checkout/js/action/select-shipping-address',
'Magento_Checkout/js/model/shipping-rates-validator',
'Magento_Checkout/js/model/shipping-address/form-popup-state',
'Magento_Checkout/js/model/shipping-service',
'Magento_Checkout/js/action/select-shipping-method',
'Magento_Checkout/js/model/shipping-rate-registry',
'Magento_Checkout/js/action/set-shipping-information',
'Magento_Checkout/js/model/step-navigator',
'Magento_Ui/js/modal/modal',
'Magento_Checkout/js/model/checkout-data-resolver',
'Magento_Checkout/js/checkout-data',
'uiRegistry',
'Magento_Catalog/js/price-utils',
'mage/translate',
'Magento_Checkout/js/model/shipping-rate-service'
],
function(
$,
_,
Component,
ko,
customer,
addressList,
addressConverter,
quote,
createShippingAddress,
selectShippingAddress,
shippingRatesValidator,
formPopUpState,
shippingService,
selectShippingMethodAction,
rateRegistry,
setShippingInformationAction,
stepNavigator,
modal,
checkoutDataResolver,
checkoutData,
registry,
priceUtils,
$t
) {
'use strict';
var popUp = null,
shippingAddress = quote.shippingAddress,
newAddressOption = {
getAddressInline: function () {
return $t('New Address');
},
customerAddressId: null
},
shippingAddressOptions = addressList().filter(function (address) {
return address.getType() == 'customer-address';
});
shippingAddressOptions.push(newAddressOption);
return Component.extend(
{
defaults: {
template: 'Magento_Checkout/shipping'
},
visible: ko.observable(!quote.isVirtual()),
errorValidationMessage: ko.observable(false),
isCustomerLoggedIn: customer.isLoggedIn,
isFormPopUpVisible: formPopUpState.isVisible,
isFormInline: addressList().length == 0,
isNewAddressAdded: ko.observable(false),
saveInAddressBook: 1,
quoteIsVirtual: quote.isVirtual(),
shippingAddressOptions: shippingAddressOptions,
chosenCountries:ko.observableArray([shippingAddress]),
Best Answer
I have to change data-bind funtion to
optionsAfterRender: function(option, item) { if(){ selectedShippingAddress(item); } }
keep your backup file before any changes. Complete solutions: It worked for my requirement. You may do some change according to your requirement. But using this we can change shipping address list to drop down.
Add custom address template name : Magento_Checkout/shipping-address/mylist.html
Change and add mylist.html file in shipping.html file.
Now add Shipping.js file.