My Account dashboard orders (Magento 2)
-
21-02-2021 - |
Frage
I am currently showing all my orders
in My Account
dashboard.
The problem only 5 orders are shown when I fetch all orders.
$_orders = $block->getOrders();
but I have 29 orders in the database, it only shows 5 orders. How to get all the orders.
I have UI like this
When I fetch all order for dashboard section in recent.phtml
/Magento/Sales/orders/recent.phtml
i only get 5 orders
whereas /Magento/Sales/orders/history.phtml
i get all orders
I tried this
class Recent extends \Magento\Sales\Block\Order\Recent
{
/**
* Limit of orders
*/
const YX_ORDER_LIMIT = 5000;
/**
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
*/
protected $_orderCollectionFactory;
/**
* @var \Magento\Customer\Model\Session
*/
protected $_customerSession;
/**
* @var \Magento\Sales\Model\Order\Config
*/
protected $_orderConfig;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;
protected function _construct()
{
parent::_construct();
$this->getRecentOrders();
}
private function getRecentOrders()
{
$this->storeManager = ObjectManager::getInstance()
->get(StoreManagerInterface::class);
$orders = $this->_orderCollectionFactory->create()->addAttributeToSelect(
'*'
)->addAttributeToFilter(
'customer_id',
$this->_customerSession->getCustomerId()
)->addAttributeToFilter(
'store_id',
$this->storeManager->getStore()->getId()
)->addAttributeToFilter(
'status',
['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
)->addAttributeToSort(
'created_at',
'desc'
)->setPageSize(
self:: YX_ORDER_LIMIT
)->load();
$this->setOrders($orders);
}
}
It didnt work
Lösung
The method getOrders()
has a filter on order status and shows only the orders with status visible_on_front = 1
(the field is in table sales_order_status_state
). That excludes for example the status pending_payment
.
So you can either set visible_on_front = 1
for every status ore overwrite the block and change the getOrder()
method.
For the recent orders there is a hard limit of 5 in the constructor. You should create a own block extending Magento\Sales\Block\Order\Recent
, implement there the getOrders()
method (copy it from History block) and change the default Magento Block in the layout with your block.