How to Filter Order Collection by Specific Product Item Name in Magento 2
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.
order collection
Comments
Post a Comment