Skip to main content
wpcrux.com

Back to all posts

How to Loop Through Order Item Quantity In Woocommerce?

Published on
3 min read
How to Loop Through Order Item Quantity In Woocommerce? image

Best WooCommerce Shop Plugins to Buy in October 2025

1 The Ultimate WordPress & WooCommerce Handbook: From Setup to Success in Online Selling

The Ultimate WordPress & WooCommerce Handbook: From Setup to Success in Online Selling

BUY & SAVE
$12.90
The Ultimate WordPress & WooCommerce Handbook: From Setup to Success in Online Selling
2 WordPress WooCommerce: Tienda online con WooCommerce (Spanish Edition)

WordPress WooCommerce: Tienda online con WooCommerce (Spanish Edition)

BUY & SAVE
$35.90 $37.90
Save 5%
WordPress WooCommerce: Tienda online con WooCommerce (Spanish Edition)
3 Building E-Commerce Solutions with WooCommerce - Second Edition

Building E-Commerce Solutions with WooCommerce - Second Edition

BUY & SAVE
$38.99
Building E-Commerce Solutions with WooCommerce - Second Edition
4 WordPress WooCommerce: Webshop met WooCommerce (Dutch Edition)

WordPress WooCommerce: Webshop met WooCommerce (Dutch Edition)

BUY & SAVE
$24.99
WordPress WooCommerce: Webshop met WooCommerce (Dutch Edition)
5 Formation Woocommerce: Créer et exploiter une boutique en ligne (French Edition)

Formation Woocommerce: Créer et exploiter une boutique en ligne (French Edition)

BUY & SAVE
$9.99
Formation Woocommerce: Créer et exploiter une boutique en ligne (French Edition)
6 Setting Up and Running an Online Store

Setting Up and Running an Online Store

BUY & SAVE
$2.99
Setting Up and Running an Online Store
+
ONE MORE?

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

foreach( $order->get_items() as $item_id => $item ){ $product = $item->get_product(); $quantity = $item->get_quantity(); // Do something with the quantity }

This code will iterate through each item in the order and retrieve the quantity of each item. You can then perform any necessary actions based on the quantity of each item.

How to add discounts or promotions to order item quantities within a loop in WooCommerce?

To add discounts or promotions to order item quantities within a loop in WooCommerce, you can use the following code snippet:

// Get the current cart contents $cart = WC()->cart->get_cart();

// Loop through each item in the cart foreach ($cart as $cart_item_key => $cart_item) {

// Get the product ID of the item
$product\_id = $cart\_item\['product\_id'\];

// Get the quantity of the item
$quantity = $cart\_item\['quantity'\];

// Apply a discount or promotion based on the quantity of the item
if ($quantity >= 5) {
    $discount = 0.1; // 10% discount
    $new\_price = $cart\_item\['data'\]->get\_price() \* (1 - $discount);

    // Update the item price in the cart
    WC()->cart->cart\_contents\[$cart\_item\_key\]\['data'\]->set\_price($new\_price);
}

}

This code snippet will loop through each item in the cart, and if the quantity of the item is 5 or more, it will apply a 10% discount to the item price. You can adjust the discount percentage and quantity threshold to fit your specific promotion.

What is the impact of efficient looping on displaying order item quantities in WooCommerce?

Efficient looping in displaying order item quantities in WooCommerce can have multiple impacts on the performance and user experience of the website:

  1. Faster loading times: By efficiently looping through order items to display quantities, the website can load and render the content more quickly, reducing the wait time for users.
  2. Improved user experience: A faster loading website creates a better user experience, as customers can quickly view and interact with the content without delays.
  3. Reduced server load: Efficient looping reduces the strain on the server, allowing it to handle more requests and process data more efficiently.
  4. Better scalability: With efficient looping, the website can handle a larger volume of orders and items without experiencing performance issues, making it more scalable for growth.
  5. Overall performance improvement: Efficient looping can contribute to overall performance improvements on the website, making it more responsive and reliable for users.

What is the ideal approach for looping through order item quantities in a custom plugin or theme in WooCommerce?

The ideal approach for looping through order item quantities in a custom plugin or theme in WooCommerce is to first get the order items for the current order using the wc_get_order function. Then, you can loop through each item using a foreach loop and access the quantity of each item using the get_quantity() method. Here's an example code snippet that demonstrates this approach:

$order = wc_get_order( $order_id );

if ( $order ) { foreach ( $order->get_items() as $item_id => $item ) { $product_name = $item->get_name(); $product_quantity = $item->get_quantity();

    // Do something with the product name and quantity
    echo "Product: {$product\_name}, Quantity: {$product\_quantity}<br>";
}

}

In this code snippet, we first get the order object using wc_get_order and then loop through each order item using a foreach loop. Inside the loop, we get the product name and quantity using the get_name() and get_quantity() methods, respectively. You can then perform any necessary operations with the product name and quantity inside the loop.