[英]Add price suffix only on WooCommerce single product page without linked products
I am adding a price suffix on the WooCommerce single product page (and only there, not in the loop.).我在 WooCommerce 单个产品页面上添加了价格后缀(并且仅在此处,不在循环中。)。
I use the following:我使用以下内容:
add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ) {
if( is_product() ) {
$price = $price . ' <small>incl. tax</small>';
}
return apply_filters( 'woocommerce_get_price', $price );
}
However, this also adds the price suffix to the Up-Sells on the product page (I am not talking about the related products, but the Up-Sells).但是,这也会在产品页面的 Up-Sells 中添加价格后缀(我说的不是相关产品,而是 Up-Sells)。
How can I exclude the price suffix for the Up-Sells?如何排除追加销售的价格后缀?
I tried:我试过了:
if( is_product() && !$woocommerce_loop['name'] == 'up-sells' )
But the suffix is still displayed for the Up-Sells.但后缀仍显示为追加销售。
In your code $woocommerce_loop
is not defined在您的代码中
$woocommerce_loop
未定义
Instead of the compare, do the reverse and only apply it to an empty value而不是比较,相反,仅将其应用于空值
So you get:所以你得到:
function filter_woocommerce_get_price_html( $price, $product ) {
global $woocommerce_loop;
if ( is_product() && $woocommerce_loop['name'] == '' ) {
$price .= ' <small> incl. tax</small>';
}
//return $price;
return apply_filters( 'woocommerce_get_price', $price );
}
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );
OR use或使用
function filter_woocommerce_get_price_html( $price, $product ) {
global $woocommerce_loop;
if ( is_product() && $woocommerce_loop['name'] !== 'related' && $woocommerce_loop['name'] !== 'up-sells' ) {
$price .= ' <small> incl. tax</small>';
}
//return $price;
return apply_filters( 'woocommerce_get_price', $price );
}
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.