Woocommerce:仅在产品页面上显示价格后缀

时间:2019-07-26 08:07:54

标签: php css woocommerce

我在“我的自定义功能PHP插入程序”插件中添加了以下代码,以显示woocommerce商店中的自定义价格后缀。

add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );

function custom_price_suffix( $price, $product ){
    $price = $price . ' inkl. MwSt. und <a href="http://www.link.to/shippinginfo">zzgl. Versandkosten</a>'; 
    return apply_filters( 'woocommerce_get_price', $price );
}

它可以工作,但也可以在产品类别页面和商店页面上显示价格。我该如何避免呢?

我尝试了此CSS代码,但不起作用:

.woocommerce-price-suffix {
      display: none;
    }
    .single-product .product-price-wrap .woocommerce-price-suffix {
      display: block !important;
    }

以下解决方案可能有效,但我不想覆盖主题中的php文件:hide Woocommerce price suffix on category page

我也想更改“ inkl。MwSt。和zzgl。Versandkosten”的字体大小,但是我不知道如何在php中做到这一点。尝试过此CSS,但什么也没做:

.custom_price_suffix {
  font-size: small;
}

1 个答案:

答案 0 :(得分:0)

您可以使用内置的is_singular()函数来检查您是否在单个产品页面上

add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ) {
    if(is_singular('product')) {
        $price = $price . ' inkl. MwSt. und <a href="http://www.link.to/shippinginfo">zzgl. Versandkosten</a>'; 
        return apply_filters( 'woocommerce_get_price', $price );
    }
}

如果您想更改大小-您只需将文本包裹在一个范围内并向其中添加CSS-即将$ price变量更改为此:

$price = $price . ' <span class="make-me-small">inkl. MwSt. und <a href="http://www.link.to/shippinginfo">zzgl. Versandkosten</a></span>'; 

,然后将以下内容添加到CSS:

.make-me-small {
    font-size: 0.8rem;
}