I have a cron job to execute some tasks over new orders. The cron job is executing perfectly. But facing an issue with the below code snippet.
Below logs are printing in system.log except the log statement in foreach. Getting query results when I am executing the MySQL statement from getSelect().
Then why its not looping through foreach ?
class ProcessPendingorders
{
protected $_logger;
protected $_orderCollectionFactory;
protected $_date;
public function __construct(
\Psr\Log\LoggerInterface $logger,
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $date
) {
$this->_logger = $logger;
$this->_orderCollectionFactory = $orderCollectionFactory;
$this->_date = $date;
}
public function execute()
{
$this->_logger->info("Orders Start - ");
$current_date = $this->_date->date()->format('Y-m-d H:i:s');
$start_date = strtotime($current_date) - 10000;
$start_date = date("Y-m-d H:i:s",$start_date);
$orders = $this->_orderCollectionFactory->create()->addAttributeToSelect('*')
->addAttributeToFilter('state', ['in' => 'new'])
->addAttributeToFilter('created_at', ['gteq'=>$start_date])
;
$this->_logger->info($orders->getSelect());
foreach($orders as $_order)
$this->_logger->info($_order->getId());
}
}