I am trying to fetch full order collection by product attribute option.

I have a "brand" attribute with options "option_1", "option_2", "option_3". This attribute is assigned against each product sku.

I have a written a cron file which triggers everyday.

In that file i need to fetch all the orders where product sku with attribute set as "option_1".

I have used below code to get the order collection for past 2 days.

Class Cronfile{

protected $searchCriteriaBuilder;
protected $sortBuilder;
protected $orderRepository;
public function __construct(
 \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
    \Magento\Framework\Api\SortOrderBuilder $sortBuilder
  $this->orderRepository = $orderRepository;
  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  $this->sortBuilder = $sortBuilder;

public function getOrderCollection(){
    $searchCriteria = $this->searchCriteriaBuilder

    $to = date("Y-m-d h:i:s"); // current date
    $from = strtotime('-2 day', strtotime($to));
    $from = date('Y-m-d h:i:s', $from); // 2 days before

    $ordersList = $this->orderRepository->getList($searchCriteria);
    $ordersList->addFieldToFilter('created_at', array('from'=>$from, 'to'=>$to));
    foreach ($ordersListas $orderItem) {
        echo $orderItem->customerEmail();

Here i need to fetch all the orders with the product attribute option value.

can someone help me on this please? Thanks in advance!!

  • Please explain bit more . You need only attribute value option 1 products order ?
    – Msquare
    Apr 7, 2020 at 3:30
  • @Msquare, yes how can we get that, Apr 7, 2020 at 5:25
  • If you are getting orderId or IncrementId inside loop the please try to load orderObject too get Customer email address. Apr 7, 2020 at 13:30
  • @AbhishekPanchal, I printed $ordersList->getData() into the log, getting customer email for that but if i use with foreah not working Apr 7, 2020 at 13:35
  • Can you please add you entire log print? Apr 7, 2020 at 13:35

1 Answer 1




<?xml version="1.0" ?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="default">
        <job instance="VendoreName\ModuleName\Cron\Cronfile" method="execute" name="my_cronfile">
            <schedule>*/5 * * * *</schedule>




namespace VendoreName\ModuleName\Cron;

use Psr\Log\LoggerInterface;

class Cronfile
    protected $logger;
    protected $_orderCollectionFactory;
    protected $orderRepository;
    protected $_customerSession;

    public function __construct(
        LoggerInterface $logger,
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
    ) {
        $this->logger = $logger;
        $this->_orderCollectionFactory = $orderCollectionFactory;
        $this->orderRepository = $orderRepository;
        $this->_productCollectionFactory = $productCollectionFactory;  

    public function execute()
        $this->logger->info("my croun run");
        // get all order data
        $order_data = $this->getOrderList();

        foreach ($order_data as $order_key => $order_value) {
            // get order id
            $order_id = $order_value->getIncrementId();

            // get order items
            $order_items = $this->getOrderItems($order_id);
            if (!empty($order_items)) {
                foreach ($order_items->getAllVisibleItems() as $key_items => $value_items) {
                    //get product id
                    $prd_id = $value_items->getId();
                    //filter product collection by id 
                    $prd_data = $this->_productCollectionFactory->create();
                    $prd_data->addFieldToFilter('entity_id', $prd_id);
                    $prd_data->addFieldToFilter('brand', 'option_1'); // set your filter value here 
                    // show your data 

                    // get order id here 
                    if($prd_data->getData()) {


        $this->logger->info("my croun over");

    public function getOrderList()
        $order_collection = $this->_orderCollectionFactory->create();
        return $order_collection;

    public function getOrderItems($order_id)
        return $this->orderRepository->get($order_id);

before run this code please create blank controller and test this code and make your change as your requirement.

I Hope This Helps You.

  • tested this I am looking for code to list all the order id's with product attribute has particular option value Apr 8, 2020 at 9:54
  • is it working for you? getting Fatal error: Allowed memory size of 792723456 bytes exhausted. Apr 8, 2020 at 13:13
  • yes it's works in which line this error come ?
    – Msquare
    Apr 8, 2020 at 13:14
  • its not showing the error line number for me compile success, may be issue with foreach loop with large data Apr 8, 2020 at 13:20
  • hi @jafarpinjar you need to create an blank controller and add code line by and check everithing.
    – Msquare
    Apr 8, 2020 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.