Magento 2 Get Ordered Products Where Order Status Is X

Magento 2 Get Ordered Products Where Order Status Is X

July 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 Get Ordered Products Where Order Status Is X

Magento 2: Retrieving Ordered Products by Status

As a Magento developer building your first module, it’s common to want to retrieve ordered products based on specific criteria. In this case, you’re looking for all items that have been ordered with a status of ‘pending’. Let me guide you through the process using models, resource models, collections, and repositories in Magento 2.

Understanding the Magento Data Access Layers

Magento employs several layers to access and manipulate data:

  • Model: The business logic layer that handles data operations.
  • Resource Model: The technical implementation of the model’s CRUD operations on the database level.
  • Collection: A Magento-specific collection of objects that allows you to query data from multiple tables at once, using SQL-like syntax.
  • Repository: An interface that provides a unified way to interact with all these layers.

Retrieving Ordered Products by Status

To fetch ordered products filtered by status, you’ll need to create your own custom repository and use it in conjunction with the built-in Magento sales_order_product collection. Here’s a step-by-step guide:

Step 1: Create Your Custom Repository Interface

First, define an interface for your custom repository:

<?php
namespace YourNamespace\YourModule\Api;

use Magento\Framework\Api\SearchCriteriaInterface;

interface YourCustomRepositoryInterface
{
    public function getPendingOrderProducts(SearchCriteriaInterface $searchCriteria);
}

Step 2: Implement the Repository

Implement this interface in your custom repository class:

<?php
namespace YourNamespace\YourModule\Api\Data;

use Magento\Framework\App\ResourceConnection;
use Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory as SalesOrderItemCollectionFactory;

class YourCustomRepository implements YourCustomRepositoryInterface
{
    private $resource;
    private $salesOrderItemCollectionFactory;

    public function __construct(
        ResourceConnection $resource,
        SalesOrderItemCollectionFactory $salesOrderItemCollectionFactory
    ) {
        $this->resource = $resource;
        $this->salesOrderItemCollectionFactory = $salesOrderItemCollectionFactory;
    }

    public function getPendingOrderProducts(SearchCriteriaInterface $searchCriteria)
    {
        // More implementation
    }
}

Step 3: Query the Sales Order Product Collection

In your getPendingOrderProducts method, query the sales order product collection and filter by status:

public function getPendingOrderProducts(SearchCriteriaInterface $searchCriteria)
{
    $collection = $this->salesOrderItemCollectionFactory->create();
    $collection->addFieldToFilter('order_status', 'pending');

    // Apply additional filters or sorting from the SearchCriteria, if needed

    return $collection;
}

Step 4: Use Your Custom Repository in Your Module’s Logic

Now you can inject your custom repository into any part of your module and use it to fetch pending order products:

public function fetchPendingOrderProducts()
{
    $searchCriteria = $this->searchCriteriaBuilder->create();
    return $this->repositoryInterface->getPendingOrderProducts($searchCriteria);
}

Conclusion

In this guide, we explored how to retrieve ordered products with a specific status in Magento 2 using the custom repository pattern. We covered creating an interface, implementing the repository, and querying the sales order product collection.

This approach provides a clean separation of concerns between your business logic and data access layers while leveraging Magento’s built-in collections for efficient querying. Remember to always follow best practices like injecting dependencies via the constructor and using factory classes for collections.

For more advanced Magento 2 development techniques, be sure to check out other tutorials and blog posts on Laravel Company.

Magento 2 Catalog Database API

Our network