WooCommerce:仅在单个产品页面上显示价格后缀 - 不适用于相关产品

Koo*_*Pal 4 php wordpress woocommerce hook-woocommerce

我试图仅在产品页面上的价格后面显示后缀。我不想在其他地方显示这个后缀。

我没有使用“税收”设置,因为我的价格已包含在内,并且我不想使用“税收”选项使设置变得复杂。

使用此答案中代码片段中的第一条路线/sf/answers/4005328631/

我的代码是:

## 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
Run Code Online (Sandbox Code Playgroud)

但是,此代码片段显示了相关产品中的后缀。

我应该做什么更改才能防止在相关产品中显示后缀

7uc*_*f3r 5

https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/

\n\n

相关产品由 \xe2\x80\x9cloop\xe2\x80\x9d 生成。有时您可能只想在单个产品页面上使用 PHP(并且不包括相关产品页面),反之亦然。

\n\n
function custom_price_suffix( $price, $product ) {\n    global $woocommerce_loop;\n\n    if( is_product() && !$woocommerce_loop[\'name\'] == \'related\' ) {\n        $price = $price . \' <span class="make-me-small"> Inclusive of all taxes</span>\';\n    }\n    //return $price;\n    return apply_filters( \'woocommerce_get_price\', $price );\n}\nadd_filter( \'woocommerce_get_price_html\', \'custom_price_suffix\', 100, 2 );\n
Run Code Online (Sandbox Code Playgroud)\n