I am looking for an optimized way to get the total number of shipped/delivered items from the entire purchase history of a customer.

This is the code i am using right now.

/* $this->order = Magento\Sales\Model\Order */
/* Get Total numbers of delivered items */
$customerEmail = 'abc@test.com';
    $ordercollection  = $this->order->getCollection()
        ->addAttributeToFilter('customer_email', $customerEmail);
    $totaldelivereditems = 0;
    foreach ($ordercollection as $orderobj){
        foreach ($orderobj->getAllVisibleItems() as $item) {
            $totaldelivereditems += $item->getQtyShipped();

But it doesnt seems to me an optimized code in case if customer has more than 100 orders or more in his purchase history. This loop might take some time. If anyone knows better way to achieve the same data (total delivered items), I would really appreciate his help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.