试试这个
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;
}
add_action( woocommerce_before_calculate_totals , change_sale_price_to_regular_price );
function change_sale_price_to_regular_price( $cart ) {
if ( is_admin() && ! defined( DOING_AJAX ) ) {
return;
}
if ( did_action( woocommerce_before_calculate_totals ) >= 2 ) {
return;
}
foreach ( $cart->get_cart() as $cart_item ) {
$product = $cart_item[ data ];
if ( $product->is_on_sale() && $product->get_sale_price() == 0 ) {
$product->set_price( $product->get_regular_price() );
}
}
}
Also add this hook in functions file.
I will replace the sale price to regular price if sale price is zero
挂入 Woocommerce_get_price_html : 这个过滤器允许我们修改产品价格的 HTML 。
检查产品是否在销售中, 销售价格为零: 如果产品在销售中, 是_ on_ sale () 方法检查, 而获得_ sale_ price () 方法取回销售价格。 如果销售价格为零, 代码将执行 。
显示正则价格 : 如果销售价格为零, 函数会使用“ 正常价格 () ”, 以 wc_ 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
如果您想要添加一些自定义样式或消息以表示销售价为零, 您可以修改 spripf 部分, 以根据需要添加 HTML 或文字