将折扣百分比添加到 Woocommerce 中的可变产品价格范围 [英] Add the discounted percentage to variable product price range in Woocommerce

查看:68
本文介绍了将折扣百分比添加到 Woocommerce 中的可变产品价格范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Woocommerce,我已经使用以下代码从产品档案页面中连续删除了销售徽章和价格:

Using Woocommerce, I have successively removed sale badges and prices from product archives pages with the following code:

// Remove Sales Flash
add_filter('woocommerce_sale_flash', 'woo_custom_hide_sales_flash');
function woo_custom_hide_sales_flash()
{
    return false;
}

// Remove prices on archives pages
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );

所有产品都是可变产品,所有变体都具有相同的价格.此外,实际上所有价格都是销售价格.

All the products are variable products and all variations have the same prices. Also actually all prices are sale prices.

我想在每个可变价格范围后添加折扣百分比,在单个产品页面.我尝试使用以下代码:

I would like to add the discounted percentage after each variable price range, in single product pages. I have tried using the following code:

add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 );
function woocommerce_custom_sales_price( $price, $product ) {
    $percentage = round( ( ( $product->regular_price – $product->sale_price ) / 
    $product->regular_price ) * 100 );
    return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
}

但我什么都没得到

我做错了什么,怎么做?

What I am doing wrong and how can this be done?

对此的任何帮助将不胜感激.

Any help on this will be appreciated.

推荐答案

我一直在测试代码,当您针对可变产品的销售价格范围时,最好在 woocommerce_format_sale_price 中使用自定义挂钩函数code> 位于 wc_format_sale_price() 函数中的过滤器钩子.

I have been testing the code and as you are targeting the sale price range for variable products it's better to use a custom hooked function in woocommerce_format_sale_price filter hook that is located in wc_format_sale_price() function.

当所有变体具有相同价格时,这将允许在价格范围之后显示保存的折扣百分比.如果变体价格不同,则此百分比只会出现在变体价格上.

That will allow to display the saved discount percentage after the price range when all variations have the same prices. If variations prices are different, then this percentage will only appear on variations prices.

所以我重新访问了您的代码:

So I have revisited your code:

// Removing sale badge
add_filter('woocommerce_sale_flash', '__return_false');

// Removing archives prices
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );

// Add the saved discounted percentage to variable products
add_filter('woocommerce_format_sale_price', 'add_sale_price_percentage', 20, 3 );
function add_sale_price_percentage( $price, $regular_price, $sale_price ){
    // Strip html tags and currency (we keep only the float number)
    $regular_price = strip_tags( $regular_price );
    $regular_price = (float) preg_replace('/[^0-9.]+/', '', $regular_price);
    $sale_price = strip_tags( $sale_price );
    $sale_price = (float) preg_replace('/[^0-9.]+/', '', $sale_price);

    // Percentage text and calculation
    $percentage  = __('Save', 'woocommerce') . ' ';
    $percentage .= round( ( $regular_price - $sale_price ) / $regular_price * 100 );

    // return on sale price range with "Save " and the discounted percentage
    return $price . ' <span class="save-percent">' . $percentage . '%</span>';
}

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

经过测试并有效.

这篇关于将折扣百分比添加到 Woocommerce 中的可变产品价格范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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