John Siniger
John Siniger

Reputation: 885

Displaying by default shipping fields on checkout page

On my WooCommerce checkout page, I am selling only one product. The checkout page is showing this product and there is ckeckbox next to it, to show the shipping details, when clicked. This is on the default form-checkout.php WooCommerce template.

I would like to have this ckeckbox always selected, to show billing details by default.

I have tried to set:

$checkout = 1;

With no luck so far. The radio button is displayed by the following hook:

<?php do_action( 'woocommerce_checkout_before_customer_details'); ?>

Any help letting me know how I can achieve this will be very helpful.

Thanks.

Upvotes: 3

Views: 6010

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254388

If you look to the code of checkout/form-shipping.php WooCommerce template, inside the <h3> tag, where the checkbox code is located, there is woocommerce_ship_to_different_address_checked hook that you can use to achieve that. Here is an extract of this code template, that shows it:

?>
<div class="woocommerce-shipping-fields">
    <?php if ( true === WC()->cart->needs_shipping_address() ) : ?>

        <h3 id="ship-to-different-address">
            <label for="ship-to-different-address-checkbox" class="checkbox"><?php _e( 'Ship to a different address?', 'woocommerce' ); ?></label>
            <input id="ship-to-different-address-checkbox" class="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" />
        </h3>

        <div class="shipping_address">

So you can use this hook this way, to get this checkbox always selected and displays billing details:

add_filter( 'woocommerce_ship_to_different_address_checked', '__return_true');

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

The code is tested and fully functional.

Upvotes: 5

Related Questions