1

im trying to find a way to use multiple ids with WC_Order. it works fine when using a single id however multiple don't work.

$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();
} 
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Omar T
  • 123
  • 11

1 Answers1

1

you need to loop in each 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();
    } 
} 
Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
  • Great thank you :) i had a feeling i missed something – Omar T Jun 07 '18 at 10:08
  • what's not working with variables? if you need variation id, you can do something like: `$variation_id = $item_product->get_variation_id();` – Reigel Gallarde Jun 14 '18 at 09:56
  • Its works for single products however when i try with a variable product it doesn't match the right price with the correct product variation id. – Omar T Jun 14 '18 at 10:29
  • `$product_variations = $product->get_available_variations(); $variation_product_id = $product_variations [0]['variation_id']; $variation_product = new WC_Product_Variation( $variation_product_id ); $t_dy = $variation_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);` – Omar T Jun 14 '18 at 10:31
  • would be better if you create another post question for that.. the comment is too limited to answer – Reigel Gallarde Jun 14 '18 at 10:54
  • Okay thanks will do - https://stackoverflow.com/questions/50856207/getting-the-correct-variation-price-with-wc-order-woocommerce – Omar T Jun 14 '18 at 11:13