Woocommerce - Remove 3 provinces from checkout dropdown [duplicate] - woocommerce

This question already has answers here:
Remove specific states of a country on Woocommerce Checkout
(2 answers)
Closed 7 months ago.
I would like to remove 3 Canadian Provinces from the Woocommerce Checkout page dropdown boxes, so those provinces cant be selected when ordering.
I found the answer for US States here on how to remove US states from the woocommerce checkout drop down boxes (code copied below):
add_filter( 'woocommerce_states', 'custom_us_states', 10, 1 );
function custom_us_states( $states ) {
$non_allowed_us_states = array( 'AK', 'HI', 'AA', 'AE', 'AP');
// Loop through your non allowed us states and remove them
foreach( $non_allowed_us_states as $state_code ) {
if( isset($states['US'][$state_code]) )
unset( $states['US'][$state_code] );
}
return $states;
}
I am wondering how to adjust this for Canadian Provinces?
The code for Canada is CA.
The codes for the provinces I want to remove are:
NU - Nunavut 2. NT - Northwest Territories 3. YT - Yukon
Territory

add_filter( 'woocommerce_states', 'custom_ca_states', 10, 1 );
function custom_ca_states( $states ) {
$non_allowed_ca_states = array( 'NU', 'NT', 'YT' );
// Loop through your non allowed us states and remove them
foreach( $non_allowed_ca_states as $state_code ) {
if ( isset($states['CA'][$state_code]) ) {
unset( $states['CA'][$state_code] );
}
}
return $states;
}

Related

Hide product price on country out of GB but show only IF products have a TAG

currently I hide all product price for out of GB but I would like to show product price only if they have a TAG.
my code to hide product price out of GB is anyone can help me ? please ?
/** hiding prices if outside of gb*/
add_filter( 'woocommerce_get_price_html', 'country_geolocated_based_hide_price', 10, 2 );
function country_geolocated_based_hide_price( $price, $product ) {
// Get an instance of the WC_Geolocation object class
$geo_instance = new WC_Geolocation();
// Get geolocated user geo data.
$user_geodata = $geo_instance->geolocate_ip();
// Get current user GeoIP Country
$country = $user_geodata['country'];
return $country !== 'GB' ? '' : $price;
}
/** fin hiding prices if outside of gb*/
thank you
show product price for out of GB and show product price only if they have a TAG
You can modify your code this way:
// This will show product prices for products with a specific tag and outside of GB
add_filter( 'woocommerce_get_price_html', 'show_price_based_on_country_and_tag', 10, 2 );
function show_price_based_on_country_and_tag( $price, $product ) {
// Get an instance of the WC_Geolocation object class
$geo_instance = new WC_Geolocation();
// Get geolocated user geo data.
$user_geodata = $geo_instance->geolocate_ip();
// Get current user GeoIP Country
$country = $user_geodata['country'];
// Check if the product has the tag 'my_tag'
$tag = 'my_tag';
$has_tag = has_term( $tag, 'product_tag', $product->get_id() );
// This will show price only if the user is outside of GB and the product has the tag 'my_tag'
return ( $country !== 'GB' && $has_tag ) ? $price : '';
}
Thank you Faisal ! you help me so much :)
first impression, I thought it working but prices are hided on GB as well is product haven't tag.
GB = can see everything
outside of GB = can see prices only product has the tag

WooCommerce: how to add suffix to attribute value? [duplicate]

This question already has answers here:
Adding a custom text next to a specific product attribute label in WooCommerce
(2 answers)
Closed 10 months ago.
We are building a wine-shop and have a few typical attributes like alcohol, sugar, acid that have values. These attributes are measured in units - e.g. alcohol in vol.%, sugar in g/litre, etc.
The "additional information" tab shows the attribute names and values but just the values without the unit right after.
Consequently we want to add a suffix for each attribute via a snippet/hook in functions.php and have tried with below code:
add_filter( 'woocommerce_attribute_label', 'custom_attribute_label', 10, 3 );
function custom_attribute_label( $label, $name, $product ) {
$taxonomy = 'pa_'.$name;
if( $taxonomy == 'alk' )
$label .= '<span class="custom-label"> vol.%</span>';
return $label;
}
but this does not generate any output on frontend.
Any ideas and/or feedback to solve/tweak this are appreciated!
The condition should be if ( $taxonomy === 'pa_alk' )
I modified the code, now it's working:
add_filter( 'woocommerce_attribute_label', 'custom_attribute_label', 10, 3);
function custom_attribute_label( $label, $taxonomy, $product ) {
if( $taxonomy == 'pa_alk' )
$label .= '<span class="custom-label"> vol.%</span>';
return $label;
}

