Get order collection between a date range magento 2

Clash 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);
magento2
add a comment |Â
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);
magento2
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
add a comment |Â
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);
magento2
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
magento2
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
add a comment |Â
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
add a comment |Â
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());
?>
add a comment |Â
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));
}
}
add a comment |Â
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());
?>
add a comment |Â
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());
?>
add a comment |Â
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());
?>
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());
?>
answered 29 mins ago
Ansar Husain
1,236217
1,236217
add a comment |Â
add a comment |Â
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));
}
}
add a comment |Â
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));
}
}
add a comment |Â
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));
}
}
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));
}
}
answered 20 mins ago
Mohit Kumar Arora
5,37041332
5,37041332
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password

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