How to loop through order item quantity in woocommerce?

Member

by rollin , in category: PHP CMS , a year ago

How to loop through order item quantity in woocommerce?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by elise_daugherty , a year 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.

Related Threads:

How to get one order for each item in woocommerce?
How to get quantity of a woocommerce subscription?
How to remove quantity from cart page in woocommerce?
How to add quantity field on shop page for woocommerce?
How to save previous product stock quantity in woocommerce?
How to get the order id from the order key in woocommerce?