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 );
Related
I've been getting great help here during the year, regarding a woocommerce issue where I needed an automatic fee added based on total cart height in this thread and then later updated with width in this thread.
Now my only issue is that I need to exclude some shipping classes from the function, as they are shipped differently and don't need a Bulky fee added.
I've been playing around with the answer to "Add a fee based on cart items dimensions in Woocommerce", trying to add a class variable to it:
// Initializing variables
$total_height = 0;
$class = 1390,1392,1389;
$apply_fee = false;
But it instantly breaks the website. I've been searching on stackoverflow.com and Google for a way to do this but no luck, and I'm still not qualified enough for that type of advanced editing of code.
Appreciate any help I can get.
You can't have a list of ids like that with just commas between them. To fix this put the ids in an array. Within the foreach loop, check the shipping class id of the product, and if it's in the array exclude it.
$exclClasses = array(1390,1392,1389);
// Checking item with
if ( $cart_item['data']->get_width() > $width_threshold ) {
$apply_fee = true;
}
// make sure product isn't in excluded shipping classes
if (in_array( $cart_item['data']->get_shipping_class_id(), $exclClasses))
{
$apply_fee = false;
}
or to use 1 statement instead of 2
if ( $cart_item['data']->get_width() > $width_threshold && !in_array( $cart_item['data']->get_shipping_class_id(), $exclClasses)) {
$apply_fee = true;
}
I need to apply different sorting options for different categories in Woo-commerce. For e.g. Category-A has default sorting by Name, Category-B has default sorting by Price Low to High, and Category-C sorted default by Newness.
you can use woocommerce_default_catalog_orderby filter to change the default order-by value.
To check which category is being displayed. you can use is_product_category function.
The value inside the is_product_category should be your category slug and of course you can use several category to check at once by using this function as follow:
is_product_category( array( 'category-a', 'category-b' ) )
add_filter('woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby');
function custom_default_catalog_orderby()
{
if (is_product_category('category-a')) {
return 'price'; //you can use either date or popularity or rating , price or price-desc
}
if (is_product_category('category-c')) {
return 'rating'; //you can use either date or popularity or rating , price or price-desc
}
if (is_product_category('category-c')) {
return 'title'; //you can use either date or popularity or rating , price or price-desc
}
}
the code above is tested you just need to change the category slug based on your need and place the code inside your functions.php
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.
I'm more or less a total newbie to php. My goal is to get the names of items from the logged in user's cart and dynamically populate a GravityForms template with the information. I've successfully managed to do that with the code below, but there are three things that I'm failing to do. 1: I only want to populate the form with all items of a certain variation. 2: the echo function will list all of the item names, but not inside the relevant field, and the return function will populate the field, but only with the first item name. 3: I'd like to have the output items listed with some form of separator in between each item name. Here's what I have so far:
<?php
add_filter( 'gform_field_value_beat_names', 'beat_names_function' );
function beat_names_function( $value ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach ($items as $item) {
$product_variation_id = $item['variation_id'];
$license_id = new WC_Product($product_variation_id);
$license_string = $license_id->get_formatted_name();
$beat_id = $item['data']->post;
$beat_name = $beat_id->post_title;
if (strpos($license_string, 'basic') !== false) {
echo $beat_name;
}
}
}?>
As you can see, I'm attempting to use strpos to isolate the particular item variation I want to target, using a certain word found in the name of the variation option, being "basic". I'm sure there's a more secure way of doing that, but it works for now. The problem lies with the return function I set up inside the conditional strpos statement, being that it will still just return the entire list of cart items, as opposed to only the items that I'm trying to isolate with the strpos conditional.
The ultimate goal is to create a license agreement that is dynamically populated with the relevant information, including the names of the items being licensed. The item variations are different license options that I have available for the products in my store. The purpose of the above code is to filter cart items by license type so that the wrong item names don't get listed on the wrong license agreement at checkout.
Any tips would be appreciated
Figured it out after some more tinkering. I broke it down step by step to help anyone as clueless as I am
add_filter( 'gform_field_value_basic_beat_names', 'basic_beat_names_function' );
function basic_beat_names_function( $value ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
// the item names you want will be stored in this array
$license_check = array();
// Loop through cart items
foreach ($items as $item) {
// get the id number of each product variation in the cart. this won't work for "simple" products
$product_variation_id = $item['variation_id'];
// translate that id number into a string containing the name of the product, and the options applied to the product
$license_id = new WC_Product($product_variation_id);
$license_string = $license_id->get_formatted_name();
// check to see if the word "basic" is found anywhere in that string. "basic" is contained in the name of the product option being targeted
if (strpos($license_string, 'basic') !== false) {
// if the above condition is met, fetch the name of any products in the cart that have that option applied. The product name will be fetched from the title of the product's page
$beat_id = $item['data']->post;
$beat_name = $beat_id->post_title;
// store the retrieved product names in the $license_check array
$license_check [] = $beat_name;
}
}
// pull the stored product names from the $license_check array, and format them using implode and concatenation, then return the final result to the form field
return "\"" . implode("\", \"", $license_check) . "\"";
}
As a warning, the strpos method is a little hacky. It'll only work properly if your string is specific enough to target ONLY the option you're looking for.
As an example, here's the format for the product variation string being fed into the strpos function:
item-name-option-one-name-option-two-name – variation #xxx of item page title
So, if you want to filter items ONLY by option one, your safest bet would be to write the strpos function like so:
if (strpos($license_string, 'option-one-name') !== false) {
//do something
}
When all is said and done, the final result should look like: "item1", "item2", "item3", etc.
Then, to do the same with any other option, and output the result in a different field, or separate form altogether, I'll just duplicate this code, and replace any mention of the word "basic" with some different unique string contained in the other option. Don't forget to configure the gravity forms field as necessary too.
I have the following function that adds a column in the Sales Log so that I may show the Order Notes. Adding the column is good and showing the id of each item ($item->id) as test is good. Where I am getting stuck is how to display the actual order notes for each item.
Tried numerous things including ($purchlog_notes->notes);
This adds the column (all good)
`function addPurchaseLogColumnHead( $columns ){
$columns['OrderNotes']=__('Order Notes');
return $columns;
}
add_filter( 'manage_dashboard_page_wpsc-purchase-logs_columns', 'addPurchaseLogColumnHead');`
This is where I want to show Order Notes content for each sale (issue)
`function addPurchaseLogColumnContent( $default, $column_name, $item ) {
if ($column_name == 'OrderNotes'){
echo $item->notes;
}
}
add_filter( 'wpsc_manage_purchase_logs_custom_column' , 'addPurchaseLogColumnContent', 10, 3);`
Appreciate any ideas.
Thank you.