2

I need to write code to get next 10 orders above the given order id.

<?php


namespace Learning\GreetingMessage\Controller\Index;


use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Context;
use Vendor\Module\Model\ModuleFactory;
use Magento\Sales\Api\OrderRepositoryInterface;

use Magento\Framework\Setup\SchemaSetupInterface;

class Test extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;

protected $_orderCollectionFactory;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    //~ \Magento\Framework\View\Result\PageFactory $pageFactory,
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
    )
{
    //~ $this->_pageFactory = $pageFactory;
    $this->_orderCollectionFactory = $orderCollectionFactory;
    return parent::__construct($context);
}

public function execute()
{
     //~ $orders = $this->_objectManager->create('Magento\Sales\Api\Data\OrderInterface')->getCollection()->addAttributeToFilter('created_at',  array('from'=>date('Y-m-d',strtotime('2018-01-01')), 'to'=>date('Y-m-d',strtotime('2019-01-01'))));   // Getting first order
     //~ $orders = $this->_objectManager->create('Magento\Sales\Api\Data\OrderInterface')->getCollection()->addAttributeToFilter('entity_id',  array('gt'=>0)); 
     $orders = $this->_objectManager->create('Magento\Sales\Api\Data\OrderInterface')->setPageSize(1)->setCurPag‌​e(10)->getCollection()->load(); 
     echo "Count is ". count($orders);
     foreach($orders as $order)
     {

         $gross = $order->getGrandTotal();
         $tax = $order->getTaxAmount();
         $net = $gross - $tax;
         $payment = $order->getPayment();
         $method = $payment->getMethodInstance();
         $methodTitle = $method->getTitle();

         echo "<br/>gross :". $gross;
         echo "<br/>tax :". $tax;
         echo "<br/>net :". $net;
         echo "<br/>Created :". $order->getCreatedAt();
         echo "<br/>state :". $order->getState();
         echo "<br/>payment methodTitle :". $methodTitle;
         echo "<br/> Customer : ". $order->getCustomerId();
         echo "<br/>Items in order";

         $items = $order->getAllItems();
        foreach($order->getAllItems() as $orderItem) {
            $itemid =  $orderItem->getId();
            echo "Item id" .$orderItem->getId() . "<br />";
        }
    }    


    echo "Hello World";
    exit;
}

}

2
  • Check my answer and let me know if its working or not. Nov 21, 2018 at 9:07
  • Please click on "right sign" also in my answer for accept answer if it's useful for you. So, it will useful for other developer also :) Nov 21, 2018 at 9:10

1 Answer 1

2

You need to use addFieldToFilter() instead of addAttributeToFilter(). Try to use this below code :

protected $_orderCollectionFactory;
public function __construct(
    .........
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
    .........
) {
    .........
    $this->_orderCollectionFactory = $orderCollectionFactory;
    .........
}
public function execute() {
    $ordercollection = $this->_orderCollectionFactory->create()->addFieldToSelect(array('*'))->addFieldToFilter('entity_id', ['gt' => $orderid]);
    $ordercollection->setOrder('entity_id', 'DESC');
    $ordercollection->setPageSize(10);
    foreach ($ordercollection as $order) {
        $gross = $order->getGrandTotal();
        $tax = $order->getTaxAmount();
        $net = $gross - $tax;

        echo "gross :" . $gross;
        echo "<br/>tax :" . $tax;
        echo "<br/>net :" . $net;
        echo "<br/>Created :" . $order->getCreatedAt();

        echo "Items in order";

        $items = $order->getAllItems();
        foreach ($order->getAllItems() as $orderItem) {
            echo $orderItem->getId() . "<br />";
        }
    }
}
9
  • while i use "\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory" in __construct i face error. Shall i load any thing before the __construct . I am working in magento module Nov 21, 2018 at 9:15
  • Did you remove generated folder? execute this command : rm -rf generated/ Nov 21, 2018 at 9:16
  • No i am not remove that folder Nov 21, 2018 at 9:16
  • You need to remove generated folder and flush cache when you change something in construct. Nov 21, 2018 at 9:17
  • Execute this command : rm -rf generated/ and php bin/magento c:f Nov 21, 2018 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.