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

查看:160
本文介绍了显示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不能直接访问Product对象属性,因此您的代码有些过时.相反,您应该使用可用的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文件中.

经过测试,可以正常工作.

Tested and works.

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

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