Magento 2 get latest order collection
Today we will talk about Magento 2 get latest order collection. We will learn how to get latest order collection. You can simply add the setOrder entity_id to DESC or use created_at to DESC to your order collection. If you want to add limit simply add setpagesize
setPageSize(yournumberhere);
protected $_orderFactory;
public function __construct(
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderFactory,
)
{
$this->_orderFactory = $orderFactory;
}
public function getLatestOrders() {
$orderCollection =$this->_orderFactory->create()->addFieldToSelect(array('*')); $orderCollection->setOrder('entity_id','DESC');
return $orderCollection;
}
public function __construct(
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderFactory,
)
{
$this->_orderFactory = $orderFactory;
}
public function getLatestOrders() {
$orderCollection =$this->_orderFactory->create()->addFieldToSelect(array('*')); $orderCollection->setOrder('entity_id','DESC');
return $orderCollection;
}
2 thoughts on “Magento 2 get latest order collection”
Leave a Reply
You must be logged in to post a comment.
There is syntax error in getLatestOrder function, object operater -> is missing before create() method.
Thanks for pointing out.