0

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
      ->addFilter('status','pending','eq')
      ->addSortOrder($this->sortBuilder->setField('entity_id')
      ->setDescendingDirection()->create())
      ->setPageSize(100)->setCurrentPage(1)->create();

    $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 at 3:30
  • @Msquare, yes how can we get that, – jafar pinjar Apr 7 at 5:25
  • If you are getting orderId or IncrementId inside loop the please try to load orderObject too get Customer email address. – Abhishek Panchal Apr 7 at 13:30
  • @AbhishekPanchal, I printed $ordersList->getData() into the log, getting customer email for that but if i use with foreah not working – jafar pinjar Apr 7 at 13:35
  • Can you please add you entire log print? – Abhishek Panchal Apr 7 at 13:35
1

app/code/VendoreName/ModuleName/etc

crontab.xml

<?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>
        </job>
    </group>
</config>

app/code/VendoreName/ModuleName/Cron

Cronfile.php

<?php

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->addAttributeToSelect('*');
                    $prd_data->addFieldToFilter('brand', 'option_1'); // set your filter value here 
                    // show your data 
                    //print_r($prd_data->getData());

                    // get order id here 
                    if($prd_data->getData()) {
                        print_r($order_value->getId());
                    }

                }//
            }
        }

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

    public function getOrderList()
    {
        $order_collection = $this->_orderCollectionFactory->create();
        $order_collection->addAttributeToSelect('*');
        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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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