Magento – Magento2 does n’t get the order collection from Order\CollectionFactory

collection;magento2magento2.2sales-order

I am using the Magento2.2 version .
I am using the class Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
to get the order collection.But getting the error

Fatal error: Uncaught Error: Call to a member function create() on
null.

when I try to call create() function.
Why I getting the error.please give a solution…

My code is:

<?php
namespace {VendorName}\{moduleName}\Cron;
use Magento\Sales\Api\Data\OrderAddressInterface;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;

class {class name}
{
    protected $orderAddress;
    protected  $orderCollectionFactory;

    /**
     * @param OrderAddressInterface $orderAddressInterface
     * @param CollectionFactoryInterface $collectionFactoryInterface
     */
    public function __contruct(

        OrderAddressInterface $orderAddressInterface,
        CollectionFactory $collectionFactoryInterface
    )
    {
        $this->orderCollectionFactory = $collectionFactoryInterface;
        $this->orderAddress = $orderAddressInterface;
    }
    public function execute()
    {
        $dateNow = (new \DateTime())->format('Y-m-d H:i:s');
       echo  $days_ago = date('Y-m-d H:i:s', strtotime('-180 days', strtotime($dateNow)));

        $orderCollection =  $this->orderCollectionFactory->create()
            ->addFieldToFilter('updated_at', ['lteq' => $days_ago]);
        echo $orderCollection->getSelect()->__toString();
        return $orderCollection;

    }


}

Best Answer

Looks like your order collection factory class's attribute not assigned properly.

You declared:

protected  $orderCollectionFactory;

And in class constructor using @param you are explicitely injecting

CollectionFactory $collectionFactoryInterface

The @param declaration should be collection factory. And collection factory's di object variable should be $orderCollectionFactory

So it should look like below:

    <?php
namespace {VendorName}\{moduleName}\Cron;
use Magento\Sales\Api\Data\OrderAddressInterface;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;

class {class name}
{
    protected $orderAddress;
    protected  $orderCollectionFactory;

    /**
     * @param OrderAddressInterface $orderAddressInterface
     * @param CollectionFactory $collectionFactory
     */
    public function __construct(

        OrderAddressInterface $orderAddressInterface,
        CollectionFactory $collectionFactory
    )
    {
        $this->orderCollectionFactory = $collectionFactory;
        $this->orderAddress = $orderAddressInterface;
    }
    public function execute()
    {
        $dateNow = (new \DateTime())->format('Y-m-d H:i:s');
       echo  $days_ago = date('Y-m-d H:i:s', strtotime('-180 days', strtotime($dateNow)));

        $orderCollection =  $this->orderCollectionFactory->create()
            ->addFieldToFilter('updated_at', ['lteq' => $days_ago]);
        echo $orderCollection->getSelect()->__toString();
        return $orderCollection;

    }
}

Also Your __construct is missing the 's'.

Related Topic