Skip to content

Commit bc12557

Browse files
committed
ACP2E-1232: [Cloud] Customers can add to cart after being changed to a different Customer Group for which Add to Cart is disallowed [Sep 28 Launch]
- with test
1 parent 24a2c46 commit bc12557

File tree

2 files changed

+50
-33
lines changed

2 files changed

+50
-33
lines changed

app/code/Magento/Sales/CustomerData/LastOrderedItems.php

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,75 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Sales\CustomerData;
79

10+
use Magento\Catalog\Model\Product;
11+
use Magento\CatalogInventory\Api\StockRegistryInterface;
812
use Magento\Customer\CustomerData\SectionSourceInterface;
913
use Magento\Catalog\Api\ProductRepositoryInterface;
14+
use Magento\Customer\Model\Session;
15+
use Magento\Framework\App\Http\Context;
1016
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;
1123
use Psr\Log\LoggerInterface;
1224

1325
/**
1426
* Returns information for "Recently Ordered" widget.
1527
* It contains list of 5 salable products from the last placed order.
1628
* Qty of products to display is limited by LastOrderedItems::SIDEBAR_ORDER_LIMIT constant.
29+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1730
*/
1831
class LastOrderedItems implements SectionSourceInterface
1932
{
2033
/**
21-
* Limit of orders in side bar
34+
* Limit of orders in sidebar
2235
*/
23-
const SIDEBAR_ORDER_LIMIT = 5;
36+
public const SIDEBAR_ORDER_LIMIT = 5;
2437

2538
/**
26-
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface
39+
* @var CollectionFactoryInterface
2740
*/
2841
protected $_orderCollectionFactory;
2942

3043
/**
31-
* @var \Magento\Sales\Model\Order\Config
44+
* @var Config
3245
*/
3346
protected $_orderConfig;
3447

3548
/**
36-
* @var \Magento\Customer\Model\Session
49+
* @var Session
3750
*/
3851
protected $_customerSession;
3952

4053
/**
41-
* @var \Magento\Framework\App\Http\Context
54+
* @var Context
4255
*/
4356
protected $httpContext;
4457

4558
/**
46-
* @var \Magento\Sales\Model\ResourceModel\Order\Collection
59+
* @var Collection
4760
*/
4861
protected $orders;
4962

5063
/**
51-
* @var \Magento\CatalogInventory\Api\StockRegistryInterface
64+
* @var StockRegistryInterface
5265
*/
5366
protected $stockRegistry;
5467

5568
/**
56-
* @var \Magento\Store\Model\StoreManagerInterface
69+
* @var StoreManagerInterface
5770
*/
5871
private $_storeManager;
5972

6073
/**
61-
* @var \Magento\Catalog\Api\ProductRepositoryInterface
74+
* @var ProductRepositoryInterface
6275
*/
6376
private $productRepository;
6477

@@ -68,20 +81,20 @@ class LastOrderedItems implements SectionSourceInterface
6881
private $logger;
6982

7083
/**
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
7689
* @param ProductRepositoryInterface $productRepository
7790
* @param LoggerInterface $logger
7891
*/
7992
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,
8598
ProductRepositoryInterface $productRepository,
8699
LoggerInterface $logger
87100
) {
@@ -99,7 +112,7 @@ public function __construct(
99112
*
100113
* @return void
101114
*/
102-
protected function initOrders()
115+
protected function initOrders(): void
103116
{
104117
$customerId = $this->_customerSession->getCustomerId();
105118

@@ -116,18 +129,19 @@ protected function initOrders()
116129
* Get list of last ordered products
117130
*
118131
* @return array
132+
* @throws NoSuchEntityException
119133
*/
120-
protected function getItems()
134+
protected function getItems(): array
121135
{
122136
$items = [];
123137
$order = $this->getLastOrder();
124138
$limit = self::SIDEBAR_ORDER_LIMIT;
125139

126140
if ($order) {
127141
$website = $this->_storeManager->getStore()->getWebsiteId();
128-
/** @var \Magento\Sales\Model\Order\Item $item */
142+
/** @var Item $item */
129143
foreach ($order->getParentItemsRandomCollection($limit) as $item) {
130-
/** @var \Magento\Catalog\Model\Product $product */
144+
/** @var Product $product */
131145
try {
132146
$product = $this->productRepository->getById(
133147
$item->getProductId(),
@@ -145,6 +159,7 @@ protected function getItems()
145159
'name' => $item->getName(),
146160
'url' => $url,
147161
'is_saleable' => $this->isItemAvailableForReorder($item),
162+
'product_id' => $item->getProductId(),
148163
];
149164
}
150165
}
@@ -156,10 +171,10 @@ protected function getItems()
156171
/**
157172
* Check item product availability for reorder
158173
*
159-
* @param \Magento\Sales\Model\Order\Item $orderItem
174+
* @param Item $orderItem
160175
* @return boolean
161176
*/
162-
protected function isItemAvailableForReorder(\Magento\Sales\Model\Order\Item $orderItem)
177+
protected function isItemAvailableForReorder(Item $orderItem)
163178
{
164179
try {
165180
$stockItem = $this->stockRegistry->getStockItem(
@@ -175,7 +190,7 @@ protected function isItemAvailableForReorder(\Magento\Sales\Model\Order\Item $or
175190
/**
176191
* Last order getter
177192
*
178-
* @return \Magento\Sales\Model\Order|void
193+
* @return Order|void
179194
*/
180195
protected function getLastOrder()
181196
{
@@ -190,7 +205,7 @@ protected function getLastOrder()
190205
/**
191206
* @inheritdoc
192207
*/
193-
public function getSectionData()
208+
public function getSectionData(): array
194209
{
195210
return ['items' => $this->getItems()];
196211
}

app/code/Magento/Sales/Test/Unit/CustomerData/LastOrderedItemsTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,15 @@ public function testGetSectionData(): void
132132
'id' => 1,
133133
'name' => 'Product Name 1',
134134
'url' => 'http://example.com',
135-
'is_saleable' => true
135+
'is_saleable' => true,
136+
'product_id' => 1
136137
];
137138
$expectedItem2 = [
138139
'id' => 2,
139140
'name' => 'Product Name 2',
140141
'url' => null,
141-
'is_saleable' => true
142+
'is_saleable' => true,
143+
'product_id' => 2
142144
];
143145
$productIdVisible = 1;
144146
$productIdNotVisible = 2;
@@ -175,12 +177,12 @@ public function testGetSectionData(): void
175177
$productNotVisible->expects($this->never())->method('getProductUrl');
176178
$productNotVisible->expects($this->once())->method('getWebsiteIds')->willReturn([1, 4]);
177179
$productNotVisible->expects($this->once())->method('getId')->willReturn($productIdNotVisible);
178-
$itemWithVisibleProduct->expects($this->once())->method('getProductId')->willReturn($productIdVisible);
180+
$itemWithVisibleProduct->expects($this->any())->method('getProductId')->willReturn($productIdVisible);
179181
$itemWithVisibleProduct->expects($this->once())->method('getProduct')->willReturn($productVisible);
180182
$itemWithVisibleProduct->expects($this->once())->method('getId')->willReturn($expectedItem1['id']);
181183
$itemWithVisibleProduct->expects($this->once())->method('getName')->willReturn($expectedItem1['name']);
182184
$itemWithVisibleProduct->expects($this->once())->method('getStore')->willReturn($storeMock);
183-
$itemWithNotVisibleProduct->expects($this->once())->method('getProductId')->willReturn($productIdNotVisible);
185+
$itemWithNotVisibleProduct->expects($this->any())->method('getProductId')->willReturn($productIdNotVisible);
184186
$itemWithNotVisibleProduct->expects($this->once())->method('getProduct')->willReturn($productNotVisible);
185187
$itemWithNotVisibleProduct->expects($this->once())->method('getId')->willReturn($expectedItem2['id']);
186188
$itemWithNotVisibleProduct->expects($this->once())->method('getName')->willReturn($expectedItem2['name']);
@@ -255,7 +257,7 @@ public function testGetSectionDataWithNotExistingProduct(): void
255257
->method('getParentItemsRandomCollection')
256258
->with(LastOrderedItems::SIDEBAR_ORDER_LIMIT)
257259
->willReturn([$orderItemMock]);
258-
$orderItemMock->expects($this->once())->method('getProductId')->willReturn($productId);
260+
$orderItemMock->expects($this->any())->method('getProductId')->willReturn($productId);
259261
$this->productRepositoryMock->expects($this->once())
260262
->method('getById')
261263
->with($productId, false, $storeId)

0 commit comments

Comments
 (0)