Why Woocommerce does not deal right with 'unset'? - woocommerce

I tried to just unset a country from the checkout dropdown menu "billing country".
In fact, the country "GB" is more like hidden and not removod (see picture). Can you hit me up, what is going on here?
My goal is to full hide or remove the country "GB" only from checkout page dropdown menu.
add_filter( 'woocommerce_countries', 'Remove_GB', 10, 1 );
function Remove_GB( $countries ) {
unset($countries["GB"]);
$emptyRemoved = array_filter($countries);
return $emptyRemoved;
}
The country is still there only the text is being hidden:
enter image description here

In a WordPress filter, the first param of the function is the value that you will change the content, so you need to return it to Woocommerce knows you changed its value:
add_filter( 'woocommerce_countries', 'Remove_GB', 10, 1 );
function Remove_GB( $countries ) {
unset($countries["GB"]);
return $countries;
}
PS.: Always check for the value you are returning or another hooks that are listening to the same filter.

Related

How do I add product attributes to the product page in WooCommerce?

I'm using WooCommerce and Elementor Pro. Is there any way to add specific product attributes (e.g. gross and net weight) below the Add to cart button?
It seems like an obvious thing but I haven't found options or snippets for it.
First you add attributes to product. Then you use this snippet in functions.php:
// maybe remove tab with attributes
add_filter( 'woocommerce_product_tabs', 'remove_info', 100, 1 );
function remove_info( $tabs ) {
unset($tabs['additional_information']);
return $tabs;
}
// show attributes
add_action( 'woocommerce_single_product_summary', 'summary_attributes', 35 );
function summary_attributes() {
global $product;
if ( $product->has_attributes() ) {
wc_display_product_attributes( $product );
}
}
Setting Up WooCommerce Attributes
go to Products > Attributes
Add the attribute name
Slug will automatically be created, and we can leave the rest of these options untouched.
Click “Add attribute” button and your attribute will be added.
Configure The Attributes
After creating attribute, now we can add different variation on your product. Click on the Configure Terms option.
Enter the variation
Add some short description
Click on the add new tab
To understand it in a better way you can follow this tutorial

WooCommerce Upsells Same Order as Admin

really hoping someone can help with this as I thought it would be far more simple!
Long story short, I have created a script that populates the upsells of WooCommerce products, all works great using the API and they are there. They show on the product page as expected but in a completely different order to how they were inputted in the admin area and I cannot seem to find a way for the order to follow admin?
function filter_woocommerce_upsells_orderby( $orderby ) {
return 'menu_order';
};
add_filter( 'woocommerce_upsells_orderby', 'filter_woocommerce_upsells_orderby', 10, 1 );
Above is the hook I have found but from the options I have found such as menu order / id / price etc, there is not simply an overide option to ignore the order and just take them as they are in admin!?
Please help!
I also encountered this problem. And i have an idea.
I checked all wordpress parameters about Order & Orderby.Link is https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters.
And i use the paramteters name "none",It can make your orderby not based on any sorting rules.In other words, it is sorted according to upsells. here is the code.
// ORDER BY
add_filter( 'woocommerce_upsells_orderby', 'filter_woocommerce_upsells_orderby', 10, 1 );
function filter_woocommerce_upsells_orderby( $orderby ){
return "none";
};
// ORDER
add_filter( 'woocommerce_upsells_order', 'filter_woocommerce_upsells_order', 10, 1 );
function filter_woocommerce_upsells_order(){
return 'asc'; // Default is 'desc';
};
But it is still chaotic, when adding any product to upsells, it is still random. Therefore I also used a plug-in "WooCommerce Drop/Drag For Upsells Cross-Sells", which allows you to drag your products in upsell at will.
If you have any question you can ask me.
Thanks.

Notice in product page only on selected products in Woo?

I have one issue. How to show notice or text , in product page, only for selected products?
i know that must to insert some function in functions.php, but dont know how to do that. Can anyone give me some dirrections? Thanks
I found this function:
// adds notice at single product page above add to cart
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo '<p id="rtrn">30-day return policy offered. See Terms and Conditions for details.</p>';
}
and CSS:
#rtrn {
text-align: center;
font-style: italic;
}
This function show message in every product, but i need only in selected products..
so how to show this message only on selected products? How to select products where message need to be showed?
Find the id of the product you want the message to appear by hovering on the product name in your product listings or by looking at the URL while editing the product and add the following code in your functions.php file
add_action( 'woocommerce_single_product_summary', 'return_policy', 20);
function return_policy() {
if ( is_single( 'product_id' ) ) {
echo '<p>30-day return policy offered. See Terms and Conditions for details.</p>';
}
}

