skip to Main Content

I am creating a WordPress Woocommerce webshop with 4000 products. The product list is updated daily by the manufacturer, which is then updated via FTP in this webshop.

Unfortunately, the manufacturer gives a value of "0" as the sale price for non-discounted products. How can I delete this value?

I tried with php code, but only the displayed price changed (the webshop shows the original price), but if I add it in the cart, it is taken into account with the price "0".

I want not only the original price to be displayed for the "0" sale price, I want to delete it.

By the way, the price filter doesn’t work that way on the website either, because it considers it a "0" price.

add_filter(‘woocommerce_get_price_html’, ‘custom_price_html’, 10, 2 );
function custom_price_html( $price, $product ) {

if ( $product->is_on_sale() && $product->get_sale_price() == '0' ) {
    $price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ) . $product->get_price_suffix();
}
return $price;

}

2

Answers


  1. Did you try to add an empty string to the sale price?

    function clear_zero_sale_price( $product ) {
        if ( $product->get_sale_price() == '0' ) {
            $product->set_sale_price( '' );
            $product->save();
        }
    }
    
    
    add_action( 'woocommerce_product_set_stock', 'clear_zero_sale_price' );
    
    Login or Signup to reply.
  2. Try this.

    add_filter('woocommerce_get_price_html', 'custom_display_price_if_sale_zero', 100, 2);
    
    function custom_display_price_if_sale_zero($price, $product) {
        if ($product->is_on_sale() && $product->get_sale_price() == 0) {
            $regular_price = wc_price($product->get_regular_price());
            $price = sprintf('<span class="woocommerce-Price-amount amount">%s</span>', $regular_price);
        }
        return $price;
    }
    

    Hook into woocommerce_get_price_html: This filter allows us to modify the HTML of the product price.

    Check if the Product is on Sale and the Sale Price is Zero: The is_on_sale() method checks if the product is on sale, and the get_sale_price() method retrieves the sale price. If the sale price is zero, the code will execute.

    Display Regular Price: If the sale price is zero, the function retrieves the regular price using get_regular_price(), formats it with wc_price(), and sets it to display as the product price.

    By adding this code to your theme’s functions.php file, WooCommerce will display the regular price instead of the sale price if the sale price is set to zero.
    Additional Customization

    If you want to add some custom styling or a message to indicate that the sale price is zero, you can modify the sprintf part to include additional HTML or text as needed

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search