I already have US selected as my default country in the woocommerce checkout. In addition to that, I was asked to move 'US' to the very top of the country list in the checkout form.
I created a new filter and hooked into 'woocommerce_countries' hook like this:
function change_country_order_in_checkout_form($countries)
{
$countries = array('US' => $countries['US']) + $countries;
return $countries;
}
add_filter( 'woocommerce_countries', 'change_country_order_in_checkout_form' );
My list of countries gets modified correctly, but then something in WooCommerce sorts the countries alphabetically and I want to avoid that. I tried adding:
remove_filter('woocommerce_sort_countries', 'wpautop');
but that did not seem to make any difference. Any help is appreciated.
To avoid ordering, you need to use woocommerce_sort_countries filter hook this way:
add_filter('woocommerce_sort_countries', '__return_false');
And to set "US" first, try this instead:
add_filter( 'woocommerce_countries', 'change_country_order_in_checkout_form' );
function change_country_order_in_checkout_form($countries)
{
$usa = $countries['US']; // Store the data for "US" key
unset($countries["US"]); // Remove "US" entry from the array
// Return "US" first in the countries array
return array('US' => $usa ) + $countries;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Related
I am using WooCommerce Subscription and One Page Checkout plugins.
I am able to unset billing first name and last name (and even set 'required' attribute to false, so both fields are successively removed from my One page checkout.
But when I fill all other fields and I place order, it displays a validation error notice: "Billing First Name and Last Name are required" and I am not really sure how to solve this problem?
Maybe it has been set again from some functions or so? How I can solve this?
To remove billing first name and last name without any issues in checkout page, try to use the following instead:
// 1. Make required fields optional
add_filter( 'woocommerce_default_address_fields', 'customize_default_address_checkout_fields', 1000 );
function customize_default_address_checkout_fields( $fields ) {
if( is_checkout() ) {
$fields['first_name']['required'] = $fields['last_name']['required'] = false;
}
return $fields;
}
// 2. Remove unneeded billing fields
add_filter( 'woocommerce_billing_fields', 'customize_billing_checkout_fields', 1000 );
function customize_billing_checkout_fields( $fields ) {
if( is_checkout() ) {
unset($fields['billing_first_name'], $fields['billing_last_name']);
}
return $fields;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works…
It should works with One Page Checkout plugin too.
Have you tried removing it manually via child function.php as below?
add_filter( 'woocommerce_checkout_fields' , ' custom_remove_checkout_fields ' );
function custom_remove_checkout_fields( $fields ) {
unset($fields['billing']['billing_first_name']);
unset($fields['billing']['billing_last_name']);
return $fields;
}
I have solved the issue already.
The root cause is because one plugin always required Billing First Name and Last Name so I use this tip below to passby the problem:
I included this code into function.php of the theme:
add_filter('woocommerce_billing_fields', 'my_woocommerce_billing_fields');
function my_woocommerce_billing_fields($fields) {
$fields['billing_first_name']['default'] = 'newSubscriber';
$fields['billing_first_name']['custom_attributes']['readonly'] = TRUE;
$fields['billing_last_name']['default'] = 'newSubscriber';
$fields['billing_last_name']['custom_attributes']['readonly'] = TRUE;
return $fields;
}
So now your first name and your last name is always set as 'newSubscriber', it will pass the validation and now you hide it from dislaying using css:
p#billing_first_name_field, p#billing_last_name_field {
display: none;
}
paste these css into appearance -> Theme-> yourtheme-> additional css and save it.
So now everything would work as charm.
Hope it would help somebody struggling with the problem.
I am getting an empty array from the woocommerce_add_cart_item_data filter when a product is added to the shopping cart. Example added to functions.php of WordPress theme;
function TEST_post_filter($a,$b,$c) {
print_r($a); //THIS IS EMPTY ARRAY ie returns Array()
return $a;
}
add_filter('woocommerce_add_cart_item_data', 'Test_post_filter',10,3);
Any idea why this might be? I have found no reference to this issue anywhere. I have tried both basket behaviours ie Redirect to the basket page after successful addition and/ or Enable AJAX add to basket buttons on archives. I can't get my head around it. Plugins I have activated are WooCommerce, and WooCommerce Stripe Gateway.
UPDATE - Code which adds product options to product screen
function option_add_to_product() {
global $product;
//get pizza categories
$categories = get_terms( 'product_cat', array(
'hide_empty' => false,
));
$use_product = [];
for($i=0;$i<count($categories);$i++) {
if(strtolower($categories[$i]->slug) === 'bespoke_product')
array_push($use_product,$categories[$i]->term_id);
}
$include_product = false;
if(! count($use_product))
return false;
for($i=0;$i<count($use_product);$i++) {
$this_product_categories = $product->get_category_ids();
for($ii=0;$ii<count($this_product_categories);$ii++) {
if($this_product_categories[$ii] === $use_prouduct[$i]) {
$include_product = true;
break;
}
}
}
if(! $include_product)
return false;
//template to add product option
require plugin_dir_path(__FILE__) . 'templates/add_product_option.php';
}
You can see the html from add_product_option.php will be added to the product if the product has a a category with the slug "bespoke_product". Note, this method of getting a match for a product is temporary (just technical debt for now).
When the filter woocommerce_add_cart_item_data is called the data posted from the product form is available to the corresponding function. Unsanitised data might read as follows:
[extra_option_one] => 0
[extra_option_two] => 0
[extra_options_three] => 1
[extra_option_four] => 0
[order_note] =>
Each of those options will have a lookup table with a related price e.g. extra_options_three might be £1.50. This cost of this option and any others selected needs to up the price of the specific item being added to the cart. It should not be represented separately. The definition of the option added should go into the product field everywhere that purchased item is shown. I'm working through step by step, but it's not easy at the point I am at! I'm guessing the next is tha I'm going to be able to update the item in this order?
The woocommerce_add_cart_item_data hook is used to add custom data to the cart item on simple add to cart submission, like additional fields included in the <form> where "add to cart" button is located…
So if you are not submitting extra custom data from custom fields on add to cart event on (from single product pages), the first function argument is going to be an empty array…
See those related answer threads using this hook.
Can someone tell how can I remove the specific country from woocommerce. There is option in woocommerce that said selling locations with All countries and specific.
However I want to sell to all countries except the 1 country that is US for example! then how can I remove US from the countries list. As if I use "specific countries" option then I will have to add all the countries except the US which is longer process.
Is there any code you can help me with that I can put into functions of theme so that US country will not appear in the list of countries during checkout?
Try this following snippet
function woo_remove_specific_country( $country )
{
unset($country["US"]);
return $country;
}
add_filter( 'woocommerce_countries', 'woo_remove_specific_country', 10, 1 );
Reference
http://www.boopathirajan.com/remove-specific-country-woocommerce-country-list/
If you want to keep an array of countries, but you only have the keys, do something like this:
function woo_remove_specific_country( $countries ) {
global $woocommerce;
// default do not limit
$limited_countries = array_values(array_flip($countries));
// Country array to keep
$eu = $woocommerce->countries->get_european_union_countries( );
// keep countries, sort them out of the full array to keep the names
$found = array_filter($countries, function($item) use ($eu) {
return in_array($item, $eu);
}, ARRAY_FILTER_USE_KEY); // USE_KEY is essential cause we are filtering the language codes
// return the found countries
return $found;
}
add_filter( 'woocommerce_countries', 'woo_remove_specific_country', 10, 1 );
Probably a very simple question but it leaves me clueless..
On the web if found the following code for searching a custom field on the order page. What If I wanted to scope another field?
add_filter( 'woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_order_total' );
function woocommerce_shop_order_search_order_total( $search_fields ) {
$search_fields[] = 'woochimp_field';
return $search_fields;
}
Thanks
$search_fields[] Is an Array as it turns out. Every time you use $search_fields[] = 'woochimp_field'; you add to the array and makes you able to search more custom fields.
add_filter( 'woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_order_total' );
function woocommerce_shop_order_search_order_total( $search_fields ) {
$search_fields[] = 'woochimp_field';
$search_fields[] = 'woochimp_field_2'; //example
return $search_fields;
}
I'm trying to override, or simply customize, the admin orders list view.
I understood the method to customize is render_shop_order_columns in includes/admin/class-wc-admin-post-types.php but I cannot remove the action (method) from theme functions.php neither by a custom plugin in the plugins_loaded hook: always get bool(false) on
var_dump(remove_action( 'manage_shop_order_posts_custom_column', array( $GLOBALS['wc_admin_post_type'], 'render_shop_order_columns' ) ));
I see there is the woocommerce_order_item_name filter, but if I add a picture there (that's what I need), I get a wrong output since it is used in the title attribute of link to product too.
Could anyone please advice?
Thank you!
I was getting a wrong way...
Maybe the right one is to unset the column and add your own.
See here:
https://wordpress.org/support/topic/hooking-and-adding-new-column-on-woocommerce-order-admin-page
basically:
add_filter('manage_edit-shop_order_columns', 'show_custom_product_column', 15);
function show_custom_column($columns) {
$new_columns = (is_array($columns)) ? $columns : array();
//remove column
unset($new_columns['column_to_unset']);
//add custom column
$new_columns['custom_column'] = __( 'Translation', 'woocommerce' );
return $new_columns;
}
add_action('manage_shop_order_posts_custom_column', 'my_custom_column', 10, 2);
function my_custom_column($column) {
global $post, $woocommerce, $the_order;
switch ($column) {
case 'custom_column' :
// Custom code
break;
}
}