如何在单个产品和循环页面中显示常规价格和销售价格? [英] How to display the regular price and sale price in single product and loop pages?

查看:138
本文介绍了如何在单个产品和循环页面中显示常规价格和销售价格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在子主题的function.php中有此代码来显示常规价格和销售价格,并且在WooCommerce v2.6.14中运行良好.

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.

但是此代码段在WooCommerce版本3.2.3上不再起作用.

But this snippet doesn't work anymore on WooCommerce version 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.不论商品在售,它都会对所有商品运行,因此您需要使用代码检查商品是否在售.

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

这篇关于如何在单个产品和循环页面中显示常规价格和销售价格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