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屋!