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.
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.
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!
In modern web development, seamless navigation and state management are crucial for delivering a smooth…
Magento Open Source 2.4.8 beta version released on October 8, 2024. The latest release of…
Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…
As the world of eCommerce continues to thrive, Shopify has become one of the most…
Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…
Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…