【发布时间】:2021-01-07 20:52:50
【问题描述】:
我正在寻找一种在 WooCommerce 中获取订单商品单一价格的方法。我在这里关注了这篇文章并使用了get_price() 方法,但这种方法似乎不再可用:
Woocommerce - Getting the order item price and quantity.
foreach ( $order_items as $order_item ) {
error_log( $order_item->get_price() );
error_log( print_r( $order_item, true ) );
}
未捕获的错误:调用未定义的方法 WC_Order_Item_Product::get_price()
问题是我不能只拿到产品并在那里读取正常价格,因为我需要在下订单期间设置价格,并且产品价格可以在以后多次更改。
我还打印了整个订单项目以找到那里的单个价格字段,但什么也没找到:
[data:protected] => Array
(
[order_id] => 24
[name] => Dings Teil
[product_id] => 23
[variation_id] => 0
[quantity] => 2
[tax_class] =>
[subtotal] => 42.4
[subtotal_tax] => 6.78
[total] => 42.4
[total_tax] => 6.78
[taxes] => Array
(
[total] => Array
(
[6] => 6.784
)
[subtotal] => Array
(
[6] => 6.784
)
)
)
总而言之,我需要以某种方式从我的订单商品中获得单一价格。 WooCommerce 似乎有办法在订单项视图中获取它,但我找不到他们处理此问题的方式:
因为我正在编写一个插件,所以对 WooCommerce 的任何更改都不是一个好主意。
更新:
是的,我也有过将小计除以数量的想法,但如果我的舍入不像 WooCommerce 舍入那样 100%,这可能会导致一些舍入问题。
【问题讨论】:
标签: php wordpress woocommerce