5

I just want the My billing and shipping address are the same checkbox to be unchecked by default and let the customer see the form to enter a new billing address.

I think the answer lies within this file: Magento_Checkout/web/js/view/billing-address.js

But I've tried an awful lot of things with no success.

I need to set isAddressSameAsShipping to false?

I mean I can just uncheck it using JavaScript, but that just seems like cheating..

Thanks

  • You can do it using javascript, if its working using js no problem for form – Rakesh Jesadiya Nov 21 '16 at 10:15
3

Override ..vendor/magento/module-payment/view/frontend/web/transparent.js and add this code :

var paymentMethodId = jQuery(".payment-methods input[type='radio']:checked").attr('id');

jQuery("#billing-address-same-as-shipping-"+paymentMethodId).trigger('click');
jQuery(".payment-method").find('select[name="billing_address_id"]  option:last-child').attr('selected','selected');
jQuery(".payment-method").find('.billing-address-form').show();

Override ../magento/module-checkout/view/frontend/web/js/action/set-billing-address.js: (Add code)

jQuery(document).on("click",".payment-method",function(){
         console.log(   jQuery(this).find('input[name="billing-address-same-as-shipping"]').is(":checked") );             
         console.log(jQuery(this).find('select[name="billing_address_id"]  option:last-child'));
         jQuery(this).find('input[name="billing-address-same-as-shipping"]').trigger('click');
         jQuery(this).find('select[name="billing_address_id"]  option:last-child').attr('selected','selected');
         jQuery(this).find('.billing-address-form').show();
    })
  • Rather dissappointing answer. The question clearly stated not to use Javascript. – leedch Feb 6 '17 at 10:34
  • 1
    You can use as alternative solution , Its not the way to down-vote any answer @leedch – Ronak Chauhan Feb 6 '17 at 13:42
1

I am copy/pasting my answer from Magento 2 : Uncheck My billing and shipping address are the same checkbox in checkout payment page based on shipping method

Disclaimer: I am an author of an article at the end of this answer.

Magento best practices way.

  1. Create a simple Magento 2 extension.
  2. Create a file app/code/Vendor/Module/view/frontend/requirejs-config.js with the content:

    var config = {
     map: {
     '*': {
       'Magento_Checkout/js/model/checkout-data-resolver': 'Vendor_Module/js/checkout-data-resolver',
       'Magento_Checkout/js/model/shipping-save-processor/default': 'Vendor_Module/js/shipping-save-processor/default'
      }
    }};
    
  3. Copy file vendor/magento/module-checkout/view/frontend/web/js/model/checkout-data-resolver.js to app/code/Vendor/Module/view/frontend/web/js/checkout-data-resolver.js.
  4. Edit file app/code/Vendor/Module/view/frontend/web/js/checkout-data-resolver.js and comment out code around line 231:

        applyBillingAddress: function () {
            var shippingAddress;
    
            if (quote.billingAddress()) {
                selectBillingAddress(quote.billingAddress());
    
                return;
            }
            shippingAddress = quote.shippingAddress();
    
            /*if (shippingAddress &&
                shippingAddress.canUseForBilling() &&
                (shippingAddress.isDefaultShipping() || !quote.isVirtual())
            ) {
                selectBillingAddress(quote.shippingAddress());
            }*/
        }
    };
    
  5. Copy file vendor/magento/module-checkout/view/frontend/web/js/model/shipping-save-processor/default.js to app/code/Vendor/Module/view/frontend/web/js/shipping-save-processor/default.js.

  6. In app/code/Vendor/Module/view/frontend/web/js/shipping-save-processor/default.js comment out code around line 34:

    saveShippingInformation: function () {
    var payload;
    
    /*if (!quote.billingAddress()) {
        selectBillingAddressAction(quote.shippingAddress());
    }*/
    
    payload = {
    
  7. Delete folder pub/static/frontend:

     rm -rf pub/static/frontend
    
  8. Regenerate static content:

     php bin/magento setup:static-content:deploy
    
  9. Clear magento cache and you are all set.

Dirty way

Instead of steps 1 through 6 simply edit the file vendor/magento/module-checkout/view/frontend/web/js/model/checkout-data-resolver.js and comment out code around line 231 as in step 4 and edit the file vendor/magento/module-checkout/view/frontend/web/js/model/shipping-save-processor/default.js and comment code code around line 34 as in step 6.

Repeat steps 7,8,9.

Originally published as https://www.goivvy.com/blog/magento-2-set-billing-shipping-address-unchecked-default-checkout

0

I have traced the code that sets the default value for the SameAsBilling attribute in the Quote\Address model.

The $address->setSameAsBilling(1) is called in Quote\Address::_populateBeforeSaveData() which in turn is called in Quote\Address::beforeSave()

The below solution works for the backend sale, where the order form comes with the selected checkbox, which makes the data input very cumbersome, since after each field is populated, theres a loading period for syncing the fields.

I have created a Plugin for the Quote\Address, as follows:

Vendor\Package\etc\di.xml

<type name="Magento\Quote\Api\Data\AddressInterface">
    <plugin name="cinch_ticketsale_plugin_quote_address"
        type="Vendor\Package\Plugin\QuoteAdress"
        sortOrder="10"
        disabled="false"/>
</type>

Vendor\Package\Plugin\QuoteAdress

namespace Vendor\Package\Plugin;

class QuoteAdress
{
    protected $_forceNotSameAsBilling = null;

    public function aroundBeforeSave($subject, $proceed)
    {
        if (!$subject->getId()) {
            $this->_forceNotSameAsBilling = true;
        }

        $returnValue = $proceed();

        $this->_forceNotSameAsBilling = false;

        return $returnValue;
    }

    public function aroundSetSameAsBilling($subject, $proceed, $sameAsBilling) {
        if ($this->_forceNotSameAsBilling) $sameAsBilling = 0;

        return $proceed($sameAsBilling);
    }
}

Your Answer

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

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