繁体   English   中英

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

[英]Display the default discounted price and percentage on Woocommerce products

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

源代码: 显示 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;
}

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM