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

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

问题描述

我正在尝试在 Woocommerce 上显示产品的折扣百分比.最初提供的解决方案(链接如下)有效,但是如果存在默认产品变体集,则不会显示折扣百分比.Only when the selection is changed to another variation does the percentage discount appear.我将如何修改代码以立即显示百分比折扣 - 无需选择其他变体?

I am trying to display the percentage discount of a product on Woocommerce. The solution originally provided (linked below) works, however the discount percentage doesn't display if there is a default product variation set. Only when the selection is changed to another variation does the percentage discount appear. How would I modify the code to display the percent discount immidiately - without having to select another variation?

源代码:显示 Woocommerce 产品的折扣价和百分比 (选项 2)

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

推荐答案

链接代码有效 也当变量产品有默认选择的变体(特价)并正确显示折扣百分比时......

The linked code works also when there is a default selected variation (on sale) for the variable product and displays the discount percentage correctly…

现在对于可变产品一般显示的价格范围,您不能显示折扣百分比,因为所有变体都需要在销售,并且每个变体的折扣百分比可以不同......

Now for the variable product general displayed price range, you can't display a discounted percentage, as all variations should need to be on sale and each variation discounted percentage can be different…

对于选定的产品变体促销价格,您还可以使用以下方法获得节省百分比:

For selected product variations on sale price, you can also use the following to get the saving percentage:

// For product variations
add_filter( 'woocommerce_available_variation', 'custom_variation_price_saving_percentage', 10, 3 );
function custom_variation_price_saving_percentage( $data, $product, $variation ) {
    $active_price  = $data['display_price'];
    $regular_price = $data['display_regular_price'];

    if( $active_price !== $regular_price ) {
        $saving_percentage = round( 100 - ( $active_price / $regular_price * 100 ), 1 ) . '%';
        $data['price_html'] .= sprintf( __('<p class="saved-sale">Save: %s</p>', 'woocommerce' ), $saving_percentage );
    }
    return $data;
}

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

Code goes in functions.php file of your active child theme (or active theme).

那么对于简单的产品,您将使用:

Then for simple products you will use:

// For simple products
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('simple') ){
         // 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 ), $precision ) . '%';

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

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

Code goes in functions.php file of your active child theme (or active theme).

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

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