• h0ssam

    (@h0ssam)


    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 option

    but 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 below

    add_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)
  • Hey there, @h0ssam! Thanks for contacting us.

    While we can’t provide support for code customization as per our support policy, we do our best to offer advice and direct you to appropriate resources.

    You can visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there too.

    I’m going to leave it open for a bit to see if anyone is able to chime in and help you out further.

    Please let us know if there’s anything else we can do to help or if you have any questions.

    Have a wonderful day!

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.