How to clear zero sale price from database?

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 );
}

New contributor

Balazs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật