How to loop through order item quantity in woocommerce?

Member

by rollin , in category: PHP CMS , 6 months ago

How to loop through order item quantity in woocommerce?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elise_daugherty , 6 months ago

@rollin 

To loop through order item quantities in WooCommerce, you can use the following code snippet:

1
2
3
4
5
6
7
8
9
$order = new WC_Order( $order_id ); //replace $order_id with the ID of the order you want to loop through

foreach( $order->get_items() as $item_id => $item ){
    $product = $item->get_product();
    $quantity = $item->get_quantity();
    
    // Output the quantity
    echo 'Quantity: ' . $quantity;
}


This code snippet retrieves the order by its ID, then loops through each order item and gets the quantity of each item. You can then use this quantity for further processing as needed.