[英]How to display woocommerce sale price or regular price if there is no sale price
[英]How to add suffix to both regular price and on sale price in WooCommerce?
我想补充一点,在 WooCommerce 的销售价格和正常价格之后。
我现在使用的 function 是:
// Add ,- after price
add_filter( 'woocommerce_get_price_html', 'njengah_text_after_price' );
function njengah_text_after_price($price){
$text_to_add_after_price = ',-'; //change text in bracket to your preferred text
return $price . $text_to_add_after_price;
}
这是有效的,但它只在 WooCommerce 的销售价格之后添加。 它也应该在标准价格之后添加。
这是 HTML 现在是如何添加的:
<span class="price">
<del>
<span class="woocommerce-Price-amount amount">
<bdi>
<span class="woocommerce-Price-currencySymbol">€</span>
4.326
</bdi>
</span>
</del>
<ins>
<span class="woocommerce-Price-amount amount">
<bdi>
<span class="woocommerce-Price-currencySymbol">€</span>
2.294
</bdi>
</span>
</ins>
,-
</span>
谢谢你的时间!
默认情况下,价格后缀添加在<ins>
元素之后(以防产品打折) ,因此它只显示一次。
如果您想以正常价格和销售价格显示后缀,您将需要使用钩子过滤器:
woocommerce_get_price_suffix
设置价格后缀并更新woocommerce_price_display_suffix
选项woocommerce_format_sale_price
更改销售价格的格式所以:
// adds the suffix to the price
add_filter( 'woocommerce_get_price_suffix', 'add_price_suffix' );
function add_price_suffix(){
$suffix = ',-';
update_option( 'woocommerce_price_display_suffix', $suffix );
return $suffix;
}
// adds the suffix to the regular price (the <del> element) if the product is on sale
add_filter( 'woocommerce_format_sale_price', 'change_format_sale_price', 99, 3 );
function change_format_sale_price( $price_html, $regular_price, $sale_price ) {
$suffix = get_option( 'woocommerce_price_display_suffix' );
$price_html = '<del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . $suffix . '</del> <ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins>';
return $price_html;
}
该代码已经过测试并且可以工作。 将其添加到您的活动主题的功能中。php。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.