php - 使用 woocommerce 更新变体订单价格
问题描述
如果产品销售价格发生变化,尝试使用 WC_Order 找到更新 woocommerce 订单产品价格的方法。它在单个产品上运行良好,但在 woocommerce 中使用可变产品时,它只会获得匹配产品的第一个变体。似乎无法理解为什么?
// Cron job
add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );
function every_three_minutes_event_func( $checkout = null)
{
global $woocommerce;
global $product;
$query = new WC_Order_Query( array(
'status' => 'on-hold',
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
$order_ids = $query->get_orders();
foreach( $order_ids as $order_id ) {
// Getting all products
$order = new WC_Order($order_id);
// Going through all orders
foreach ($order->get_items() as $item_id => $item_obj) {
$_product = wc_get_product($item_obj['product_id']);
$t_dy = $_product->get_price();
$item_qty = $item_obj['qty'];
$it_total = $item_qty * $t_dy;
$td = wc_update_order_item_meta($item_id, '_line_total', $it_total);
$order->calculate_totals();
$order->save();
}
}
}
解决方案
推荐阅读
- haskell - 由两个给定函数组成的函数的类型和定义
- php - Rest api:如何使用 Put 方法创建 api?
- javascript - 在 fullcalendar V5 中删除或隐藏特定日期
- javascript - StandardJS VSCode 扩展不再突出显示错误 - 突然停止工作
- flutter - 编译时不推荐使用的 API
- django - django 无法计算嵌套 SUM 的总和
- html - 表格切换在 Safari 中不起作用...在 Chrome 中可以正常工作
- reactjs - AG-GRID forEachNode 仅对有限数量的缓冲行进行操作
- javascript - 如何在javascript中使用“indexOf”
- python - python数据框新列基于另一列,其值为数据框的条件最小值