使用woocommerce

时间:2018-06-08 15:28:58

标签: php wordpress woocommerce hook-woocommerce woocommerce-rest-api

如果产品销售价格发生变化,尝试使用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();
        }
    }
}

0 个答案:

没有答案