首页 > 解决方案 > 在 WooCommerce 中创建订单时过滤商品单价

问题描述

创建订单时,我正在使用 Zapier 将订单从 WooCommerce 导出到我的帐户软件。数据通过 WooCommerce REST API 发送。

一切正常,但我注意到订单元数据中的单价有时会显示超过 2 位小数的产品价格。当订购了不止一件相同的物品时,就会发生这种情况。当商品价格有两位小数并且以奇数的倍数排序时,似乎总是会发生这种情况。

我的会计软件最多只能接受 2 位小数。所以每次单价超过小数点后两位都会出现错误。这就是我发现这个问题的方式。

我已经检查了商品价格并且价格是正确的。我还通过启用和禁用“小计级别的舍入税,而不是每行舍入”进行了测试。似乎没有什么可以解决问题。

我想在 WooCommerce 中生成订单时过滤单价,以便将它们转换为小数点后两位。任何有关如何解决此问题的想法或建议将不胜感激。

下面是一个示例,其中商品显示的小数点后位数超过 2 位:

isCreditInvoice:false
customer:d58ca99b-07f3-45fa-80ae-164ebbc881f1
invoiceDate:2021-03-07T09:18:06+02:00
quantity:1,1,1,1,1,1,2,6,6,5,7,7,4,10,10,1,1,1,1,1,1,1,1
yourReference:Order Number 6631
deliveryDate:{{111079934__date_completed}}
ourReference:Order Number 6631
unitPrice:22.5,22.5,26.25,25,34.95,34.95,23.76,6.95,6.95,6.95,9.950000000000001,9.950000000000001,9.95,1.6,1.6,14.95,23.4,8.4,30,21.36,25,12.5,26.5

更新:这是 WooCommerce 插件核心问题,已在 Github 上向 WooCommerce 报告

https://github.com/woocommerce/woocommerce/issues/27911

使用以下补丁暂时修复此问题:

导航到 woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller.php 的第 172 行

代替:

$data['price'] = $item->get_quantity() ? $item->get_total() / $item->get_quantity() : 0;

和:

$data['price'] = $item->get_quantity() ? wc_format_decimal( $item->get_total() / $item->get_quantity() ): 0;

测试和工作。

现在 unitPrice 将四舍五入到小数点后两位。

标签: phpwordpresswoocommercefilterorders

解决方案


推荐阅读