显示 Woocommerce 产品的默认折扣价格和百分比

问题描述 投票:0回答:1

我正在尝试在 Woocommerce 上显示产品的折扣百分比。最初提供的解决方案(下面链接)有效,但是如果设置了默认产品变体,则不会显示折扣百分比。仅当选择更改为其他变体时,才会出现百分比折扣。我如何修改代码以立即显示百分比折扣 - 而无需选择其他变体?

源代码: 显示 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;
}
php wordpress woocommerce percentage product-price
1个回答
1
投票

当变量产品有默认选择的变体(促销)并正确显示折扣百分比时,链接的代码也可以工作...

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

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

// 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 文件中。

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

// 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 文件中。

© www.soinside.com 2019 - 2024. All rights reserved.