Magento 2

How to get Total Order Item Quantity in Magento 2

Ecommerce is made up of several rich elements, and Magento has successfully covered all of it, and maybe that’s why it became pick for store owners. But usually, every Magento projects end at customization by giving a personalized touch to store.

Working for Magento custom development, we went through several different customization requirements and sometimes we are shocked by listening to some of the needs but Yaa, every business has its own story, own needs, and individual requirements and that’s why it’s unique.

Recently, one of the clients told us that his most of the time spent in identifying that how many quantities reside within an order. And he wanted to save his time so he can invest his valuable time in other business stuff. Maybe several times you need to know total order item quantity, so this code might help you to save your time. So, we have identified two possible way to quickly done his work using the following two methods.

Method 1: Standard Magento Method

protected $orderFactory;

public function __construct(\Magento\Sales\Model\OrderFactory $orderFactory)
{
    $this->orderFactory = $orderFactory;
}

public function execute()
{
     $orderId = 5;  //PASS YOUR ORDER ID HERE
     $order = $this->orderFactory->create()->load($orderId);
     $orderItems = $order->getAllItems();
     $total_qty = 0;
     foreach ($orderItems as $item)
     {
          $total_qty = $total_qty + $item->getQtyOrdered();
     }
     return $total_qty;
}

Method 2 : Using Object Manager (Not Recommended Method)

$order_id = 5;  //PASS YOUR ORDER ID HERE
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->load($order_id);
$orderItems = $order->getAllItems();
$total_qty = 0;
foreach ($orderItems as $item)
{
   $total_qty = $total_qty + $item->getQtyOrdered();
}
 
echo $total_qty;

Note:  According to Magento Standard, Using Object Manager is not Recommended, We shared the way just for the knowledge purpose.

You can use any of two code as per the need of getting order quantity.
Lastly, hit that falling stars if the code worked for you and don’t forget to comment down below if you are looking for any help regarding this code.
Happy Coding.

Click to rate this post!
[Total: 16 Average: 3.6]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

18 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

7 days ago