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:
And in class constructor using
@param
you are explicitely injectingThe
@param
declaration should be collection factory. And collection factory's di object variable should be$orderCollectionFactory
So it should look like below:
Also Your __construct is missing the 's'.