How to exclude product categories in WooCommerce? [duplicate]

This question already has answers here:
Exclude a product category from the loop in Woocommerce
(2 answers)
Exclude a specific term from product categories widget in Woocommerce
(1 answer)
Closed 2 years ago.
how can I exclude woo product categories only in the front of the shop with the function?
Can anyone help me?
The following code hides categories on the store page and the accordion using the ID and name of each category.
For the accordion, the category ID must be used, which will be placed where it says array ('1', '2'); and for the store you must use the name of the category and it will be placed where it says array ('category-name', 'category-name')
// Hide in Accordion
add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'oaf_wc_exclude_categories_from_list_widget' );
add_filter( 'woocommerce_product_categories_widget_args', 'oaf_wc_exclude_categories_from_list_widget' );
function oaf_wc_exclude_categories_from_list_widget( $cat_args ) {
$cat_args['exclude'] = array('1','2'); // array with categories ids to exclude
return $cat_args;
}
// Hide in Store
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
// if a product category and on the shop page
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( 'name-category', 'name-category' ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
Tested and working, the code goes to the end in the fuctions.php file of your theme o your child theme.
Here you have an image where you can see where to find the id and name of categories. Regards.
name category, id category

How to set minimum weight of order in woocommerce? [duplicate]

This question already exists:
How to Set Purchase Limits which a minimum weight requirement before checking out in WordPress Woo commerce/?
Closed 3 years ago.
How to get Woo Commerce product variation values in WordPress .I've three values 250g,500g and 1kg.
I've restrict on checkout process 500g.
Add this code to functions.php. You may check article here
add_action( 'woocommerce_check_cart_items', 'cldws_set_weight_requirements' );
function cldws_set_weight_requirements() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set the minimum weight before checking out
$minimum_weight = 500;
// Get the Cart's content total weight
$cart_contents_weight = WC()->cart->cart_contents_weight;
// Compare values and add an error is Cart's total weight
if( $cart_contents_weight < $minimum_weight ) {
// Display our error message
wc_add_notice( sprintf('<strong>A Minimum Weight of %s%s is required before checking out.</strong>'
. '<br />Current cart weight: %s%s',
$minimum_weight,
get_option( 'woocommerce_weight_unit' ),
$cart_contents_weight,
get_option( 'woocommerce_weight_unit' ),
get_permalink( wc_get_page_id( 'shop' ) )
),
'error' );
}
}
}

Setting up an error message for minimum quantity in WooCommerce

I set up a minimum quantity for a product category in WooCommerce. In my case I set the minimum quantity to 5 items. The code I use works fine but I would like to add two error messages for the customer:
1) If the customer tries to change the the quantity to less than the minimum by clicking the "-" symbol I would like to have something like: "The minimum quantity of this product is 5, please add at least 5 items to the basket"
2) If the customer clicks the "add to the basket" button I would like to have something like this: "There is a minimum quantity of 5 items for this product. Please check your basket"
Is there some code I can add to my actual code?
add_filter( 'woocommerce_quantity_input_args', 'min_qty_filter_callback', 20, 2 );
function min_qty_filter_callback( $args, $product ) {
$category = 'Noten'; // The targeted product category
$min_qty = 5; // The minimum product quantity
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
if( has_term( $category, 'product_cat', $product_id ) ){
$args['min_value'] = $min_qty;
}
return $args;
}
For add to cart validation add the follows code snippet to validate your item quantity -
function add_to_cart_validation( $flag, $product_id, $qunatity ) {
if( $qunatity < 5 ) { // if quantity less than 5
wc_add_notice( __( 'There is a minimum quantity of 5 items for this product. Please check your basket', 'text-domain' ), 'error' );
$flag = false;
}
return $flag;
}
add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_validation', 10, 3 );

Resources