显示 Woocommerce 产品的折扣价和百分比 [英] Display the discounted price and percentage on Woocommerce products

查看:46
本文介绍了显示 Woocommerce 产品的折扣价和百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下图中,它显示了折扣价格和百分比

On the image below, it shows the discounted price and percentage

我没有发现具有此功能的自定义代码搜索.

I found no custom code searching that has this feature.

我使用下面的代码来显示折扣价,但价格没有格式化(缺少货币符号和小数):

I am using the code below to display the discounted price, but the price is not formatted (the currency symbol and the decimals are missing):

add_filter( 'woocommerce_get_price_html', 'modify_woocommerce_get_price_html', 10, 2 );

function modify_woocommerce_get_price_html( $price, $product ) {
    if( $product->is_on_sale() && ! is_admin() )
        return $price . sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $product->regular_price - $product->sale_price );
    else
        return $price;
}

如何显示正确格式的折扣价?如何同时显示折扣百分比?

How can I display the correct formatted discounted price? How can I display also the discounted percentage?

感谢任何帮助.

推荐答案

自从 woocommerce 版本 3 以来,您的代码有点过时了,因为无法直接访问产品对象属性.相反,您应该使用可用的WC_Product 方法.

Your code is a bit outdated since woocommerce version 3 as Product object properties ca't be accessed directly. Instead you should use available WC_Product methods.

要格式化价格,您将使用 wc_price() 专用格式化函数.

To format the prices you will use wc_price() dedicated formatting function.

现在你可以拥有(3种可能性):

1) 节省的价格:

add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
    // Only on sale products on frontend and excluding min/max price on variable products
    if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
        // Get product prices
        $regular_price = (float) $product->get_regular_price(); // Regular price
        $sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)

        // "Saving price" calculation and formatting
        $saving_price = wc_price( $regular_price - $sale_price );

        // Append to the formated html price
        $price .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_price );
    }
    return $price;
}

2) 节省百分比:

add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
    // Only on sale products on frontend and excluding min/max price on variable products
    if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
        // Get product prices
        $regular_price = (float) $product->get_regular_price(); // Regular price
        $sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)

        // "Saving Percentage" calculation and formatting
        $precision = 1; // Max number of decimals
        $saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';

        // Append to the formated html price
        $price .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_percentage );
    }
    return $price;
}

3 两者都(折扣价和百分比):

add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
    // Only on sale products on frontend and excluding min/max price on variable products
    if( $product->is_on_sale() && ! is_admin() && ! $product->is_type('variable')){
        // Get product prices
        $regular_price = (float) $product->get_regular_price(); // Regular price
        $sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)

        // "Saving price" calculation and formatting
        $saving_price = wc_price( $regular_price - $sale_price );

        // "Saving Percentage" calculation and formatting
        $precision = 1; // Max number of decimals
        $saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';

        // Append to the formated html price
        $price .= sprintf( __('<p class="saved-sale">Save: %s <em>(%s)</em></p>', 'woocommerce' ), $saving_price, $saving_percentage );
    }
    return $price;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.

经过测试并有效.

这篇关于显示 Woocommerce 产品的折扣价和百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