The country of Vietnam in WooCommerce does not have defined states, so I
added some states to my checkout page.
This is my code:
add_filter( 'woocommerce_states', 'vietnam_cities_woocommerce' );
function vietnam_cities_woocommerce( $states ) {
$states['VN'] = array(
'HCM' => __('Hồ Chí Minh', 'woocommerce') ,
'HANOI' => __('Hà Nội', 'woocommerce') ,
'HAIPHONG' => __('Hải Phòng', 'woocommerce') ,
);
return $states;
}
It do work as I would like, but it is an optional field for Vietnam.
How to make this state field as required for Vietnam?
Any help is appreciated.
The following function will make for Vietnam the state field as a required in woocommerce:
add_filter( 'woocommerce_get_country_locale', 'custom_country_locale', 10, 1 );
function custom_default_address_fields( $locale ) {
$locale['VN']['state']['required'] = true;
return $locale;
}
Code goes in functions.php file of your active child theme (active theme). Tested and works.
Explanations: Each country have specific "locale" fields settings in WooCommerce. We are adding/altering the default WooCommerce country locale settings that are defined on WC_Countries get_country_locale() method
Related
I came across with this How to blank all WooCommerce checkout fields by default except country? similar question which has an answer code that meet my needs, but this code will blank all woocommerce shipping and also billing checkout field when customer click checkout button.
if I want the shipping fields only to be blank, how the code will looks like?
To blank all WooCommerce shipping checkout fields, you will use the following:
add_filter('woocommerce_checkout_get_value', 'checkout_get_value_filter', 10, 2 );
function checkout_get_value_filter( $value, $input ) {
if ( strpos($input, "shipping_") === 0 ) {
return '';
}
return $value;
}
Or also this specifying each related shipping field key:
add_filter('woocommerce_checkout_get_value', 'checkout_get_value_filter', 10, 2 );
function checkout_get_value_filter( $value, $input ) {
$key_fields = array(
'shipping_first_name',
'shipping_last_name',
'shipping_company',
'shipping_country',
'shipping_address_1',
'shipping_address_2',
'shipping_city',
'shipping_country',
'shipping_state',
'shipping_postcode',
);
if ( in_array($input, $key_fields) ) {
return '';
}
return $value;
}
Code goes on functions.php file of your active child theme (or active theme). It should works.
I thought this would be easy but I'm not getting the results I want. Basically I have 2 countries in woocommerce CA and US. I'm trying to remove one conditionally, and I can do that with the following code below. However, when I go from 2 countries to 1, the dropdown menu still appears. An odd thing I'm noticing with the below code as well, is that if I go into my Woocommerce settings, then the country that is removed with this code is also removed from the "Sell to specific countries" options.... not sure what's going on. Thanks in advance.
add_filter( 'woocommerce_countries', 'custom_woocommerce_countries_limit');
function custom_woocommerce_countries_limit( $countries ) {
/*
will place a conditional here if x then remove country
*/
unset($countries['CA']);
$countries = array(
'US' => __( 'United States', 'woocommerce' )
);
return $countries;
}
EDIT: Using this hook may be close to the answer, but when I use this one, The states don't turn into a dropdown...?
add_filter( 'woocommerce_countries_allowed_countries', 'custom_woocommerce_countries_limit');
You can use woocommerce_countries_allowed_countries filter hook, but you should need some additional code to force the state fields to be a state dropdown (select field):
add_filter( 'woocommerce_countries_allowed_countries', 'filter_allowed_countries_conditionally');
function filter_allowed_countries_conditionally( $countries ) {
// will place a conditional here if x then remove country
if( true ) {
$countries = array( 'US' => __( 'United States', 'woocommerce' ) );
} else {
$countries = array( 'CA' => __( 'Canada', 'woocommerce' ) );
}
return $countries;
}
// Force billing state field type to be a dropdown
add_filter( 'woocommerce_billing_fields', 'filter_billing_state_fields', 100, 1 );
function filter_billing_state_fields( $fields ) {
$fields['billing_state']['type'] = 'state';
return $fields;
}
// Force shipping state field type to be a dropdown
add_filter( 'woocommerce_shipping_fields', 'filter_shipping_state_fields', 100, 1 );
function filter_shipping_state_fields( $fields ) {
$fields['shipping_state']['type'] = 'state';
return $fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
In Woocommerce, I have been able to remove postcode checkout field using this code:
add_filter( 'woocommerce_default_address_fields', 'custom_override_default_address_fields' );
function custom_override_default_address_fields( $address_fields ) {
unset($address_fields['postcode']);
return $address_fields;
}
How can I remove the postcode field from the shipping calculator in cart page?
This can be done adding the following:
add_filter( 'woocommerce_shipping_calculator_enable_postcode', '__return_false' );
Code goes in function.php file of your active child theme (or active theme). tested and works.
add_filter( 'woocommerce_default_address_fields', 'custom_override_address_fields', 999, 1 );
function custom_override_address_fields( $address_fields ) {
// set as not required
$address_fields['postcode']['required'] = false;
// remove validation
unset( $address_fields['postcode']['validate'] );
return $address_fields;
}
(this is the real code that devalidates the postcode)
I'm trying to change the fields placeholder of "billing address 2" in the checkout page, using the following code:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields',9999 );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_address_2']['placeholder']="dssfsd";
return $fields;
}
it changes only for a moment than return back to it's defualt value.
see the following video (10s):
https://www.youtube.com/watch?v=-qOZ67gFQ98
The only way to get this is using woocommerce_default_address_fields, but it will change both billing and shipping placeholder for Address 2 checkout fields:
add_filter( 'woocommerce_default_address_fields', 'custom_override_default_checkout_fields', 10, 1 );
function custom_override_default_checkout_fields( $address_fields ) {
$address_fields['address_2']['placeholder'] = __( 'dssfsd', 'woocommerce' );
return $address_fields;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works
Now if you want to change only the Address 2 billing field placeholder you will:
Unset default Address 2 field placeholder (for both shipping and billing fields)
Customize only billing Address 2 field placeholder
Put back shipping Address 2 field placeholder
Here is the code on two hooked function:
add_filter( 'woocommerce_default_address_fields', 'custom_override_default_checkout_fields', 10, 1 );
function custom_override_default_checkout_fields( $address_fields ) {
// Remove labels for "address 2" shipping fields
unset($address_fields['address_2']['placeholder']);
return $address_fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields', 90, 1 );
function custom_override_checkout_fields( $fields ) {
// Add custom billing "address 2" label
$fields['billing']['billing_address_2']['placeholder'] = __( 'dssfsd', 'woocommerce' );
// Put back shipping "address 2" label
$fields['shipping']['shipping_address_2']['placeholder'] = __( 'Apartment, suite, unit etc. (optional)', 'woocommerce' );
return $fields;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works
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.
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:
Then you will get something like this on checkout:
Both dropdown have only one value… So you get what you are expecting.