简体   繁体   English

仅在 WooCommerce 单品页面添加价格后缀,无链接产品

[英]Add price suffix only on WooCommerce single product page without linked products

I am adding a price suffix on the WooCommerce single product page (and only there, not in the loop.).我在 WooCommerce 单个产品页面上添加了价格后缀(并且仅在此处,不在循环中。)。

I use the following:我使用以下内容:

add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ) {
    if( is_product() ) {   
        $price = $price . ' <small>incl. tax</small>';
    }

    return apply_filters( 'woocommerce_get_price', $price );
}

However, this also adds the price suffix to the Up-Sells on the product page (I am not talking about the related products, but the Up-Sells).但是,这也会在产品页面的 Up-Sells 中添加价格后缀(我说的不是相关产品,而是 Up-Sells)。

How can I exclude the price suffix for the Up-Sells?如何排除追加销售的价格后缀?

I tried:我试过了:

if( is_product() && !$woocommerce_loop['name'] == 'up-sells' )

But the suffix is still displayed for the Up-Sells.但后缀仍显示为追加销售。

In your code $woocommerce_loop is not defined在您的代码中$woocommerce_loop未定义

Instead of the compare, do the reverse and only apply it to an empty value而不是比较,相反,仅将其应用于空值

So you get:所以你得到:

function filter_woocommerce_get_price_html( $price, $product ) {
    global $woocommerce_loop;

    if ( is_product() && $woocommerce_loop['name'] == '' ) {
        $price .= ' <small> incl. tax</small>';
    }
    
    //return $price;
    return apply_filters( 'woocommerce_get_price', $price );
}
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );

OR use或使用

function filter_woocommerce_get_price_html( $price, $product ) {
    global $woocommerce_loop;

    if ( is_product() && $woocommerce_loop['name'] !== 'related' && $woocommerce_loop['name'] !== 'up-sells' ) {
        $price .= ' <small> incl. tax</small>';
    }
    
    //return $price;
    return apply_filters( 'woocommerce_get_price', $price );
}
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce:仅在单个产品页面上显示价格后缀 - 不适用于相关产品 - WooCommerce: Show price suffix only on single product Page - Not for related products Woocommerce:仅在产品页面上显示价格后缀 - Woocommerce: Show Price Suffix only on Product Page 仅在所有 WooCommerce 单品上显示价格后缀 - Show a price suffix only on all WooCommerce single products Woocommerce - 仅当有加售产品时才将 html 添加到单个产品页面 - Woocommerce - Add html to the single product page only if there are upsell products 在 Woocommerce 单个产品的标题前添加链接的特定产品属性 - Add linked specific product attribute before title on Woocommerce single products 在 WooCommerce 单个产品页面上仅显示“有货”相关产品 - Show only "in stock" related products on WooCommerce single product page 在WooCommerce单品页的相关商品标题添加URL - Add relative URL to related products title on WooCommerce single product page 在 WooCommerce 的单个产品页面上为低库存产品添加 CSS 类 - Add CSS class for low stock products on single product page in WooCommerce 为 Woocommerce 中几乎所有产品的显示价格添加后缀 - Add a suffix to the displayed price of mostly all products in Woocommerce WooCommerce ONLY的产品页面如何在价格后添加图标 - How to add an icon in the product page of WooCommerce ONLY after the price
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM