Search code examples
wordpresswoocommerceorderswoocommerce-rest-api

Using multiple ids with WC_Order - Woocommerce


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();
} 

Solution

  • 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();
        } 
    }