在 WooCommerce 中创建订单时过滤项目单价

时间:2021-03-12 22:10:31

标签: php wordpress woocommerce filter orders

创建订单时,我正在使用 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 将四舍五入到两位小数。

0 个答案:

没有答案
相关问题