Magento 2 $quote->getAllItems(); does not working properly

Magento 2 $quote->getAllItems(); does not working properly

July 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 $quote->getAllItems(); does not working properly

Why Magento 2 $quote->getAllItems() May Fail in Certain Scenarios

As an experienced developer working with Magento 2, I’ve encountered this issue multiple times during after-sale processes or custom cron jobs. The getAllItems() method is a core functionality of the quote model that should return all items associated with a specific quote ID.

However, you’re right to notice that this function might fail when the quote is not in the default website context. This occurs because Magento uses the website id to scope the items and other data related to the quote. Let me explain why this happens and provide some solutions based on best practices.

Understanding the Scope of Quote Items

In Magento 2, quotes are associated with a specific website (although this is not always obvious). When you create a new quote or when one is generated during checkout, it’s assigned to a website. This assignment ensures data integrity by restricting access to products and other resources based on the current website scope.

When you try to load a quote using $quote = $objectManager->create('\Magento\Quote\Model\Quote')->load($order->getQuoteId());, Magento first checks if the loaded quote belongs to the currently running website. If not, it returns an empty collection of items.

Solving the Issue: Handling Multi-Site Environments

To resolve this issue and ensure you can access all items regardless of the website context, you have a few options:

  1. Use the Correct Website Context Before Loading the Quote You can manually set the current website scope before loading the quote using Magento’s website management functions:

    $websiteId = 2; // replace with the desired website id
    Mage::app()->setCurrentStore($websiteId);
    $quote = Mage::getModel('sales/order')->load($orderId)->getQuote();
    
  2. Use the Quote Repository with Specific Website Filter Magento provides a quote repository that allows you to specify the website scope when retrieving quotes:

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $quoteRepository = $objectManager->create('\Magento\Quote\Model\QuoteRepository');
    $quote = $quoteRepository->get($order->getQuoteId(), null, false);
    
  3. Load the Quote Using the Quote Model Factory Magento’s quote model factory allows you to specify the website scope when loading a quote:

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $quoteFactory = $objectManager->create('\Magento\Quote\Model\QuoteFactory');
    $quote = $quoteFactory->create()->load($order->getQuoteId());
    

Remember, using Magento’s built-in factory methods or repositories is always the recommended approach. These methods handle the website context automatically and ensure data consistency across your application.

Conclusion

The getAllItems() method in Magento 2 may fail when dealing with quotes from different websites due to the scoped nature of quote-related data. To resolve this issue, you should either set the correct website context before loading the quote or use Magento’s repository and factory methods that handle the website scope internally.

By following these best practices, you can ensure a seamless integration between your custom cron jobs, after-sale processes, and Magento’s core functionality. If you need further assistance with Magento 2 development or wish to explore advanced features like multi-site configurations in depth, I recommend checking out our comprehensive guides at Laravel Company.

Magento 2 Checkout Catalog API

Our network