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


Hello magento friends todays in this blog post im share with you to How to Filter Order Collection by Specific Product Item Name in Magento 2.


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


In Magento 2, you can filter the order collection by a specific product item name by using the built-in collection filtering capabilities. Here's a step-by-step guide on how to achieve this:


Step 1: Create a custom module (if you haven't already)


If you don't have a custom module, you'll need to create one to add the filtering functionality.


Step 2: Define a function to filter the order collection


In your custom module, create a new PHP file, e.g., FilterOrder.php, and define a function to filter the order collection. You can place this file in your module's Model or Helper folder, depending on your preferences. Here's an example of the function:


<?php

namespace [Vendor]\[Module]\Model;

use Magento\Framework\Api\FilterBuilder;

use Magento\Framework\Api\Search\FilterGroupBuilder;

use Magento\Framework\Api\SearchCriteriaBuilder;

use Magento\Framework\App\ResourceConnection;


class FilterOrder

{

    protected $filterBuilder;

    protected $filterGroupBuilder;

    protected $searchCriteriaBuilder;

    protected $resourceConnection;


    public function __construct(

        FilterBuilder $filterBuilder,

        FilterGroupBuilder $filterGroupBuilder,

        SearchCriteriaBuilder $searchCriteriaBuilder,

        ResourceConnection $resourceConnection

    ) {

        $this->filterBuilder = $filterBuilder;

        $this->filterGroupBuilder = $filterGroupBuilder;

        $this->searchCriteriaBuilder = $searchCriteriaBuilder;

        $this->resourceConnection = $resourceConnection;

    }


    public function getOrderByProductName($productName)

    {

        $connection = $this->resourceConnection->getConnection();

        $orderItemTable = $this->resourceConnection->getTableName('sales_order_item');


        $filter = $this->filterBuilder

            ->setField('name')

            ->setValue('%' . $productName . '%')

            ->setConditionType('like')

            ->create();


        $filterGroup = $this->filterGroupBuilder

            ->addFilter($filter)

            ->create();


        $searchCriteria = $this->searchCriteriaBuilder

            ->setFilterGroups([$filterGroup])

            ->create();


        $orderCollection = \Magento\Framework\App\ObjectManager::getInstance()

            ->get('\Magento\Sales\Api\OrderRepositoryInterface')

            ->getList($searchCriteria);


        return $orderCollection->getItems();

    }

}


Step 3: Use the filter function in your code


In your code where you need to filter orders by the product item name, you can call the getOrderByProductName function from the FilterOrder class. Replace [Vendor]\[Module] with your custom module's namespace.


For example:


$productName = 'Sample Product'; // Replace with the product name you want to filter by

$orderFilter = \Magento\Framework\App\ObjectManager::getInstance()

    ->create('[Vendor]\[Module]\Model\FilterOrder');


$orders = $orderFilter->getOrderByProductName($productName);

foreach ($orders as $order) {

    // Process each order as needed

    $orderId = $order->getEntityId();

    // ...

}



That's it! Now you can filter the order collection by a specific product item name using this custom module and function in Magento 2.



Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post