Magento 2 – Remove Billing Address from Payment Step in Checkout

billing-addresscheckoutknockoutjsmagento2.2shipping-address

In the payment step under checkout in Magento 2, I'm trying to remove the address and the option: 'My billing and shipping address are the same
'. Because I have added billing address to shipping step.

I got a LayoutProcessor.php and I'm trying to edit the $jsLayout but I'm unsure if this is the way to go and what path to unset it at.

I currently got the following code which doesn't work yet. It's in the aroundprocess function.

if (isset($jsLayoutResult['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['payments-list'])) {
            $componentBilling = $jsLayoutResult['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['payments-list']['children'];
            foreach ($componentBilling as $keyBilling => $value) {
                if (preg_match('/-form/', $keyBilling)) {
                    unset($componentBilling[$keyBilling]['children']['form-fields']);
                }
            }
        }

Thanks in advance!

P.s. for anyone who is curious about how to move the billing address to the shipping step. I followed this guide.


Update: I did find this option: In admin under Stores -> Configuration -> Sales -> Checkout -> Checkout Options -> Display Billing Address On .. change it to Payment Page. And then add the following code

    unset($jsLayoutResult['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['afterMethods']);

But I would prefer a prettier option where no settings have to be changed in the admin, so if anyone knows anything?

Best Answer

I ended up using this code that I found as part of an answer on a different stackexchange question.

/* config: checkout/options/display_billing_address_on = payment_method */
            if (isset($jsLayoutResult['components']['checkout']['children']['steps']['children']['billing-step']['children']
                ['payment']['children']['payments-list']['children']
            )) {
                foreach ($jsLayoutResult['components']['checkout']['children']['steps']['children']['billing-step']['children']
                         ['payment']['children']['payments-list']['children'] as $key => $payment) {

                    unset($jsLayoutResult['components']['checkout']['children']['steps']['children']['billing-step']['children']
                        ['payment']['children']['payments-list']['children'][$key]);

                }
            }

The problem with this is that if a shop uses checkout-agreements that those will also be deleted. Anyone know how to fix this?