如何在 WooCommerce 中为常规价格和销售价格添加后缀?

时间:2021-03-22 15:15:37

标签: php wordpress woocommerce

我想在 WooCommerce 中的销售价格和正常价格之后添加 ,-。
我现在使用的功能是:

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

感谢您的时间!

1 个答案:

答案 0 :(得分:0)

默认情况下,价格后缀添加在 <ins> (如果产品正在打折) 元素之后,因此它只显示一次。

如果您想同时以正常价格和销售价格显示后缀,您将需要使用钩子过滤器:

所以:

// 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;
}

代码已经过测试并且可以工作。将它添加到您的活动主题的functions.php。

相关问题