/**
* MYLS Health WooCommerce Checkout
* Classic Checkout Shortcode:
*
* – Require Billing Address
* – Require Shipping Address
* – Keep Shipping Address permanently visible
* – Remove “Ship to a different address?” checkbox
* – Rename section to “Shipping Address”
*/
/**
* Force WooCommerce to treat shipping address
* as different from billing.
*/
add_filter( ‘woocommerce_ship_to_different_address_checked’, ‘__return_true’ );
/**
* Make Billing and Shipping fields required.
*/
add_filter( ‘woocommerce_checkout_fields’, ‘myls_require_checkout_fields’ );
function myls_require_checkout_fields( $fields ) {
// REQUIRED BILLING FIELDS
$billing_required = array(
‘billing_first_name’,
‘billing_last_name’,
‘billing_country’,
‘billing_address_1’,
‘billing_city’,
‘billing_state’,
‘billing_postcode’,
‘billing_phone’,
‘billing_email’
);
foreach ( $billing_required as $field ) {
if ( isset( $fields[‘billing’][ $field ] ) ) {
$fields[‘billing’][ $field ][‘required’] = true;
}
}
// REQUIRED SHIPPING FIELDS
$shipping_required = array(
‘shipping_first_name’,
‘shipping_last_name’,
‘shipping_country’,
‘shipping_address_1’,
‘shipping_city’,
‘shipping_state’,
‘shipping_postcode’
);
foreach ( $shipping_required as $field ) {
if ( isset( $fields[‘shipping’][ $field ] ) ) {
$fields[‘shipping’][ $field ][‘required’] = true;
}
}
return $fields;
}
/**
* Validate Billing and Shipping fields.
*/
add_action( ‘woocommerce_checkout_process’, ‘myls_validate_checkout_fields’ );
function myls_validate_checkout_fields() {
// BILLING VALIDATION
$billing_fields = array(
‘billing_first_name’ => ‘Billing first name’,
‘billing_last_name’ => ‘Billing last name’,
‘billing_country’ => ‘Billing country’,
‘billing_address_1’ => ‘Billing street address’,
‘billing_city’ => ‘Billing city’,
‘billing_state’ => ‘Billing state’,
‘billing_postcode’ => ‘Billing ZIP code’,
‘billing_phone’ => ‘Billing phone number’,
‘billing_email’ => ‘Billing email address’
);
foreach ( $billing_fields as $field => $label ) {
if ( empty( $_POST[ $field ] ) ) {
wc_add_notice(
$label . ‘ is required.’,
‘error’
);
}
}
// SHIPPING VALIDATION
if ( WC()->cart && WC()->cart->needs_shipping_address() ) {
$shipping_fields = array(
‘shipping_first_name’ => ‘Shipping first name’,
‘shipping_last_name’ => ‘Shipping last name’,
‘shipping_country’ => ‘Shipping country’,
‘shipping_address_1’ => ‘Shipping street address’,
‘shipping_city’ => ‘Shipping city’,
‘shipping_state’ => ‘Shipping state’,
‘shipping_postcode’ => ‘Shipping ZIP code’
);
foreach ( $shipping_fields as $field => $label ) {
if ( empty( $_POST[ $field ] ) ) {
wc_add_notice(
$label . ‘ is required.’,
‘error’
);
}
}
}
}
/**
* Remove the “Ship to a different address?” checkbox
* and replace it with a simple Shipping Address heading.
*/
add_action( ‘wp_footer’, ‘myls_checkout_shipping_customization’ );
function myls_checkout_shipping_customization() {
if ( ! is_checkout() ) {
return;
}
?>
jQuery(function($){
/* * Force the hidden checkbox to remain checked. */ $('#ship-to-different-address-checkbox') .prop('checked', true) .trigger('change');
/* * Keep shipping fields open at all times. */ $('.shipping_address').show();
/* * If WooCommerce refreshes checkout via AJAX, * reopen shipping fields automatically. */ $(document.body).on('updated_checkout', function(){
$('#ship-to-different-address-checkbox') .prop('checked', true);
$('.shipping_address').show();
});
});