Filtering orders in a Magento 2 store can be a powerful way to gain insights into your sales data. Whether you need to analyze specific time periods or identify orders containing products from a particular attribute set, this tutorial will guide you through the process of creating a script to filter order collections by date range and product set ID.

Please Note: This script is intended for educational purposes and may require customization to fit your specific needs.


Before you begin, ensure you have the following:

  1. Access to your Magento 2 store.
  2. Basic knowledge of Magento 2.
  3. A product set ID that you want to use as a filter criterion.

Step 1: Setting Up the Script

Start by creating a PHP script and placing it within your Magento 2 installation directory. You can name the file something like order_filter.php. Here’s the initial script:


use Magento\Framework\App\Bootstrap;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Framework\App\Area;

require __DIR__ . '/../app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$areaCode = Area::AREA_ADMINHTML;

try {
} catch (LocalizedException $e) {
    // Area code is already set

$dateTime = $objectManager->get(DateTime::class);
$collectionFactory = $objectManager->get(CollectionFactory::class);

// Set your product set ID
$productSetId = 4; // Replace with the actual product set ID you want to filter by

try {
    // Create an order collection
    $orderCollection = $collectionFactory->create();
            'from' => $dateTime->gmtDate('Y-m-d H:i:s', strtotime('2022-01-01')),
            'to' => $dateTime->gmtDate('Y-m-d H:i:s', strtotime('2023-01-01')),
            'date' => true

    $orderIds = [];

    foreach ($orderCollection as $order) {
        // Get order items
        $orderItems = $order->getAllVisibleItems();
        $orderContainsProductSet = false;

        foreach ($orderItems as $item) {
            $product = $item->getProduct();

            if ($product !== null) {
                $itemProductSetId = $product->getAttributeSetId();
                if ($itemProductSetId == $productSetId) {
                    $orderContainsProductSet = true;

        if ($orderContainsProductSet) {
            $orderIds[] = $order->getId();

    foreach ($orderIds as $orderId) {
        echo "Order ID: " . $orderId . "\n";
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();

Step 2: Customize the Script

In the script above, you need to make the following customizations:

  • Set your product set ID: Replace $productSetId = 4; with the actual product set ID you want to use as the filter criterion.
  • Specify the date range: Modify the strtotime values in the addFieldToFilter method to define your desired date range.

Step 3: Running the Script

Save the script and navigate to your Magento 2 installation directory using a command-line terminal. Then, execute the script as follows:

php order_filter.php

The script will filter and display order IDs that match the specified product set and date range.


Filtering order collections by specific criteria, such as date ranges and product set IDs, can provide valuable insights into your store’s sales data. This script serves as a starting point for customizing and extracting the information you need from your Magento 2 store.

Remember to adapt the script to your specific requirements and use it responsibly to maintain the integrity of your e-commerce data.

Rate this post