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…', '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
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
and changed the text to districts
I hope this is useful for someone somewhere.
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.