Get order collection between a date range magento 2

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I have code for magento 1 site. Need to run on Magento 2.
I have used this code on outside of magento using this.



use MagentoFrameworkAppBootstrap;
require_once '../app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');


$fromDate = date('Y-m-d 00:00:00', strtotime($_POST['fromdate1']));
$toDate = date('Y-m-d 23:59:59', strtotime($_POST['todate1']));
$order_status = $_POST['order_status1'];
$orders = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate));
$orders->addFieldToFilter('status', array('in' => array($order_status)));
//echo $totalOrders = count($orders);









share|improve this question























  • Can you please mention where you need the order Collection in custom block, helper or somewhere else ?
    – ROBIN
    52 mins ago










  • I need it on out side of magento .
    – Pratik Kamani
    44 mins ago
















up vote
2
down vote

favorite












I have code for magento 1 site. Need to run on Magento 2.
I have used this code on outside of magento using this.



use MagentoFrameworkAppBootstrap;
require_once '../app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');


$fromDate = date('Y-m-d 00:00:00', strtotime($_POST['fromdate1']));
$toDate = date('Y-m-d 23:59:59', strtotime($_POST['todate1']));
$order_status = $_POST['order_status1'];
$orders = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate));
$orders->addFieldToFilter('status', array('in' => array($order_status)));
//echo $totalOrders = count($orders);









share|improve this question























  • Can you please mention where you need the order Collection in custom block, helper or somewhere else ?
    – ROBIN
    52 mins ago










  • I need it on out side of magento .
    – Pratik Kamani
    44 mins ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have code for magento 1 site. Need to run on Magento 2.
I have used this code on outside of magento using this.



use MagentoFrameworkAppBootstrap;
require_once '../app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');


$fromDate = date('Y-m-d 00:00:00', strtotime($_POST['fromdate1']));
$toDate = date('Y-m-d 23:59:59', strtotime($_POST['todate1']));
$order_status = $_POST['order_status1'];
$orders = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate));
$orders->addFieldToFilter('status', array('in' => array($order_status)));
//echo $totalOrders = count($orders);









share|improve this question















I have code for magento 1 site. Need to run on Magento 2.
I have used this code on outside of magento using this.



use MagentoFrameworkAppBootstrap;
require_once '../app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');


$fromDate = date('Y-m-d 00:00:00', strtotime($_POST['fromdate1']));
$toDate = date('Y-m-d 23:59:59', strtotime($_POST['todate1']));
$order_status = $_POST['order_status1'];
$orders = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate));
$orders->addFieldToFilter('status', array('in' => array($order_status)));
//echo $totalOrders = count($orders);






magento2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 40 mins ago

























asked 56 mins ago









Pratik Kamani

721415




721415











  • Can you please mention where you need the order Collection in custom block, helper or somewhere else ?
    – ROBIN
    52 mins ago










  • I need it on out side of magento .
    – Pratik Kamani
    44 mins ago
















  • Can you please mention where you need the order Collection in custom block, helper or somewhere else ?
    – ROBIN
    52 mins ago










  • I need it on out side of magento .
    – Pratik Kamani
    44 mins ago















Can you please mention where you need the order Collection in custom block, helper or somewhere else ?
– ROBIN
52 mins ago




Can you please mention where you need the order Collection in custom block, helper or somewhere else ?
– ROBIN
52 mins ago












I need it on out side of magento .
– Pratik Kamani
44 mins ago




I need it on out side of magento .
– Pratik Kamani
44 mins ago










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Use Below code in your Root file.



 <?php
use MagentoFrameworkAppBootstrap;

require __DIR__ . '/app/bootstrap.php';

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');


$now = new DateTime();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$OrderFactory = $objectManager->create('MagentoSalesModelResourceModelOrderCollectionFactory');
$orderCollection = $OrderFactory->create()->addFieldToSelect(array('*'));
$orderCollection->addFieldToFilter('created_at', ['lteq' => $now->format('Y-m-d H:i:s')])->addFieldToFilter('created_at', ['gteq' => $now->format('2018-05-01 H:i:s')]);
echo "<pre>"; print_r($orderCollection->getData());
?>





