Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我怎样才能在商业中把价格汇总起来?_Php_Wordpress_Woocommerce_Product_Price - Fatal编程技术网

Php 我怎样才能在商业中把价格汇总起来?

Php 我怎样才能在商业中把价格汇总起来?,php,wordpress,woocommerce,product,price,Php,Wordpress,Woocommerce,Product,Price,我想知道是否有办法将WooCommerce中的价格四舍五入,保持显示2位小数(因此价格将以第一位和第二位小数00结尾) 例如:12,54欧元将四舍五入为13,00欧元,145,67欧元将四舍五入为146,00欧元 我正在使用一个插件来打折产品价格,即39,00欧元-20%=35,10欧元(应该变成36,00欧元) 我想把前面解释过的所有价格汇总起来 有没有办法做到这一点 您无法真正汇总全球所有真实的计算价格,如税收、运费、折扣、总计和其他第三方插件计算的价格。四舍五入计算价格需要逐案进行 您只能

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

例如:12,54欧元将四舍五入为13,00欧元,145,67欧元将四舍五入为146,00欧元

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

我想把前面解释过的所有价格汇总起来


有没有办法做到这一点

您无法真正汇总全球所有真实的计算价格,如税收、运费、折扣、总计和其他第三方插件计算的价格。四舍五入计算价格需要逐案进行

您只能对所有“显示”格式的价格进行全局取整保留2位小数。为此,有以下两个步骤:

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

如果需要,它将通过对该值进行四舍五入来返回下一个最高的整数值

add_filter( 'raw_woocommerce_price', 'round_up_raw_woocommerce_price' )
function round_up_raw_woocommerce_price( $price ){
    return ceil( $price );
}
代码进入活动子主题(或活动主题)的functions.php文件

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

2)格式化WooCommerce中显示的价格。

WooCommerce使用everywhere来设置WooCommerce设置中定义的价格格式

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

// 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\u Product
内置方法
get\u price\u html()
更紧凑:


你好@LoicTheAztec,非常感谢你的回答。我尝试添加代码的第一部分:add_filter('raw_-woocommerce_price','round_-up_-raw_-woocommerce_price')函数round_-up_-raw_-woocommerce_price($price){return ceil($price);}但我遇到了问题,网站崩溃了。我注意到在functions.php文件的末尾没有?>。然而,我认为问题是由我放置代码的位置引起的。文件末尾有两个“if”函数,我认为代码应该放在这两个部分之前。
// 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();