I am making a woocommerce website for my supermarket which delivers only to one city(Ras Tanura). Since it is a supermarket store, I replaced Shipping with Delivery. I'm trying to restrict checking out to allow only customers how live in Ras Tanura to choose a payment method. I don't want people to pay for something that can't be delivered to them. Here is what I tried.
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 'SA'; // 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 'RT'; // 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('SA' => array('RT' => 'Ras Tanura'));
}
// Only one country 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('Ras Tanura' => 'Ras Tanura');
$fields['shipping']['shipping_city']['type'] = 'select';
$fields['shipping']['shipping_city']['options'] = array('Ras Tanura' => 'Ras Tanura');
return $fields;
}
I have added this code to my functions.php under oceanWP theme. This code does half of the job. It sure sets the default city to Ras Tanura and it can't be changed by customers, but I tried to order from another city and it accepted my order the only thing that this code has done is writing that the order is coming from Ras Tanura even though it wasn't.
how can I make my website knows the location of the user and prevent or accept checkout based on that? (accepts if the person is in Ras Tanura and prevents if he lives elsewhere)
Note that I have already set the selling location under woocommerce> setting> general to sell to specific countries and I have chosen this country to SA.