如何在 WooCommerce 中汇总价格?

编程入门 行业动态 更新时间:2024-10-23 20:27:31
本文介绍了如何在 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 中汇总价格?

本文发布于:2023-11-22 19:33:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618735.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:价格   如何在   WooCommerce

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!