3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Sales \CustomerData ;
7
9
10
+ use Magento \Catalog \Model \Product ;
11
+ use Magento \CatalogInventory \Api \StockRegistryInterface ;
8
12
use Magento \Customer \CustomerData \SectionSourceInterface ;
9
13
use Magento \Catalog \Api \ProductRepositoryInterface ;
14
+ use Magento \Customer \Model \Session ;
15
+ use Magento \Framework \App \Http \Context ;
10
16
use Magento \Framework \Exception \NoSuchEntityException ;
17
+ use Magento \Sales \Model \Order ;
18
+ use Magento \Sales \Model \Order \Config ;
19
+ use Magento \Sales \Model \Order \Item ;
20
+ use Magento \Sales \Model \ResourceModel \Order \Collection ;
21
+ use Magento \Sales \Model \ResourceModel \Order \CollectionFactoryInterface ;
22
+ use Magento \Store \Model \StoreManagerInterface ;
11
23
use Psr \Log \LoggerInterface ;
12
24
13
25
/**
14
26
* Returns information for "Recently Ordered" widget.
15
27
* It contains list of 5 salable products from the last placed order.
16
28
* Qty of products to display is limited by LastOrderedItems::SIDEBAR_ORDER_LIMIT constant.
29
+ * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
17
30
*/
18
31
class LastOrderedItems implements SectionSourceInterface
19
32
{
20
33
/**
21
- * Limit of orders in side bar
34
+ * Limit of orders in sidebar
22
35
*/
23
- const SIDEBAR_ORDER_LIMIT = 5 ;
36
+ public const SIDEBAR_ORDER_LIMIT = 5 ;
24
37
25
38
/**
26
- * @var \Magento\Sales\Model\ResourceModel\Order\ CollectionFactoryInterface
39
+ * @var CollectionFactoryInterface
27
40
*/
28
41
protected $ _orderCollectionFactory ;
29
42
30
43
/**
31
- * @var \Magento\Sales\Model\Order\ Config
44
+ * @var Config
32
45
*/
33
46
protected $ _orderConfig ;
34
47
35
48
/**
36
- * @var \Magento\Customer\Model\ Session
49
+ * @var Session
37
50
*/
38
51
protected $ _customerSession ;
39
52
40
53
/**
41
- * @var \Magento\Framework\App\Http\ Context
54
+ * @var Context
42
55
*/
43
56
protected $ httpContext ;
44
57
45
58
/**
46
- * @var \Magento\Sales\Model\ResourceModel\Order\ Collection
59
+ * @var Collection
47
60
*/
48
61
protected $ orders ;
49
62
50
63
/**
51
- * @var \Magento\CatalogInventory\Api\ StockRegistryInterface
64
+ * @var StockRegistryInterface
52
65
*/
53
66
protected $ stockRegistry ;
54
67
55
68
/**
56
- * @var \Magento\Store\Model\ StoreManagerInterface
69
+ * @var StoreManagerInterface
57
70
*/
58
71
private $ _storeManager ;
59
72
60
73
/**
61
- * @var \Magento\Catalog\Api\ ProductRepositoryInterface
74
+ * @var ProductRepositoryInterface
62
75
*/
63
76
private $ productRepository ;
64
77
@@ -68,20 +81,20 @@ class LastOrderedItems implements SectionSourceInterface
68
81
private $ logger ;
69
82
70
83
/**
71
- * @param \Magento\Sales\Model\ResourceModel\Order\ CollectionFactoryInterface $orderCollectionFactory
72
- * @param \Magento\Sales\Model\Order\ Config $orderConfig
73
- * @param \Magento\Customer\Model\ Session $customerSession
74
- * @param \Magento\CatalogInventory\Api\ StockRegistryInterface $stockRegistry
75
- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
84
+ * @param CollectionFactoryInterface $orderCollectionFactory
85
+ * @param Config $orderConfig
86
+ * @param Session $customerSession
87
+ * @param StockRegistryInterface $stockRegistry
88
+ * @param StoreManagerInterface $storeManager
76
89
* @param ProductRepositoryInterface $productRepository
77
90
* @param LoggerInterface $logger
78
91
*/
79
92
public function __construct (
80
- \ Magento \ Sales \ Model \ ResourceModel \ Order \ CollectionFactoryInterface $ orderCollectionFactory ,
81
- \ Magento \ Sales \ Model \ Order \ Config $ orderConfig ,
82
- \ Magento \ Customer \ Model \ Session $ customerSession ,
83
- \ Magento \ CatalogInventory \ Api \ StockRegistryInterface $ stockRegistry ,
84
- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
93
+ CollectionFactoryInterface $ orderCollectionFactory ,
94
+ Config $ orderConfig ,
95
+ Session $ customerSession ,
96
+ StockRegistryInterface $ stockRegistry ,
97
+ StoreManagerInterface $ storeManager ,
85
98
ProductRepositoryInterface $ productRepository ,
86
99
LoggerInterface $ logger
87
100
) {
@@ -99,7 +112,7 @@ public function __construct(
99
112
*
100
113
* @return void
101
114
*/
102
- protected function initOrders ()
115
+ protected function initOrders (): void
103
116
{
104
117
$ customerId = $ this ->_customerSession ->getCustomerId ();
105
118
@@ -116,18 +129,19 @@ protected function initOrders()
116
129
* Get list of last ordered products
117
130
*
118
131
* @return array
132
+ * @throws NoSuchEntityException
119
133
*/
120
- protected function getItems ()
134
+ protected function getItems (): array
121
135
{
122
136
$ items = [];
123
137
$ order = $ this ->getLastOrder ();
124
138
$ limit = self ::SIDEBAR_ORDER_LIMIT ;
125
139
126
140
if ($ order ) {
127
141
$ website = $ this ->_storeManager ->getStore ()->getWebsiteId ();
128
- /** @var \Magento\Sales\Model\Order\ Item $item */
142
+ /** @var Item $item */
129
143
foreach ($ order ->getParentItemsRandomCollection ($ limit ) as $ item ) {
130
- /** @var \Magento\Catalog\Model\ Product $product */
144
+ /** @var Product $product */
131
145
try {
132
146
$ product = $ this ->productRepository ->getById (
133
147
$ item ->getProductId (),
@@ -145,6 +159,7 @@ protected function getItems()
145
159
'name ' => $ item ->getName (),
146
160
'url ' => $ url ,
147
161
'is_saleable ' => $ this ->isItemAvailableForReorder ($ item ),
162
+ 'product_id ' => $ item ->getProductId (),
148
163
];
149
164
}
150
165
}
@@ -156,10 +171,10 @@ protected function getItems()
156
171
/**
157
172
* Check item product availability for reorder
158
173
*
159
- * @param \Magento\Sales\Model\Order\ Item $orderItem
174
+ * @param Item $orderItem
160
175
* @return boolean
161
176
*/
162
- protected function isItemAvailableForReorder (\ Magento \ Sales \ Model \ Order \ Item $ orderItem )
177
+ protected function isItemAvailableForReorder (Item $ orderItem )
163
178
{
164
179
try {
165
180
$ stockItem = $ this ->stockRegistry ->getStockItem (
@@ -175,7 +190,7 @@ protected function isItemAvailableForReorder(\Magento\Sales\Model\Order\Item $or
175
190
/**
176
191
* Last order getter
177
192
*
178
- * @return \Magento\Sales\Model\ Order|void
193
+ * @return Order|void
179
194
*/
180
195
protected function getLastOrder ()
181
196
{
@@ -190,7 +205,7 @@ protected function getLastOrder()
190
205
/**
191
206
* @inheritdoc
192
207
*/
193
- public function getSectionData ()
208
+ public function getSectionData (): array
194
209
{
195
210
return ['items ' => $ this ->getItems ()];
196
211
}
0 commit comments