Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

php - 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.

Using the first route from code snippet on this answer https://stackoverflow.com/a/57218980/8044005

My code is:

## 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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 );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...