如何在 WooCommerce 中汇总价格? [英] How can I round up the price in WooCommerce?

查看:75
本文介绍了如何在 WooCommerce 中汇总价格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法将 WooCommerce 中的价格四舍五入 保持显示两位小数(因此价格将以第一位和第二位小数结束,00).

I'd like to know if there is a way to round up the prices in WooCommerce keeping 2 decimals displayed (so the price will end with the first and second decimal ,00).

例如:12.54 欧元将四舍五入为 13.00 欧元,145.67 欧元将四舍五入为 146.00 欧元.

For example: €12,54 will be rounded to €13,00 and €145,67 will be rounded up to €146,00.

我正在使用插件来打折产品价格,即 €39,00 - 20% = €35,10(应该变成 €36,00).

I'm using a plugin to discount the product price i.e. €39,00 - 20% = €35,10 (it should became €36,00).

我想按照之前的说明汇总所有价格.

I'd like to round up all the prices as explained before.

有没有办法做到这一点?

Is there a way to do so?

推荐答案

您无法真正汇总全局所有真实计算出的价格,例如税费、运费、折扣、总计和其他第三方插件计算的价格.四舍五入计算价格需要逐案完成......

You can't really round up globally all real calculated prices, like taxes, shipping, discounts, totals and others third party plugins calculated prices. Rounding calculated prices requires to be done case by case…

您只能在全局范围内对所有显示"格式的价格进行四舍五入,保留两位小数.为此,有两个步骤解释如下:

You can only round up globally all "displayed" formatted prices keeping the 2 decimals. For that there are 2 steps explained below:

1) 以下挂钩函数将四舍五入 WooCommerce 中的原始显示价格:

如有必要,它将通过舍入该值来返回下一个最高的整数值.

It will return the next highest integer value by rounding up that value if necessary.

add_filter( 'raw_woocommerce_price', 'round_up_raw_woocommerce_price' )
function round_up_raw_woocommerce_price( $price ){
    return ceil( $price );
}

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

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

请注意,所有 Woocommerce 价格格式功能都将使用我们第一个挂钩函数中的四舍五入价格......

Note that All Woocommerce price formatting functions will use our rounded prices from our first hooked function…

2) 在 WooCommerce 中格式化显示的价格.

WooCommerce 使用 函数wc_price() 以格式化 WooCommerce 设置中定义的价格.

WooCommerce uses the function wc_price() everywhere to format prices as defined in the WooCommerce settings.

因此,如果您需要格式化任何原始价格,您将使用该示例中的格式化函数:

So if you need to format any raw price you will use that formatting function like in this example:

// Get the WC_Product Object instance from the product ID
$product = wc_get_product ( $product_id );

// Get the product active price for display
$price_to_display = wc_get_price_to_display( $product );

// Format the product active price
$price_html = wc_price( $price_to_display );

// Output
echo $price_html

或者同样的事情,使用 WC_Product 内置方法 get_price_html() 更紧凑:

Or the same thing, more compact using WC_Product built in method get_price_html():

// Get the WC_Product Object instance from the product ID
$product = wc_get_product ( $product_id );

// Display the formatted product price
echo $product->get_price_html();

这篇关于如何在 WooCommerce 中汇总价格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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