1

Trying to create a cron job to export order details: amount, total number of orders, order created time. etc. Below is the cron file. On executing this I get error

<?php

namespace Vendor_Name\Salesverification\Cron;

class Salesverification extends \Magento\Framework\App\Action\Action

{

protected $logger;

protected $_order;

protected $resultPageFactory;

public function __construct(
    \Psr\Log\LoggerInterface $loggerInterface,

    \Magento\Framework\App\Action\Context $context,
    \Magento\Sales\Model\Order $_order, // <-- new line 
    \Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
    $this->logger = $loggerInterface;

    parent::__construct($context);
    $this->_order = $_order; // <-- new line
    $this->resultPageFactory = $resultPageFactory;
}

public function execute() {
    $_orderId = 1; 
    // Order Id here or the variable that you plan on using 
    // to specify it. You could pass it by using: 
    // $this->getRequest()->getParam('id', 0)

    /** @var $_order \Magento\Sales\Model\Order  */
    $_order = $this->_order->load($_orderId);

    // Get all items from your order
    $_items = $_order->getAllItems();

    // Iterate through all of the items in the order
    foreach ($_items as $_item) {

        // Get sku, item_id and price
        $_orderItems[] = [
            'sku' => $_item->getSku(),
            'item_id' => $_item->getId(),
            'price' => $_item->getPrice(),
        ];

    }

  return $this->resultPageFactory->create(); 
 }
}

I was checking for atleast one order if this works then could make this work for all the orders but am getting the below error.

On executing the above file I get following error.

