ajax – How to switch Shipping & Billing address on woocommerce checkout

Question

I am searching for a way to show the shipping details first, then change the “Ship to a different address” checkbox to “Bill to a different address”, followed by the billing details.
Just changing the labels doesnt work, because then the shipping costs are still calculated based on the original shipping country (which will then appear to be the billing country) (no matter if I set Shipping destination to customer’s shipping address or to customer’s billing address in woocommerce shipping options settings).

So I came across this answer (https://stackoverflow.com/a/55386894) and edited the templates as described. However, the shipping costs are not updated correctly.
I’ve set the following shipping options:

  • Germany: Free shipping
  • Rest of Europe: 10 Euro.

No matter which country I select as shipping country, it shows free shipping (it makes an ajax refresh, but does not change shipping costs). Only if I check the “Bill to different address checkbox”, the shipping costs are updated correctly (based on the shipping country, means as it should be). So I need a way to update the shipping costs independently from clicking the checkbox. Any help would be really appreciated.

Here is what I tried:

  1. In form-checkout.php I just simply switched the billing and shipping output.

  2. In form-billing.php I have this:

    <div class="woocommerce-billing-fields">
            <h3 id="ship-to-different-address">
                <label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
                    <input id="ship-to-different-address-checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" <?php checked( apply_filters( 'woocommerce_ship_to_different_address_checked', 'shipping' === get_option( 'woocommerce_ship_to_destination' ) ? 1 : 0 ), 1 ); ?> type="checkbox" name="ship_to_different_address" value="1" /> <span><?php esc_html_e( 'Different billing address?', 'woocommerce' ); ?></span>
                </label>
            </h3>
    
        <?php do_action( 'woocommerce_before_checkout_billing_form', $checkout ); ?>
    
    
            <div class="shipping_address">
    
            <?php do_action( 'woocommerce_before_checkout_shipping_form', $checkout ); ?>
    
                <div class="woocommerce-billing-fields__field-wrapper">
                    <?php
                    $fields = $checkout->get_checkout_fields( 'billing' );
    
                    foreach ( $fields as $key => $field ) {
                        woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
                    }
                    ?>
                </div>
    
                <?php do_action( 'woocommerce_after_checkout_shipping_form', $checkout ); ?>
    
            </div>
    
        <?php do_action( 'woocommerce_after_checkout_billing_form', $checkout ); ?>
    </div>
  1. And in form-shipping.php, I have this:
<div class="woocommerce-shipping-fields">
<?php if ( true === WC()->cart->needs_shipping_address() ) : ?>
    <?php if ( wc_ship_to_billing_address_only() && WC()->cart->needs_shipping() ) : ?>
        <h3><?php esc_html_e( 'Shipping &amp; Billing', 'woocommerce' ); ?></h3>
    <?php else : ?>

        <h3><span class="checkout-step"><?php echo '1'; ?></span><span class="checkout-title"><?php esc_html_e( 'Shipping Address', 'woocommerce' ); ?></span></h3>

    <?php endif; ?>

        <div class="woocommerce-shipping-fields__field-wrapper">
            <?php
            $fields = $checkout->get_checkout_fields( 'shipping' );

            foreach ( $fields as $key => $field ) {
                woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
            }
            ?>
        </div>

<?php endif; ?>
</div>

0
Lina 2 months 2021-07-21T04:50:01-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse