我将以下代码添加到“我的自定义函数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文件:在类别页面上隐藏Woocommerce价格后缀
我也想更改“ inkl。MwSt。和zzgl。Versandkosten”的字体大小,但是我不知道如何在php中做到这一点。尝试过此CSS,但不执行任何操作:
.custom_price_suffix {
font-size: small;
}
您可以使用内置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;
}
根据您有关基于网站基本语言调整链接文本的评论,有两种方法可以实现此目的:
第一种方法(可能是最好的/公认的方法)是使用内置的字符串翻译管理功能。您可以通过使用该__()
函数并将其包装在文本周围来做到这一点,如下所示:
$price = $price . __(' inkl. MwSt. und <a href="http://www.link.to/shippinginfo">zzgl. Versandkosten</a>', 'my-text-domain');
完成此操作后,您将看到您的文本现在将显示在仪表板上WPML菜单项下的“字符串翻译”选项卡下-从此处您可以根据不同的网站语言手动为其分配新的字符串。
第二种方法是使用switch
(或if
语句)语句在函数内部手动添加调整:
add_filter( 'woocommerce_get_price_html', 'custom_price_suffix', 100, 2 );
function custom_price_suffix( $price, $product ) {
if(is_singular('product')) {
$language_code = apply_filters( 'wpml_current_language', NULL );
switch ($language_code) {
case 'de':
$suffix = ' inkl. MwSt. und <a href="http://www.link.to/shippinginfo">zzgl. Versandkosten</a>';
break;
case 'it':
$suffix = ' IVA e spese di <a href="http://www.link.to/shippinginfo">spedizione incluse</a>';
break;
default:
$suffix = ' incl. VAT and <a href="http://www.link.to/shippinginfo">shipping costs</a>';
break;
}
$price = $price . $suffix;
}
return apply_filters( 'woocommerce_get_price', $price );
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句