share|improve this answer



























    up vote
    1
    down vote













    Try below code from any class within Magento. You need to change namespace, class name and function name as per your requirement:



    <?php
    namespace 'NAME_SPACEOFYOURCLASS';

    class YOURCALSS extends MagentoFrameworkAppActionAction


    protected $_orderCollectionFactory;

    public function __construct(
    MagentoFrameworkAppActionContext $context,
    MagentoSalesModelResourceModelOrderCollectionFactory $orderCollectionFactory
    )
    $this->_orderCollectionFactory = $orderCollectionFactory;
    parent::__construct($context);




    public function YOURFUNCTION()

    $collection = $this->_orderCollectionFactory->create()->addAttributeToSelect('*');

    // You Can filter collection as
    $this->orderCollectionFactory
    ->addFieldToFilter('created_at', array('gteq' => $fromDate))
    ->addFieldToFilter('created_at', array('lteq' => $toDate));



    }
    }





    share|improve this answer




















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "479"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f242695%2fget-order-collection-between-a-date-range-magento-2%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      Use Below code in your Root file.



       <?php
      use MagentoFrameworkAppBootstrap;

      require __DIR__ . '/app/bootstrap.php';

      $params = $_SERVER;

      $bootstrap = Bootstrap::create(BP, $params);

      $objectManager = $bootstrap->getObjectManager();

      $state = $objectManager->get('MagentoFrameworkAppState');
      $state->setAreaCode('frontend');


      $now = new DateTime();
      $objectManager = MagentoFrameworkAppObjectManager::getInstance();
      $OrderFactory = $objectManager->create('MagentoSalesModelResourceModelOrderCollectionFactory');
      $orderCollection = $OrderFactory->create()->addFieldToSelect(array('*'));
      $orderCollection->addFieldToFilter('created_at', ['lteq' => $now->format('Y-m-d H:i:s')])->addFieldToFilter('created_at', ['gteq' => $now->format('2018-05-01 H:i:s')]);
      echo "<pre>"; print_r($orderCollection->getData());
      ?>





      share|improve this answer
























        up vote
        1
        down vote



        accepted










        Use Below code in your Root file.



         <?php
        use MagentoFrameworkAppBootstrap;

        require __DIR__ . '/app/bootstrap.php';

        $params = $_SERVER;

        $bootstrap = Bootstrap::create(BP, $params);

        $objectManager = $bootstrap->getObjectManager();

        $state = $objectManager->get('MagentoFrameworkAppState');
        $state->setAreaCode('frontend');


        $now = new DateTime();
        $objectManager = MagentoFrameworkAppObjectManager::getInstance();
        $OrderFactory = $objectManager->create('MagentoSalesModelResourceModelOrderCollectionFactory');
        $orderCollection = $OrderFactory->create()->addFieldToSelect(array('*'));
        $orderCollection->addFieldToFilter('created_at', ['lteq' => $now->format('Y-m-d H:i:s')])->addFieldToFilter('created_at', ['gteq' => $now->format('2018-05-01 H:i:s')]);
        echo "<pre>"; print_r($orderCollection->getData());
        ?>





        share|improve this answer






















          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Use Below code in your Root file.



           <?php
          use MagentoFrameworkAppBootstrap;

          require __DIR__ . '/app/bootstrap.php';

          $params = $_SERVER;

          $bootstrap = Bootstrap::create(BP, $params);

          $objectManager = $bootstrap->getObjectManager();

          $state = $objectManager->get('MagentoFrameworkAppState');
          $state->setAreaCode('frontend');


          $now = new DateTime();
          $objectManager = MagentoFrameworkAppObjectManager::getInstance();
          $OrderFactory = $objectManager->create('MagentoSalesModelResourceModelOrderCollectionFactory');
          $orderCollection = $OrderFactory->create()->addFieldToSelect(array('*'));
          $orderCollection->addFieldToFilter('created_at', ['lteq' => $now->format('Y-m-d H:i:s')])->addFieldToFilter('created_at', ['gteq' => $now->format('2018-05-01 H:i:s')]);
          echo "<pre>"; print_r($orderCollection->getData());
          ?>





          share|improve this answer












          Use Below code in your Root file.



           <?php
          use MagentoFrameworkAppBootstrap;

          require __DIR__ . '/app/bootstrap.php';

          $params = $_SERVER;

          $bootstrap = Bootstrap::create(BP, $params);

          $objectManager = $bootstrap->getObjectManager();

          $state = $objectManager->get('MagentoFrameworkAppState');
          $state->setAreaCode('frontend');


          $now = new DateTime();
          $objectManager = MagentoFrameworkAppObjectManager::getInstance();
          $OrderFactory = $objectManager->create('MagentoSalesModelResourceModelOrderCollectionFactory');
          $orderCollection = $OrderFactory->create()->addFieldToSelect(array('*'));
          $orderCollection->addFieldToFilter('created_at', ['lteq' => $now->format('Y-m-d H:i:s')])->addFieldToFilter('created_at', ['gteq' => $now->format('2018-05-01 H:i:s')]);
          echo "<pre>"; print_r($orderCollection->getData());
          ?>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 29 mins ago









          Ansar Husain

          1,236217




          1,236217






















              up vote
              1
              down vote













              Try below code from any class within Magento. You need to change namespace, class name and function name as per your requirement:



              <?php
              namespace 'NAME_SPACEOFYOURCLASS';

              class YOURCALSS extends MagentoFrameworkAppActionAction


              protected $_orderCollectionFactory;

              public function __construct(
              MagentoFrameworkAppActionContext $context,
              MagentoSalesModelResourceModelOrderCollectionFactory $orderCollectionFactory
              )
              $this->_orderCollectionFactory = $orderCollectionFactory;
              parent::__construct($context);




              public function YOURFUNCTION()

              $collection = $this->_orderCollectionFactory->create()->addAttributeToSelect('*');

              // You Can filter collection as
              $this->orderCollectionFactory
              ->addFieldToFilter('created_at', array('gteq' => $fromDate))
              ->addFieldToFilter('created_at', array('lteq' => $toDate));



              }
              }





              share|improve this answer
























                up vote
                1
                down vote













                Try below code from any class within Magento. You need to change namespace, class name and function name as per your requirement:



                <?php
                namespace 'NAME_SPACEOFYOURCLASS';

                class YOURCALSS extends MagentoFrameworkAppActionAction


                protected $_orderCollectionFactory;

                public function __construct(
                MagentoFrameworkAppActionContext $context,
                MagentoSalesModelResourceModelOrderCollectionFactory $orderCollectionFactory
                )
                $this->_orderCollectionFactory = $orderCollectionFactory;
                parent::__construct($context);




                public function YOURFUNCTION()

                $collection = $this->_orderCollectionFactory->create()->addAttributeToSelect('*');

                // You Can filter collection as
                $this->orderCollectionFactory
                ->addFieldToFilter('created_at', array('gteq' => $fromDate))
                ->addFieldToFilter('created_at', array('lteq' => $toDate));



                }
                }





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Try below code from any class within Magento. You need to change namespace, class name and function name as per your requirement:



                  <?php
                  namespace 'NAME_SPACEOFYOURCLASS';

                  class YOURCALSS extends MagentoFrameworkAppActionAction


                  protected $_orderCollectionFactory;

                  public function __construct(
                  MagentoFrameworkAppActionContext $context,
                  MagentoSalesModelResourceModelOrderCollectionFactory $orderCollectionFactory
                  )
                  $this->_orderCollectionFactory = $orderCollectionFactory;
                  parent::__construct($context);




                  public function YOURFUNCTION()

                  $collection = $this->_orderCollectionFactory->create()->addAttributeToSelect('*');

                  // You Can filter collection as
                  $this->orderCollectionFactory
                  ->addFieldToFilter('created_at', array('gteq' => $fromDate))
                  ->addFieldToFilter('created_at', array('lteq' => $toDate));



                  }
                  }





                  share|improve this answer












                  Try below code from any class within Magento. You need to change namespace, class name and function name as per your requirement:



                  <?php
                  namespace 'NAME_SPACEOFYOURCLASS';

                  class YOURCALSS extends MagentoFrameworkAppActionAction


                  protected $_orderCollectionFactory;

                  public function __construct(
                  MagentoFrameworkAppActionContext $context,
                  MagentoSalesModelResourceModelOrderCollectionFactory $orderCollectionFactory
                  )
                  $this->_orderCollectionFactory = $orderCollectionFactory;
                  parent::__construct($context);




                  public function YOURFUNCTION()

                  $collection = $this->_orderCollectionFactory->create()->addAttributeToSelect('*');

                  // You Can filter collection as
                  $this->orderCollectionFactory
                  ->addFieldToFilter('created_at', array('gteq' => $fromDate))
                  ->addFieldToFilter('created_at', array('lteq' => $toDate));



                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 20 mins ago









                  Mohit Kumar Arora

                  5,37041332




                  5,37041332



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f242695%2fget-order-collection-between-a-date-range-magento-2%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      White Anglo-Saxon Protestant

                      Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                      One-line joke