如何在单品和循环页面显示正价和促销价?

How to display the regular price and sale price in single product and loop pages?

我在我的子主题的 function.php 中使用了这段代码来显示正常价格和促销价,它在 WooCommerce v2.6.14 中运行良好。

但是此代码段在 WooCommerce 版本 3.2.3 上不再有效。

我该如何解决?

这是代码:

add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 );
function woocommerce_custom_sales_price( $price, $product ) {
$saved = wc_price( $product->regular_price - $product->sale_price );
return $price . sprintf( __('<p>Save %s</p>', 'woocommerce' ), $saved );
}

感谢帮助我的人!

过滤器 woocommerce_sale_price_html 不再存在,请改用 woocommerce_get_price_html。这将 运行 用于所有商品,无论商品是否在打折,因此您需要在代码中检查商品是否在打折。

add_filter( 'woocommerce_get_price_html', 'modify_woocommerce_get_price_html', 10, 2 );

function modify_woocommerce_get_price_html( $price, $product ) {
    if( $product->is_on_sale() && ! is_admin() )
        return $price . sprintf( __('<p>Save %s</p>', 'woocommerce' ), $product->regular_price - $product->sale_price );
    else
        return $price;
}