In Woocommerce I am trying to hide the product on the archive page and single product page based on category however the condition does not appear to work and just hide all the price whether I set the category or not
add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_prices', 10, 2 );
function woocommerce_remove_prices( $price, $product ) {
if(is_product_category('sold')){
$price = '';
return $price;
}
}
To make your code working you should need to use has_term() conditional function for single product pages and you will need to always return the price at the end, outside the if statement:
add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_prices', 10, 2 );
function woocommerce_remove_prices( $price, $product ) {
if( is_product_category('sold') || has_term( 'sold', 'product_cat', $product->get_id() ) )
$price = '';
return $price;
}
It works! But this will not remove the selected product variation price and everywhere you still have the add to cart buttons.
Code goes in function.php file of your active child theme (or active theme).
Instead you could use the following that will remove all prices, quantity buttons and add-to-cart buttons on that specific product category:
// Specific product category archive pages
add_action( 'woocommerce_after_shop_loop_item_title', 'hide_loop_product_prices', 1 );
function hide_loop_product_prices(){
global $product;
if( is_product_category('sold') ):
// Hide prices
remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
// Hide add-to-cart button
remove_action('woocommerce_after_shop_loop_item','woocommerce_template_loop_add_to_cart', 30 );
endif;
}
// Single product pages
add_action( 'woocommerce_single_product_summary', 'hide_single_product_prices', 1 );
function hide_single_product_prices(){
global $product;
if( has_term( 'sold', 'product_cat', $product->get_id() ) ):
// Hide prices
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
// Hide add-to-cart button, quantity buttons (and attributes dorpdowns for variable products)
if( ! $product->is_type('variable') ){
remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart', 30 );
} else {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
}
endif;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works.
Related
I need to add a suffix text to the price of a woocommerce product from a specific category while on WooCommerce cart.
Here is my code:
add_filter( 'woocommerce_cart_item_price', 'filter_woocommerce_cart_item_price', 10, 3 );
add_filter( 'woocommerce_cart_item_subtotal', 'filter_woocommerce_cart_item_price', 10, 3 );
function filter_woocommerce_cart_item_price( $wc, $cart_item, $cart_item_key ) {
if(!in_array('67',$cart_item['data']->get_category_ids()))
{
return $wc;
}
else{
return $wc . ' Monthly';
}
};
The problem is it only works on simple products. Doesn't seem to affect the prices of variable products (I tested on 3 different sites).
Any idea what I'm missing?
To make it work for product variations items in cart too, use instead the following:
add_filter( 'woocommerce_cart_item_price', 'cart_item_amounts_prefix', 10, 3 );
add_filter( 'woocommerce_cart_item_subtotal', 'cart_item_amounts_prefix', 10, 3 );
function cart_item_amounts_prefix( $amount, $cart_item, $cart_item_key ) {
if( has_term( array(67), 'product_cat', $cart_item['product_id'] ) ){
$amount .= ' ' . __("Monthly", "woocommerce");
}
return $amount;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
I would like to hide prices for products in specific categories, prices should be hidden only on shop and category page, but visible on product page, chart and subtotals.
Thank you!
This code has this functionality but I would like to apply it only on specific categories:
add_filter( 'woocommerce_after_shop_loop_item_title', 'remove_woocommerce_loop_price', 2 );
function remove_woocommerce_loop_price() {
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
You can check if the product has a category by has_term function, and it's an action hook not a filter
add_action( 'woocommerce_after_shop_loop_item_title', 'remove_woocommerce_loop_price', 2 );
function remove_woocommerce_loop_price() {
$category_name = 'the_category_name_here';
if ( has_term( $category_name, 'product_cat' ) ) {
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
}
You can get the cat name if you have the prodict id with the following:
$term = get_term_by( 'id', $cat_id, 'product_cat' );
echo $term->name;
So simply write a condition to the cat name
function remove_woocommerce_loop_price() {
if($term->name == 'your_category')
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
To hide the price based on specific product category slugs, you can use this:
// hide the price based on product categories
add_action( 'woocommerce_after_shop_loop_item_title', 'hide_price_for_specific_product_categories');
function hide_price_for_specific_product_categories() {
// set the slugs of the product categories for which prices must be hidden
$categories = array(
'clothing',
'bags',
'shoes'
);
global $product;
// if the product belongs to at least one product category present in the "$categories" array, it hides the price
if ( has_term( $categories, 'product_cat', $product->get_id() ) ) {
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
}
To hide the price based on specific product category ids, you can use this:
// hide the price based on product categories
add_action( 'woocommerce_after_shop_loop_item_title', 'hide_price_for_specific_product_categories');
function hide_price_for_specific_product_categories() {
// set the ids of the product categories for which prices must be hidden
$categories = array( 61 );
global $product;
// if the product belongs to at least one product category present in the "$categories" array, it hides the price
if ( has_term( $categories, 'product_cat', $product->get_id() ) ) {
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}
}
The code has been tested and works. Add it to your theme's functions.php.
I have this code to empty the cart before adding new item to the cart.
add_action( 'woocommerce_add_cart_item_data', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
But I want to limit this function to product id 986 and 1146. I mean when ever the customer add product id 986 or 1146, empty the cart first!
please help me i am totally noob
Try the following which will empty cart before whenadding to cart product Ids 986 or **1146**:
add_filter( 'woocommerce_add_to_cart_validation', 'custom_empty_cart', 10, 3 );
function custom_empty_cart( $passed, $product_id, $quantity ) {
if( ( ! WC()->cart->is_empty() ) && in_array( $product_id, [986, 1146] ) )
WC()->cart->empty_cart();
return $passed;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
I have a Woocommerce store set up with various product categories.
I want to apply a 20% discount to all products, which belong to the product category Cuckoo
For now all I'm trying to achieve is set a sale price in my functions.php
It tried as follows:
/*
* For a specific date, 20% off all products with product category as cuckoo clock.
*/
function cuckoo_minus_twenty($sale_price, $product) {
$sale_price = $product->get_price() * 0.8;
return $sale_price;
};
// add the action
add_filter( 'woocommerce_get_sale_price', 'cuckoo_minus_twenty', 10, 2 );
If I var_dump the result of $sale_price after the calculation I get the correct answer, however the price display on the front-end strikes out the regular price and displays the sale price as the regular price.
Is there a hook/filter I can use to achieve this?
I've also tried setting the sale price by doing:
$product->set_sale_price($sale_price);
to no avail.
The hook woocommerce_get_sale_price is deprecated since WooCommerce 3 and replaced by woocommerce_product_get_sale_price.
Also Product displayed prices are cached. When sale price is active, regular price is also active.
Try this instead:
// Generating dynamically the product "regular price"
add_filter( 'woocommerce_product_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_regular_price', 'custom_dynamic_regular_price', 10, 2 );
function custom_dynamic_regular_price( $regular_price, $product ) {
if( empty($regular_price) || $regular_price == 0 )
return $product->get_price();
else
return $regular_price;
}
// Generating dynamically the product "sale price"
add_filter( 'woocommerce_product_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
function custom_dynamic_sale_price( $sale_price, $product ) {
$rate = 0.8;
if( empty($sale_price) || $sale_price == 0 )
return $product->get_regular_price() * $rate;
else
return $sale_price;
};
// Displayed formatted regular price + sale price
add_filter( 'woocommerce_get_price_html', 'custom_dynamic_sale_price_html', 20, 2 );
function custom_dynamic_sale_price_html( $price_html, $product ) {
if( $product->is_type('variable') ) return $price_html;
$price_html = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ) . $product->get_price_suffix();
return $price_html;
}
Code goes in function.php file of your active child theme (active theme).
Tested and works on single product, shop, product category and tag archive pages.
The continuation in:
Wrong Woocommerce cart item price after setting programmatically product sale price
I realiced, you more or less need all of the following filters to make the HTML work out of the box.
add_filter( 'woocommerce_product_get_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_variation_prices_price', 'custom_dynamic_sale_price', 10, 2 );
add_filter( 'woocommerce_variation_prices_sale_price', 'custom_dynamic_sale_price', 10, 2 );
And you need to make sure you've changed the woocommerce_get_variation_prices_hash becasue of the stored transients if you want to display it correctly for variable products.
You may find the gist i've created for a client useful
https://gist.github.com/xandl/743fb6af60827eb95ad42b20b478b020
use woocomerce_get_sale_price filter.
add_filter('woocommerce_get_sale_price', 'my_custom_price', 99, 2);
add_filter('woocommerce_get_price', 'my_custom_price', 99, 2);
function my_custom_price( $price, $product )
{
//your logic for calculating the new price here
$price = $product->get_regular_price() * 0.8;
//Return the new price (this is the price that will be used everywhere in the store)
return $price;
}
In Woocommerce I am trying to hide the product on the archive page and single product page based on category however the condition does not appear to work and just hide all the price whether I set the category or not
add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_prices', 10, 2 );
function woocommerce_remove_prices( $price, $product ) {
if(is_product_category('sold')){
$price = '';
return $price;
}
}
To make your code working you should need to use has_term() conditional function for single product pages and you will need to always return the price at the end, outside the if statement:
add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_prices', 10, 2 );
function woocommerce_remove_prices( $price, $product ) {
if( is_product_category('sold') || has_term( 'sold', 'product_cat', $product->get_id() ) )
$price = '';
return $price;
}
It works! But this will not remove the selected product variation price and everywhere you still have the add to cart buttons.
Code goes in function.php file of your active child theme (or active theme).
Instead you could use the following that will remove all prices, quantity buttons and add-to-cart buttons on that specific product category:
// Specific product category archive pages
add_action( 'woocommerce_after_shop_loop_item_title', 'hide_loop_product_prices', 1 );
function hide_loop_product_prices(){
global $product;
if( is_product_category('sold') ):
// Hide prices
remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
// Hide add-to-cart button
remove_action('woocommerce_after_shop_loop_item','woocommerce_template_loop_add_to_cart', 30 );
endif;
}
// Single product pages
add_action( 'woocommerce_single_product_summary', 'hide_single_product_prices', 1 );
function hide_single_product_prices(){
global $product;
if( has_term( 'sold', 'product_cat', $product->get_id() ) ):
// Hide prices
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
// Hide add-to-cart button, quantity buttons (and attributes dorpdowns for variable products)
if( ! $product->is_type('variable') ){
remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart', 30 );
} else {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
}
endif;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works.