show cities / states in checkout dropdown options if exist in shipping zone
-
Hello
I’m trying to show only states / cities and hide others , the showing or available states are added to shipping zone !
its like selling to specific country on woocommerce optionbut my condition based on shipping zone , hide all from checkout and
show state only if added in shipping zone
i tried a lot of functions
like this one belowadd_filter( ‘woocommerce_checkout_fields’, ‘filter_checkout_fields_based_on_shipping_zones’ );
function filter_checkout_fields_based_on_shipping_zones( $fields ) {
// Get all the shipping zones
$shipping_zones = WC_Shipping_Zones::get_zones();
$allowed_states = array();// Loop through each shipping zone foreach ( $shipping_zones as $zone ) { // Get the zone regions $zone_regions = $zone['zone_locations']; foreach ( $zone_regions as $region ) { // If the region type is a state, add it to the allowed states if ( 'state' === $region->type ) { $allowed_states[] = $region->code; } } } // Ensure allowed states are unique to avoid duplicates $allowed_states = array_unique( $allowed_states ); // Filter out states not included in any shipping zone foreach ( $fields['billing']['billing_state']['options'] as $state_code => $state_name ) { if ( ! in_array( $state_code, $allowed_states, true ) ) { unset( $fields['billing']['billing_state']['options'][$state_code] ); } } foreach ( $fields['shipping']['shipping_state']['options'] as $state_code => $state_name ) { if ( ! in_array( $state_code, $allowed_states, true ) ) { unset( $fields['shipping']['shipping_state']['options'][$state_code] ); } } return $fields;
}
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘show cities / states in checkout dropdown options if exist in shipping zone’ is closed to new replies.