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

问题描述

我正在尝试在Woocommerce上显示产品的折扣百分比.最初提供的解决方案(在下面链接)有效,但是,如果存在默认的产品差异集,则不会显示折扣百分比.只有当选择更改为另一个变化并不折扣率出现.我将如何修改代码以立即显示折扣百分比,而不必选择其他变体?

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

推荐答案

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

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).

然后使用简单的产品:

// 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产品的默认折扣价格和百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 20:51