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

随声附和 提交于 2020-01-16 18:27:53

问题


I had this code in function.php of my child theme to display the regular price and sale price and it was working fine in WooCommerce v2.6.14.

But this snippet doesn't work anymore on WooCommerce version 3.2.3.

How can I fix that?

Here is that code:

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

Thanks to whoever help me out!


回答1:


The filter woocommerce_sale_price_html doesn't exist anymore use woocommerce_get_price_html instead. This will run for everything regardless of of the item being on sale so you'll need to check if the product is on sale in your code.

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


来源:https://stackoverflow.com/questions/47255535/how-to-display-the-regular-price-and-sale-price-in-single-product-and-loop-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!