Magento\Framework\Exception\LocalizedException: Area code is not set in C:\xampp\htdocs\fao\html\vendor\magento\framework\App\State.php:139 Stack trace: #0 C:\xampp\htdocs\fao\html\vendor\magento\framework\Session\SessionManager.php(173): Magento\Framework\App\State->getAreaCode() #1 C:\xampp\htdocs\fao\html\var\generation\Magento\Framework\Session\Generic\Interceptor.php(50): Magento\Framework\Session\SessionManager->start() #2 C:\xampp\htdocs\fao\html\vendor\magento\framework\Session\SessionManager.php(130): Magento\Framework\Session\Generic\Interceptor->start() #3 C:\xampp\htdocs\fao\html\var\generation\Magento\Framework\Session\Generic\Interceptor.php(14): Magento\Framework\Session\SessionManager->__construct(Object(Magento\Framework\App\Request\Http), Object(Magento\Framework\Session\SidResolver\Proxy), Object(Magento\Framework\Session\Config), Object(Magento\Framework\Session\SaveHandler), Object(Magento\Framework\Session\Validator), Object(Magento\Framework\Session\Storage), Object(Magento\Framework\Stdlib\Cookie\PhpCookieManager), Object(Magento\Framework\Stdlib\Cookie\CookieMetadataFactory), Object(Magento\Framework\App\State)) #4 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php(93): Magento\Framework\Session\Generic\Interceptor->__construct(Object(Magento\Framework\App\Request\Http), Object(Magento\Framework\Session\SidResolver\Proxy), Object(Magento\Framework\Session\Config), Object(Magento\Framework\Session\SaveHandler), Object(Magento\Framework\Session\Validator), Object(Magento\Framework\Session\Storage), Object(Magento\Framework\Stdlib\Cookie\PhpCookieManager), Object(Magento\Framework\Stdlib\Cookie\CookieMetadataFactory), Object(Magento\Framework\App\State)) #5 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php(89): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Magento\\Framewo...', Array) #6 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\ObjectManager.php(71): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Magento\\Framewo...') #7 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php(126): Magento\Framework\ObjectManager\ObjectManager->get('Magento\\Framewo...') #8 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php(53): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument(Array, 'Magento\\Framewo...', NULL, 'session', 'Magento\\Store\\A...') #9 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php(82): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments('Magento\\Store\\A...', Array, Array) #10 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\ObjectManager.php(71): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Magento\\Store\\A...') #11 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php(126): Magento\Framework\ObjectManager\ObjectManager->get('Magento\\Store\\A...') #12 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php(53): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument(Array, 'Magento\\Framewo...', NULL, 'redirect', 'Magento\\Framewo...') #13 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php(82): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments('Magento\\Framewo...', Array, Array) #14 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\ObjectManager.php(71): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Magento\\Framewo...') #15 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php(126): Magento\Framework\ObjectManager\ObjectManager->get('Magento\\Framewo...') #16 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php(53): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument(Array, 'Magento\\Framewo...', NULL, 'context', 'Corra\\SalesVeri...') #17 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php(82): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments('Corra\\SalesVeri...', Array, Array) #18 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\ObjectManager.php(57): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Corra\\SalesVeri...', Array) #19 C:\xampp\htdocs\fao\html\testapp.php(7): Magento\Framework\ObjectManager\ObjectManager->create('Corra\\SalesVeri...') #20 C:\xampp\htdocs\fao\html\vendor\magento\framework\App\Bootstrap.php(258): testapp->launch() #21 C:\xampp\htdocs\fao\html\test.php(7): Magento\Framework\App\Bootstrap->run(Object(testapp)) #22 {main} Next Magento\Framework\Exception\SessionException: Area code not set: Area code must be set before starting a session. in C:\xampp\htdocs\fao\html\vendor\magento\framework\Session\SessionManager.php:175 Stack trace: #0 C:\xampp\htdocs\fao\html\var\generation\Magento\Framework\Session\Generic\Interceptor.php(50): Magento\Framework\Session\SessionManager->start() #1 C:\xampp\htdocs\fao\html\vendor\magento\framework\Session\SessionManager.php(130): Magento\Framework\Session\Generic\Interceptor->start() #2 C:\xampp\htdocs\fao\html\var\generation\Magento\Framework\Session\Generic\Interceptor.php(14): Magento\Framework\Session\SessionManager->__construct(Object(Magento\Framework\App\Request\Http), Object(Magento\Framework\Session\SidResolver\Proxy), Object(Magento\Framework\Session\Config), Object(Magento\Framework\Session\SaveHandler), Object(Magento\Framework\Session\Validator), Object(Magento\Framework\Session\Storage), Object(Magento\Framework\Stdlib\Cookie\PhpCookieManager), Object(Magento\Framework\Stdlib\Cookie\CookieMetadataFactory), Object(Magento\Framework\App\State)) #3 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php(93): Magento\Framework\Session\Generic\Interceptor->__construct(Object(Magento\Framework\App\Request\Http), Object(Magento\Framework\Session\SidResolver\Proxy), Object(Magento\Framework\Session\Config), Object(Magento\Framework\Session\SaveHandler), Object(Magento\Framework\Session\Validator), Object(Magento\Framework\Session\Storage), Object(Magento\Framework\Stdlib\Cookie\PhpCookieManager), Object(Magento\Framework\Stdlib\Cookie\CookieMetadataFactory), Object(Magento\Framework\App\State)) #4 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php(89): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Magento\\Framewo...', Array) #5 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\ObjectManager.php(71): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Magento\\Framewo...') #6 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php(126): Magento\Framework\ObjectManager\ObjectManager->get('Magento\\Framewo...') #7 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php(53): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument(Array, 'Magento\\Framewo...', NULL, 'session', 'Magento\\Store\\A...') #8 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php(82): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments('Magento\\Store\\A...', Array, Array) #9 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\ObjectManager.php(71): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Magento\\Store\\A...') #10 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php(126): Magento\Framework\ObjectManager\ObjectManager->get('Magento\\Store\\A...') #11 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php(53): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument(Array, 'Magento\\Framewo...', NULL, 'redirect', 'Magento\\Framewo...') #12 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php(82): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments('Magento\\Framewo...', Array, Array) #13 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\ObjectManager.php(71): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Magento\\Framewo...') #14 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php(126): Magento\Framework\ObjectManager\ObjectManager->get('Magento\\Framewo...') #15 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php(53): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument(Array, 'Magento\\Framewo...', NULL, 'context', 'Corra\\SalesVeri...') #16 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php(82): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments('Corra\\SalesVeri...', Array, Array) #17 C:\xampp\htdocs\fao\html\vendor\magento\framework\ObjectManager\ObjectManager.php(57): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Corra\\SalesVeri...', Array) #18 C:\xampp\htdocs\fao\html\testapp.php(7): Magento\Framework\ObjectManager\ObjectManager->create('Corra\\SalesVeri...') #19 C:\xampp\htdocs\fao\html\vendor\magento\framework\App\Bootstrap.php(258): testapp->launch() #20 C:\xampp\htdocs\fao\html\test.php(7): Magento\Framework\App\Bootstrap->run(Object(testapp)) #21 {main}
2
1

In Magento1 for to filter order by date range we could use filter with 'from' and 'to'

 $orders = Mage::getModel('sales/order')->getCollection()
->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate))

In Magento2, I could figure out the following way to complete my task

<?php

namespace Corra\Salesverification\Cron;

class Salesverification
{
/**
 * @var \Psr\Log\LoggerInterface
 */
protected $logger;
/**
 * @var \Magento\Framework\App\State
 */
protected $state;

 /**
 * @var \Magento\Sales\Model\ResourceModel\Order\Collection
 */
protected $_OrderCollection;


public function __construct(
    \Magento\Framework\App\State $state,
    \Psr\Log\LoggerInterface $loggerInterface,
    \Magento\Sales\Model\ResourceModel\Order\Collection $_OrderCollection
) {
    $this->logger = $loggerInterface;
    $this->_OrderCollection = $_OrderCollection;
    $state->setAreaCode('frontend');
}

public function execute() {
    $from = "2017-02-13";
    $to = '2017-02-14';

    echo "<pre>";
    $collection = $this->_OrderCollection;
    $collection
    ->addFieldToFilter('created_at', array('gteq' => $from))
    ->addFieldToFilter('created_at', array('lteq' => $to));         

     print_r($collection->getData());

}
}

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.