首页 > 解决方案 > 使用 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();
        }
    }
}

标签: phpwordpresswoocommercehook-woocommercewoocommerce-rest-api

解决方案


推荐阅读