I am creating a woocommerce shop. I update my products daily from a csv file (via manufacturer’s FTP). If a product is not on sale, the table contains a zero value. If a product is “out of stock”, the regular price is zero.
I would like the zero sale price to be deleted, and I would also like the zero regular price products not to appear on my page.
Thanks to @LoicTheAztec for helping me with a code that shows the regular price instead of the zero sale price.
I tried to use another code to remove the zero regular price products, but the two codes together do not work correctly.
I need more addition.
With the code, the zero sale price remains in the database, so the “sort by price” function does not work.
Example:
Product “A”: regular price: 1000, sale price: 0
Product “B”: regular price: 1000, sale price: 900
Product “C” (out of stock): regular price:0
I would like product “A” to appear with a price of 1,000, product “B” with a price of 900, and product “C” not to appear.
At the same time, “sort by price” should work and there is a price filter on the page, it correctly shows the price range 900-1000 (or 0-1000, it doesn’t matter).
This is related to my previous question/answer: How to clear product sale price if it Zero?
#1 Code, that hide zero regular price product:
// Adjust product active price
add_filter('woocommerce_product_get_price', 'adjust_product_active_price', 20, 2);
add_filter('woocommerce_product_variation_get_price', 'adjust_product_active_price', 20, 2);
function adjust_product_active_price( $price, $product ) {
if ( $price == 0 && $product->get_regular_price() > 0 ) {
return floatval($product->get_regular_price());
}
return $price;
}
// Empty product sale price with zero value
add_filter('woocommerce_product_get_sale_price', 'adjust_product_zero_sale_price', 20, 2 );
add_filter('woocommerce_product_variation_get_sale_price', 'adjust_product_zero_sale_price', 20, 2 );
function adjust_product_zero_sale_price( $price, $product ) {
if ( $price == 0 ) {
return '';
}
return $price;
}
// Utility function (Remove zero sale prices)
function remove_zero_prices( $prices ) {
foreach( $prices as $key => $price ) {
if ( $price == 0 ) {
unset($prices[$key]);
}
}
return $prices;
}
// Adjust displayed variable price
add_filter( 'woocommerce_variable_price_html', 'adjust_variable_price_html', 20, 2 );
function adjust_variable_price_html( $price_html, $product ) {
$prices = $product->get_variation_prices( true );
if ( min($prices['sale_price']) > 0 ) {
return $price_html;
}
$active_prices = remove_zero_prices($prices['price']);
$regular_prices = $prices['regular_price'];
$min_price = min(array_merge($active_prices, $regular_prices));
$max_price = max(array_merge($active_prices, $regular_prices));
$min_reg_price = current($regular_prices);
$max_reg_price = end($regular_prices);
if ( $product->is_on_sale() && $min_reg_price === $max_reg_price ) {
$price_html = wc_format_sale_price( wc_price( $max_reg_price ), wc_price( $min_price ) );
} elseif ( $min_price !== $max_price ) {
$price_html = wc_format_price_range( $min_price, $max_price );
} else {
$price_html = wc_price( $min_price );
}
return $price_html;
}
add_filter('woocommerce_product_is_on_sale', 'filter_variable_product_is_on_sale', 20, 2);
function filter_variable_product_is_on_sale( $is_on_sale, $product ) {
if( $product->is_type('variable') ) {
$is_on_sale = false;
foreach( $product->get_available_variations('') as $variation ) {
if( $variation->is_on_sale() ) {
return true;
}
}
}
return $is_on_sale;
}
#2 Code, that hide zero regular price product
add_action( 'woocommerce_product_query', 'themelocation_product_query' );
function themelocation_product_query( $q ){
$meta_query = $q->get( 'meta_query' );
$meta_query[] = array(
'key' => '_price',
'value' => 0,
'compare' => '='
);
$q->set( 'meta_query', $meta_query );
}
Balazs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.