I am customizing WooCommerce checkout behavior and have encountered an issue with the “Ship to a different address?” checkbox.
When the checkbox is checked, shipping fields are displayed and calculations are based on the shipping address. This works as expected.
However, when the checkbox is unchecked, WooCommerce automatically sets the shipping fields to match the billing fields. I want to prevent this behavior.
What I Want to Achieve:
Display shipping fields regardless of whether the checkbox is checked or unchecked.
Keep billing and shipping fields completely independent.
Ensure that all calculations are based solely on the shipping fields, even when the checkbox is unchecked.
What I Have Tried:
I have attempted the following solutions:
Used the woocommerce_checkout_posted_data filter to clear billing fields when the checkbox is unchecked:
add_filter('woocommerce_checkout_posted_data', 'force_shipping_fields_only');
function force_shipping_fields_only($data) {
if (isset($data['ship_to_different_address']) && !$data['ship_to_different_address']) {
foreach ($data as $key => $value) {
if (strpos($key, 'billing_') !== false) {
$data[$key] = ''; // Clear billing fields
}
}
}
return $data;
}
However, this did not stop WooCommerce from overwriting the shipping fields with the billing data.
Tried overriding the customer data in woocommerce_checkout_update_order_review:
add_action('woocommerce_checkout_update_order_review', 'force_shipping_only_calculation');
function force_shipping_only_calculation($posted_data) {
WC()->customer->set_shipping_country($posted_data['shipping_country']);
WC()->customer->set_shipping_state($posted_data['shipping_state']);
// And so on for other fields...
}
But this also didn’t resolve the issue.
Expected Behavior:
When the checkbox is unchecked:
Shipping fields should remain editable and not be overwritten by billing data.
Calculations should always be based on shipping fields.
Actual Behavior:
When the checkbox is unchecked:
Shipping fields are overwritten by billing data.
WooCommerce ignores the independent shipping fields.