Hide WooCommerce Add To Cart button when price is zero

I need to hide my add to cart button on the condition where the price = 0.00. If the price is greater than 0.00 i would like the button to reappear.
However i also use the WooTheme Wish List extension and i need the add to wish list button to remain. I would also like to change the text from Free to "Subscription Product" when the value is 0.00.
This is only needed on the single product page as we do not show the add to cart anywhere else.
Currently if the price is set to 0.00 i get the text at the top of the single product page saying "free" and the add to cart button remains as does the add to wish list button. If i remove the price all together the buttons also disappear hence why i need to "hide" the button to retain the wish list one.
I have tried the following Code inserted into my functions.php file
<?php
/*
* Swop the 'Free!' price notice and hide the cart with 'POA' in WooCommerce
*/
add_filter( 'woocommerce_variable_free_price_html', 'hide_free_price_notice' );
add_filter( 'woocommerce_free_price_html', 'hide_free_price_notice' );
add_filter( 'woocommerce_variation_free_price_html', 'hide_free_price_notice' );
function hide_free_price_notice( $price ) {
remove_action ( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
return 'POA';
}
?>
However, although it did work in that it changed the "Free" text to "POA" and also hid the "Add to Cart button" the "Add to Wishlist" button also disappeared.
If i changed the value to 0.01 everything appeared again so it nearly there... i just need the Wish List button to stay.
Any help or pointers would be greatly appreciated. Many Thanks...
Just for info, the reason i need to do this is because we run a subscription service, so products that are included in a subscription service do not need a price but do need to be shown so they can be added to a wish list. However sometimes we sell the products off so when they have a value i need the button to reappear so they can be added to the cart.
Simply having zero doesn't work because believe it or not people think they are free and try to add them to the cart to purchase !
Please add below code in your active theme functions.php and check it.
function wpcustom_is_purchasable( $purchasable, $product ){
if( $product->get_price() == 0 )
$purchasable = false;
return $purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'wpcustom_is_purchasable', 10, 2 );

Woocommerce Add Product To Cart Multiple Times But As Different items

I have some custom code that adds an item to the cart, a variable product. If i add the same product twice it just increases the quantity. I would like it to add the product again as a different item in the cart.
How would i go about doing that? The item is being added to the cart using a link like domain.com?add-to-cart=3434&variation_id=4434 and so on.
The system i have developed is a product designer. So i may want to pick the same product but design it in different ways, and then add the same variation to the cart.
Is there also a way to send a unique identifier in the url to split these items up?
I want to do it by using add_to_cart but every time i do that with the variation attributes, the shipping ends up with a bug, it basically can't seem to find shipping methods:
$woocommerce->cart->add_to_cart(522,1, 523,array("attribute_colour" => "colour","attribute_size" => "a3", "attribute_invoice-numbering" => "yes", "attribute_quantity-column" => "yes", "attribute_cc-type" => "duplicate"));
While that code adds the item to the cart, it causes the following:
Warning: Invalid argument supplied for foreach() in /home/****/public_html/wp-content/plugins/woocommerce/includes/class-wc-shipping.php on line 291
There doesn‘t seem to be any available shipping methods. Please double check your address, or contact us if you need any help.
It doesn't make any sense, because the standard add to cart code in woocommerce uses add_to_cart and has none of these issues. The same if i use a URL, it works fine!
Try this !
/*
* #desc Force individual cart item
*/
function force_individual_cart_items( $cart_item_data, $product_id ){
$unique_cart_item_key = md5( microtime().rand() );
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data','force_individual_cart_items', 10, 2 );
/*
* #desc Remove quantity selector in all product type
*/
function remove_all_quantity_fields( $return, $product ) {
return true;
}
add_filter( 'woocommerce_is_sold_individually', 'remove_all_quantity_fields', 10, 2 );
I can't tell yet if the shipping array issue is related. But, if you take a look at the beginning of the add_to_cart() method in the cart class:
// Generate a ID based on product ID, variation ID, variation data, and other cart item data
$cart_id = $this->generate_cart_id( $product_id, $variation_id, $variation, $cart_item_data );
// See if this product and its options is already in the cart
$cart_item_key = $this->find_product_in_cart( $cart_id );
Basically, what we're seeing here is that the item must be completely unique in order to add it to the cart again. Otherwise, it will just up the quantity of the item already in the cart. So, to get around this, you will want to make the add_to_cart() parameters unique... probably via the final, $cart_item_data array.

Resources