我试图在产品页面上仅在价格后显示一个后缀。我不想在其他任何地方显示此后缀。
我不使用“税收”设置,因为我的价格都包含在内,并且我不想使用“税收”选项使设置复杂化。
在此答案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
但是,此代码段显示了相关产品中的后缀。
要防止显示相关产品中的后缀,我应该怎么做
答案 0 :(得分:3)
相关产品由“循环”生成。有时您可能只想在单个产品页面上使用PHP(并且不包括相关页面),反之亦然。
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 );