1

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;

    }


}
3
  • I will try the code Feb 4, 2018 at 15:22
  • Let me check the code again and let you know Feb 4, 2018 at 15:24
  • try to do di:compile
    – Keyur Shah
    Feb 4, 2018 at 18:50

1 Answer 1

0

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'.

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.