WooCommerce:仅在单个产品页面上显示价格后缀 - 不适用于相关产品 [英] WooCommerce: Show price suffix only on single product Page - Not for related products
问题描述
我试图在产品页面上仅在价格之后显示后缀.我不想在其他任何地方显示这个后缀.
I am trying to display a suffix after the Price ONLY on the Product Page. I do not want to display this suffix anywhere else.
我没有使用税收设置,因为我的价格是全包的,我不想使用税收选项使设置复杂化.
I am NOT using the Tax settings as my prices are all inclusive and I do not want to complicate the settings using the Tax options.
使用此答案的代码片段中的第一条路线https://stackoverflow.com/a/57218980/8044005
Using the first route from code snippet on this answer https://stackoverflow.com/a/57218980/8044005
我的代码是:
## Add suffix to price on Product Page
add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ) {
if(is_singular('product')) {
$price = $price . ' <span class="make-me-small"> Inclusive of all taxes</span>';
}
return apply_filters( 'woocommerce_get_price', $price );
}
##-End of above code - Start new code below
但是,此代码段显示了相关产品中的后缀.
However, this code snippet shows the suffix in the Related Products.
我应该做哪些更改以防止在相关产品中显示后缀
What changes should I do to prevent displaying the suffix in Related Products
推荐答案
https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/
相关产品由循环"生成.有时您可能只想在单个产品页面上使用 PHP(不包括相关页面),反之亦然.
Related products are generated by a "loop". Sometimes you might want to use your PHP on the single product page only (and excluding the related ones) or viceversa.
function custom_price_suffix( $price, $product ) {
global $woocommerce_loop;
if( is_product() && !$woocommerce_loop['name'] == 'related' ) {
$price = $price . ' <span class="make-me-small"> Inclusive of all taxes</span>';
}
//return $price;
return apply_filters( 'woocommerce_get_price', $price );
}
add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
这篇关于WooCommerce:仅在单个产品页面上显示价格后缀 - 不适用于相关产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!