skip to Main Content

I am building my website. I have a WooCommerce plugin installed and the country I live in it doesn’t have postcode or zip. So to calculate the shipping I use districts. I am adding all districts as a dropdown in the cart page in (calculate shipping) area and checkout page. below the code for checkout page

function custom_override_default_postcode_field( $address_fields ) {
    // Your postcodes array
    $postcode_array = array(
        'ALMOHAMMDIAH' => "ALMOHAMMDIAH",
        'ALNAEEM' => "ALNAEEM",
        'ALZAHRRA' => "ALZAHRRA"
    );
    $address_fields['postcode']['type'] = 'select';
    $address_fields['postcode']['options'] = $postcode_array;

    return $address_fields;
}

and adding the district in cart page in (calculate shipping) area code below

'woocommerce_shipping_calculator_enable_postcode', true ) ) : ?>
            <p class="form-row form-row-wide" id="calc_shipping_postcode_field">

                <?php
                // HERE <===== define your array of cities
                $postcodes = array('ALMOHAMMDIAH','ALNAEEM','ALZAHRRA');

                $current_postcode = esc_attr( WC()->customer->get_shipping_postcode() );
                ?>
            <select name="calc_shipping_postcode" id="calc_shipping_postcode">
                <option value=""><?php _e( 'Select a District&hellip;', 'woocommerce' ); ?></option>
                <?php foreach( $postcodes as $postcode ):
                echo '<option value="'.$postcode.'" '.selected( $current_postcode, $postcode ).'>'.$postcode.'</option>';
                endforeach; ?>
            </select>
            </p>

then I change the postcode / ZIP to districts by this core below

function my_text_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'Postcode / ZIP' :
            $translated_text = __( 'District', 'woocommerce' );
            break;
    }
    return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );

All the code above I found them online and changed some text and they are working.

The problem now is that the postcode in WooCommerce backend forces me to use all caps. Some districts must have spaces and I need to do that but I can’t figure it out. How can I make the postcode in the backend accept spaces or at least – or _ ?

like ( AL MOHAMMDIAH or AL-MOHAMMDIAH ) instead of (ALMOHAMMDIAH)

btw the codes above I caps all districts so the calculation works. If I added space, _ or – it brock and nothing shows.

2

Answers


  1. Chosen as BEST ANSWER

    What I did to make this work is I changed the state/Country field to be districts and hide the ZIP code. its working fine so far the only downside is the district name comes after the city. I guess its no big deal in the country where I live in

    /*  district added as state/Country for shipping purposes */
    add_filter('woocommerce_states', 'add_custom_states_to_country');
    add_filter('woocommerce_countries_allowed_country_states', 'add_custom_states_to_country');
    function add_custom_states_to_country( $states ) {
        $states['SA'] = array(
            'JOR' => __('Abruq-Ar-Rughamah', 'woocommerce'),
            'JAL' => __('Al-Adel', 'woocommerce'),
            'JAJ' => __('Al-Ajaweed', 'woocommerce'),
            'JAD' => __('Al-Ajwad', 'woocommerce'),
            'JAW' => __('Al-Amwaj', 'woocommerce'),
            'JAS' => __('Al-Andalus', 'woocommerce'),
            'JAH' => __('Al-Asalah', 'woocommerce'),
            'JZZ' => __('Al-Aziziyah', 'woocommerce'),
            'JBH' => __('Al-Baghdadiyah', 'woocommerce'),
    );
          return $states;
    }
    

    and changed the text to districts

    add_filter( 'woocommerce_default_address_fields' , 'bbloomer_override_postcode_validation' );
     
    function bbloomer_override_postcode_validation( $address_fields ) {
    
        unset($address_fields['company']);  /* remove company */
        unset($address_fields['postcode']); /* remove zip */
        /*$address_fields['postcode']['required'] = false;*/ /* postcode req */
        $address_fields['state']['required'] = true; /* state required */
        $address_fields['state']['label'] = __('District'); /* state name to dist */
        
        return $address_fields;
    }
    

    I hope this is useful for someone somewhere.


  2. There is no easy fix. It would require editing core files and it should never be done if possible. ALL postcode data is sent through the ‘cleaning’ process, which removes all unwanted data in normal use cases (also made uppercase). There are no filters to stop it.

    Now to a solution. There is no simple solution. The best way would be to make a small plugin, which would add field district and remove postcode, but only for your country. Then apply shipping logic to get prices etc. An alternative is to just ignore spaces and live without them, or replace the district names with something else.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search