Magento – One Page Checkout – Manually setAddress()

addresscheckoutjavascript

In One Page Checkout, the Billing class offers a setAddress(val) function that looks useful for setting the address that gets chosen when a user that has saved addresses selects one from the first-step of the checkout process.

When you call, say, setAddress(2) it will populate the hidden form with the proper values for that address. I am doing some custom validation on this form, so I appreciate being able to update the hidden form. I am doing a simple call:

jQuery('#billing-address-select').change(function(){
    option = jQuery('#billing-address-select option:selected').val();
    billing.setAddress(option);
});

However, after a user changes the selected address, after a quick Ajax call, the selected option gets automatically reverted back to the 'New Address' option (which has a null value).

The only thing I can see where this would happen, there is onchange="billing.newAddress(!this.value)" set on the select element. Looking at this method, though, all it is doing is showing or hiding the hidden form as needed.

Why is this form element getting reverted back to the 'New Address' option?

Best Answer

Open the file- "skin/frontend/base/default/js/opcheckout.js"

Look into the function billing.fillForm()

Change the code to the one below(the change is in the if condition)-

var elementValues = {};
if (transport && transport.responseText){
    try{
        elementValues = eval('(' + transport.responseText + ')');
    }
    catch (e) {
        elementValues = {};
    }
}
else{
    this.resetSelectedAddress();
}
arrElements = Form.getElements(this.form);
for (var elemIndex in arrElements) {
    if (arrElements[elemIndex].id && 'billing-address-select' != arrElements[elemIndex].id) {
        var fieldName = arrElements[elemIndex].id.replace(/^billing:/, '');
        arrElements[elemIndex].value = elementValues[fieldName] ? elementValues[fieldName] : '';
        if (fieldName == 'country_id' && billingForm){
            billingForm.elementChildLoad(arrElements[elemIndex]);
        }
    }
}