How To

How to Filter Order Collection by Specific Product Item Name in Magento 2?

Hello Magento Friends,

In this blog post, we will explore how to filter order collections in Magento 2 based on a specific product item name.

Filtering order collections based on specific product item names can be useful in various scenarios, such as generating reports, analyzing sales data, or performing targeted marketing campaigns for your Magento 2 store.

By following the steps outlined below, you’ll be able to retrieve orders containing a particular product efficiently.

Steps to Filter Order Collection by Specific Product Item Name in Magento 2:

Step 1: Create a file in your Magento root directory at the below path

magento_root_directory\getOrderItem.php

Then add the code as follows

<?php
use Magento\Framework\App\Bootstrap;
require_once __DIR__ . '/../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

try {
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $productname='test 2'; // Replace with product name you want to search
 $orderdata = $objectManager->create('\Magento\Sales\Model\ResourceModel\Order\CollectionFactory')->create()->addAttributeToSelect('*');
 $orderdata->getSelect()->join(array('order_item' => 'sales_order_item'),'main_table.entity_id = order_item.order_id');
 $orderdata->addFieldToFilter('order_item.name', array(array('like' => '%'.$productname.'%')));
 echo "<pre>";
 print_r($orderdata->getdata());
 echo "</pre>";
}
catch (\Exception $e){
    echo "Exception Throw \n";
    echo $e->getMessage() . " \n";
}

Output:

It will print all order data filter by given product name.

Conclusion:

By following the steps outlined in this blog post, you can easily retrieve orders that contain a particular product item. Whether you’re generating reports, analyzing sales data, or planning targeted marketing campaigns, this technique will help you gain valuable insights from your Magento 2 store.

You can also Get Orders Collection Between a Date Range in Magento 2.

Share the tutorial with your friends, and stay in touch with us.

Happy Coding!

Click to rate this post!
[Total: 0 Average: 0]
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

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago