Showing a Single City on the WooCommerce Payment Page

It is possible to show a single city or certain cities on the WooCommerce payment page. Some e-commerce startups may only sell to certain cities and regions. For this reason, they may want to prevent their customers from ordering from different cities. Unfortunately, the WooCommerce infrastructure only restricts countries. If you want to block only […]

Showing a Single City on the WooCommerce Payment Page

It is possible to show a single city or certain cities on the WooCommerce payment page. Some e-commerce startups may only sell to certain cities and regions. For this reason, they may want to prevent their customers from ordering from different cities. Unfortunately, the WooCommerce infrastructure only restricts countries. If you want to block only certain cities on the payment page, it is possible to do so with the code below.

/**
 * ------------------------------------------------------
 * Showing a Single City on the WooCommerce Payment Page
 * ------------------------------------------------------
 */

add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
function custom_woocommerce_states( $states ) {
  $states['US'] = array(
    'NYC' => 'New York', 
  );
  return $states;
}

When you look at the information in this code snippet, you can see that I chose the city of “News York” as an example. You can add your own city based on this example. By changing the field ‘NYC’ => ‘News York’ in the code snippet to example ‘BOS’ => ‘Boston’, you can show only Boston.

So how is this code added? There are two ways to do this;

The first method is to add it to the functions.php file. Select the WordPress admin panel> Appearance> Theme editor> functions.php file and add this code to the appropriate field.

adding-functions-code

In the second step, go to the WordPress management panel> Plugins> Add New tab and type Code Snippets in the search field to install the relevant plugin. Then add a new content by going to the Snippets tab in our WordPress dashboard. See image for sample usage;

adding-code-1

Make sure to make a backup of the functions.php file before using the app (no need for method in step 2). Join me in the comments area for all the questions you have in mind.

One thought on “Showing a Single City on the WooCommerce Payment Page”

Leave a Reply

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