Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce: Pre-select and restrict to one state and city on checkout

I'm setting up a store that only sells to one city. I'm trying to figure out how I can restrict the city option to a pre-filled field.

I've already limited the country and using the following code in my functions.php:

add_filter( 'default_checkout_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_state', 'change_default_checkout_state' );

function change_default_checkout_country() {
  return 'PK'; // country code
}

function change_default_checkout_state() {
  return 'SD'; // state code
}

And I've pre-selected the state using the following:

add_filter( 'woocommerce_states', 'bbloomer_custom_woocommerce_states' );

function bbloomer_custom_woocommerce_states( $states ) {
$states['PK'] = array(
'SD' => 'Sindh',
);
return $states;
}

But... Now comes the city and I am lost. I've tried using $fields['billing']['billing_city'] as well as $fields['billing']['billing_city']['value'] to no avail.

I want the customer to be limited to Karachi. I am not very familiar with WooCommerce filters, so I need help on achieving this.

like image 366
AliIshaq Avatar asked Aug 01 '17 17:08

AliIshaq


People also ask

How do I restrict states in WooCommerce?

To add a shipping restriction by the state in WooCommerce, click the “Add Restriction” button. Once you click the button, a new rule will be added, and you can customize it to match your requirements so that the users from the selected states won't be able to place the order in your store.

How do I add a conditional field in WooCommerce checkout?

Adding a conditional field in WooCommerce checkout is simple. First, you need to login to your WordPress dashboard and go to WooCommerce > Checkout Fields. Here, you will see a list of all the default fields that are displayed in the checkout process. To add a new field, simply click on the Add Field button.

How do I change the city field to a dropdown in WooCommerce?

Changing the city field to a simple dropdown You are able to set up both the display value to the customer and the so-called 'index' or 'key' which is what the 'City' condition will match against (when using the Advanced Shipping plugin). The below code needs to go in your theme/child theme's functions. php file: <?


1 Answers

Since woocommerce 3, default_checkout_country and default_checkout_state filter hooks are now deprecated and replaced.

Also in your last function, if you want to restrict just to one city, you should not include your city in the array of states. Instead you should return only one possible value.

So this will be your code:

// default checkout country
add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_shipping_country', 'change_default_checkout_country' );
function change_default_checkout_country() {
    return 'PK'; // country code
}

// default checkout state
add_filter( 'default_checkout_billing_state', 'change_default_checkout_state' );
add_filter( 'default_checkout_shipping_state', 'change_default_checkout_state' );
function change_default_checkout_state() {
    return 'SD'; // state code
}

// Setting one state only
add_filter( 'woocommerce_states', 'custom_woocommerce_state', 10, 1 );
function custom_woocommerce_state( $states ) {
    // Returning a unique state
    return array('PK' => array('SD' => 'Sindh'));
}

// Only one city at checkout
add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields', 10, 1 );
function custom_checkout_fields( $fields ) {

    $fields['billing']['billing_city']['type'] = 'select';
    $fields['billing']['billing_city']['options'] = array('Karachi' => 'Karachi');
    $fields['shipping']['shipping_city']['type'] = 'select';
    $fields['shipping']['shipping_city']['options'] = array('Karachi' => 'Karachi');

    return $fields;
}

The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works for wooCommerce versions 3+

Once added the code above and saved, you will need in WooCommerce > Settings > General to set locations this way:

enter image description here

Then you will get something like this on checkout:

enter image description here

Both dropdown have only one value… So you get what you are expecting.

like image 53
LoicTheAztec Avatar answered Oct 19 '22 13:10

LoicTheAztec