1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Block\Order;
use Magento\Framework\View\Element\Template\Context;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Customer\Model\Session;
use Magento\Sales\Model\Order\Config;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\ObjectManager;
/**
* Sales order history block
*
* @api
* @since 100.0.2
*/
class Recent extends \Magento\Framework\View\Element\Template
{
/**
* Limit of orders
*/
const ORDER_LIMIT = 5;
/**
* @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;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Sales\Model\Order\Config $orderConfig
* @param array $data
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __construct(
Context $context,
CollectionFactory $orderCollectionFactory,
Session $customerSession,
Config $orderConfig,
array $data = [],
StoreManagerInterface $storeManager = null
) {
$this->_orderCollectionFactory = $orderCollectionFactory;
$this->_customerSession = $customerSession;
$this->_orderConfig = $orderConfig;
$this->_isScopePrivate = true;
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
->get(StoreManagerInterface::class);
parent::__construct($context, $data);
}
/**
* @return void
*/
protected function _construct()
{
parent::_construct();
$this->getRecentOrders();
}
/**
* Get recently placed orders. By default they will be limited by 5.
*/
private function getRecentOrders()
{
$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::ORDER_LIMIT
)->load();
$this->setOrders($orders);
}
/**
* @param object $order
* @return string
*/
public function getViewUrl($order)
{
return $this->getUrl('sales/order/view', ['order_id' => $order->getId()]);
}
/**
* @param object $order
* @return string
*/
public function getTrackUrl($order)
{
return $this->getUrl('sales/order/track', ['order_id' => $order->getId()]);
}
/**
* @return string
*/
protected function _toHtml()
{
if ($this->getOrders()->getSize() > 0) {
return parent::_toHtml();
}
return '';
}
/**
* @param object $order
* @return string
*/
public function getReorderUrl($order)
{
return $this->getUrl('sales/order/reorder', ['order_id' => $order->getId()]);
}
}