Move the billing address to right after contact information
By default the checkout asks for the billing address near the end, after delivery and shipping. If you would rather collect it right after the customer gives you their name and email, you can change the order of the checkout steps.
The code below moves the billing address step so it comes right after contact information. Since the billing address now comes first, the usual "Use shipping address as billing address" checkbox is replaced with a "Use billing address as shipping address" checkbox on the shipping step. It's ticked by default, so a customer whose two addresses match never types their address twice. Everything else stays where it is. For more on what you can do with checkout steps and fields, see Custom checkout fields.
Add this custom code:
add_filter( 'sunshine_checkout_fields', 'sunshine_billing_after_contact', 20 );
function sunshine_billing_after_contact( $sections ) {
if ( empty( $sections['contact'] ) || empty( $sections['billing'] ) ) {
return $sections;
}
$billing = $sections['billing'];
unset( $sections['billing'] );
// Remove the "Use shipping address as billing address" checkbox and always show the address fields
if ( ! empty( $billing['fields'] ) ) {
foreach ( $billing['fields'] as $key => $field ) {
if ( isset( $field['id'] ) && 'shipping_as_billing' === $field['id'] ) {
unset( $billing['fields'][ $key ] );
continue;
}
unset( $billing['fields'][ $key ]['conditions'] );
}
$billing['fields'] = array_values( $billing['fields'] );
}
// Add a "Use billing address as shipping address" checkbox to the shipping step instead
if ( ! empty( $sections['shipping']['fields'] ) ) {
$condition = array(
'field' => 'billing_as_shipping',
'compare' => '!=',
'value' => 'yes',
'action' => 'show',
);
foreach ( $sections['shipping']['fields'] as $key => $field ) {
$sections['shipping']['fields'][ $key ]['conditions'] = array( $condition );
}
array_unshift(
$sections['shipping']['fields'],
array(
'id' => 'billing_as_shipping',
'type' => 'checkbox',
'name' => 'Use billing address as shipping address', // Change this to any wording you like
'default' => 'yes', // Change to '' if you want the box unticked by default
)
);
}
$reordered = array();
foreach ( $sections as $section_id => $section ) {
$reordered[ $section_id ] = $section;
if ( 'contact' === $section_id ) {
$reordered['billing'] = $billing;
}
}
return $reordered;
}
// When the box is ticked, copy the billing address over as the shipping address
add_action( 'sunshine_checkout_validation', 'sunshine_copy_billing_to_shipping', 10, 2 );
function sunshine_copy_billing_to_shipping( $section, $data ) {
if ( 'shipping' !== $section || empty( $data['billing_as_shipping'] ) || 'yes' !== $data['billing_as_shipping'] ) {
return;
}
foreach ( SPC()->countries->get_default_address_fields() as $address_field ) {
$value = SPC()->cart->get_checkout_data_item( 'billing_' . $address_field['id'] );
SPC()->cart->set_checkout_data_item( 'shipping_' . $address_field['id'], $value ? $value : '' );
}
}
Learn how to add this custom code to your WordPress website
The billing step only shows up when Sunshine actually needs a billing address, so on orders that never ask for one this code does nothing.
Still need help?
If you have not yet found your answer in the documentation articles, please contact support