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
Did you try to add an empty string to the sale price?
Try this.
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