Support » Plugin: WooCommerce » How to hide postalcode if MZ…

  • Resolved sotnas

    (@sotnas)


    Hi,

    how to hide postalcode if select country is MZ (Mozambique) ?
    I think for AO the Woocommerce already does…

    Thank you

Viewing 8 replies - 1 through 8 (of 8 total)
  • Stef

    (@serafinnyc)

    Does Mozambique only have one zipcode for the entire country?

    sotnas

    (@sotnas)

    Hi Stef.

    Mozambique, don’t have Zip codes…
    Angola don’t have to…

    Stef

    (@serafinnyc)

    Are you only selling to these areas or worldwide?

    sotnas

    (@sotnas)

    Nop Stef.

    I selling for other countries with Zip code.
    Only need:
    – When i choose ‘MZ’ our ‘AO’ country, unset/hide the Zip code field

    Thank you

    • This reply was modified 9 months ago by sotnas.
    Stef

    (@serafinnyc)

    Since you sell globally I would suggest making it optional rather than get specific country.

    Try this, test and let me know.

    add_filter( 'woocommerce_default_address_fields', 'ssd_postcode_fields' );
    function ssd_postcode_fields( $address_fields ) {
        $address_fields['postcode']['required'] = false;
    
        return $address_fields;
    }

    Know that you should add this to a child theme and not your parent or current theme. Anytime you get into customizing this is the proper process.

    Plugin Support RK

    (@riaanknoetze)

    @serafinnyc is spot on with his code; If you’re not comfortable in working with code yourself, you could look into using this extension from Woo: https://woocommerce.com/products/woocommerce-checkout-field-editor/ (It does pretty much the same thing although you have a user interface in the WordPress admin area 🙂

    sotnas

    (@sotnas)

    Ok,
    And thank you
    If i add one conditional statement for ‘MZ’ country

    
    add_filter( 'woocommerce_default_address_fields', 'ssd_postcode_fields' );
    function ssd_postcode_fields( $address_fields ) {
    	$country = WC()->customer->get_billing_country();
    	    if( $country === 'MZ' ){
                    $address_fields['postcode']['required'] = false;
            }
        return $address_fields;
    }
    

    But don’t work 🙁

    Plugin Support Jesse Pearson

    (@jessepearson)

    Automattic Happiness Engineer

    @sotnas According this, postal codes are used in MZ:
    https://en.youbianku.com/Mozambique

    However, this code would do what you need:

    
    function jp_set_mz_postcode_not_required( $countries ) {
    
    	$countries['MZ'] = array(
    		'postcode' => array(
    			'required' => false,
    			'hidden'   => true,
    		),
    	);
    	return $countries;
    }
    add_filter( 'woocommerce_get_country_locale', 'jp_set_mz_postcode_not_required' );
    
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to hide postalcode if MZ…’ is closed to new replies.