Sell to Specific States Woocommerce

Coding

Filed Under: Code Snippets


In this post I am sharing WooCommerce code snippet that will allow you to restrict selling of products in a specific state(s) of a selected country. I am sharing two examples, one for United States and one for Australia.

Example 1: United States

The code snippet will allow you to sell to only three states; Florida, Indiana and Oklahoma.

add_filter( 'woocommerce_states', 'shyk_sell_specific_states' ); 
function shyk_sell_specific_states( $states ) { 
$states['US'] = array( 
'FL' => __( 'Florida', 'woocommerce' ), 
'IN' => __( 'Indiana', 'woocommerce' ), 
'OK' => __( 'Oklahoma', 'woocommerce' ), 
); 
return $states; 
} 

Example 2: Australia

The code snippet will allow you to sell only to customers of Queensland.

add_filter( 'woocommerce_states', 'shyk_sell_specific_states' ); 
function shyk_sell_specific_states( $states ) { 
$states['AU'] = array( 
'QLD' => __( 'Queensland', 'woocommerce' ), 
); 
return $states; 
} 
Advertisement

Where to put code?

Paste the code at the bottom of your child themes’ functions.php just before ?> (if it’s at the end of file)

I hope this will help you, drop a comment below if you need further assistance.

Also Read:


Tags:



Leave a Reply

Your email address will not be published. Required fields are marked *