在WC_Order中使用多个ID - Woocommerce

时间:2018-06-07 09:46:31

标签: wordpress woocommerce orders woocommerce-rest-api

我试图找到一种方法来使用WC_Order使用多个ID。使用单个ID时它工作正常,但多个不起作用。

$order_id = array("156","155","156"); // The order_id

// get an instance of the WC_Order object
$order = new WC_Order( $order_id )

// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item_product ){
    //Get the product ID
    $item_product->get_product_id();
    //Get the WC_Product object
    $item_product->get_product();
} 

1 个答案:

答案 0 :(得分:1)

你需要循环每个id。

$order_ids = array("156","155","156"); // The order_id


foreach( $order_ids as $order_id ){
    // get an instance of the WC_Order object
    $order = new WC_Order( $order_id );

    // The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
    foreach( $order->get_items() as $item_id => $item_product ){
        //Get the product ID
        $item_product->get_product_id();
        //Get the WC_Product object
        $item_product->get_product();
    } 
}