Skip to content

Commit 8fccf46

Browse files
add error to aggregator when trying to delete price when price scope is global
1 parent 8f843ef commit 8fccf46

File tree

3 files changed

+124
-135
lines changed

3 files changed

+124
-135
lines changed

app/code/Magento/Catalog/Model/Product/Price/SpecialPriceStorage.php

Lines changed: 112 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,82 @@
66

77
namespace Magento\Catalog\Model\Product\Price;
88

9+
use Magento\Catalog\Api\Data\SpecialPriceInterface;
10+
use Magento\Catalog\Api\Data\SpecialPriceInterfaceFactory;
11+
use Magento\Catalog\Api\SpecialPriceStorageInterface;
12+
use Magento\Catalog\Model\Product\Price\Validation\InvalidSkuProcessor;
13+
use Magento\Catalog\Model\Product\Price\Validation\Result;
14+
use Magento\Catalog\Model\ProductIdLocatorInterface;
15+
use Magento\Framework\App\ObjectManager;
916
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magento\Catalog\Helper\Data;
18+
use Magento\Store\Api\StoreRepositoryInterface;
1019

1120
/**
1221
* Special price storage presents efficient price API and is used to retrieve, update or delete special prices.
22+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1323
*/
14-
class SpecialPriceStorage implements \Magento\Catalog\Api\SpecialPriceStorageInterface
24+
class SpecialPriceStorage implements SpecialPriceStorageInterface
1525
{
1626
/**
1727
* @var \Magento\Catalog\Api\SpecialPriceInterface
1828
*/
1929
private $specialPriceResource;
2030

2131
/**
22-
* @var \Magento\Catalog\Api\Data\SpecialPriceInterfaceFactory
32+
* @var SpecialPriceInterfaceFactory
2333
*/
2434
private $specialPriceFactory;
2535

2636
/**
27-
* @var \Magento\Catalog\Model\ProductIdLocatorInterface
37+
* @var ProductIdLocatorInterface
2838
*/
2939
private $productIdLocator;
3040

3141
/**
32-
* @var \Magento\Store\Api\StoreRepositoryInterface
42+
* @var StoreRepositoryInterface
3343
*/
3444
private $storeRepository;
3545

3646
/**
37-
* @var \Magento\Catalog\Model\Product\Price\Validation\Result
47+
* @var Result
3848
*/
3949
private $validationResult;
4050

4151
/**
42-
* @var \Magento\Catalog\Model\Product\Price\Validation\InvalidSkuProcessor
52+
* @var InvalidSkuProcessor
4353
*/
4454
private $invalidSkuProcessor;
4555

4656
/**
4757
* @var array
4858
*/
49-
private $allowedProductTypes = [];
59+
private $allowedProductTypes;
60+
61+
/**
62+
* @var Data
63+
*/
64+
private $catalogData;
5065

5166
/**
5267
* @param \Magento\Catalog\Api\SpecialPriceInterface $specialPriceResource
53-
* @param \Magento\Catalog\Api\Data\SpecialPriceInterfaceFactory $specialPriceFactory
54-
* @param \Magento\Catalog\Model\ProductIdLocatorInterface $productIdLocator
55-
* @param \Magento\Store\Api\StoreRepositoryInterface $storeRepository
56-
* @param \Magento\Catalog\Model\Product\Price\Validation\Result $validationResult
57-
* @param \Magento\Catalog\Model\Product\Price\Validation\InvalidSkuProcessor $invalidSkuProcessor
58-
* @param array $allowedProductTypes [optional]
68+
* @param SpecialPriceInterfaceFactory $specialPriceFactory
69+
* @param ProductIdLocatorInterface $productIdLocator
70+
* @param StoreRepositoryInterface $storeRepository
71+
* @param Result $validationResult
72+
* @param InvalidSkuProcessor $invalidSkuProcessor
73+
* @param array $allowedProductTypes
74+
* @param Data|null $catalogData
5975
*/
6076
public function __construct(
6177
\Magento\Catalog\Api\SpecialPriceInterface $specialPriceResource,
62-
\Magento\Catalog\Api\Data\SpecialPriceInterfaceFactory $specialPriceFactory,
63-
\Magento\Catalog\Model\ProductIdLocatorInterface $productIdLocator,
64-
\Magento\Store\Api\StoreRepositoryInterface $storeRepository,
65-
\Magento\Catalog\Model\Product\Price\Validation\Result $validationResult,
66-
\Magento\Catalog\Model\Product\Price\Validation\InvalidSkuProcessor $invalidSkuProcessor,
67-
array $allowedProductTypes = []
78+
SpecialPriceInterfaceFactory $specialPriceFactory,
79+
ProductIdLocatorInterface $productIdLocator,
80+
StoreRepositoryInterface $storeRepository,
81+
Result $validationResult,
82+
InvalidSkuProcessor $invalidSkuProcessor,
83+
array $allowedProductTypes = [],
84+
?Data $catalogData = null
6885
) {
6986
$this->specialPriceResource = $specialPriceResource;
7087
$this->specialPriceFactory = $specialPriceFactory;
@@ -73,10 +90,11 @@ public function __construct(
7390
$this->validationResult = $validationResult;
7491
$this->invalidSkuProcessor = $invalidSkuProcessor;
7592
$this->allowedProductTypes = $allowedProductTypes;
93+
$this->catalogData = $catalogData ?: ObjectManager::getInstance()->get(Data::class);
7694
}
7795

7896
/**
79-
* {@inheritdoc}
97+
* @inheritdoc
8098
*/
8199
public function get(array $skus)
82100
{
@@ -85,7 +103,7 @@ public function get(array $skus)
85103

86104
$prices = [];
87105
foreach ($rawPrices as $rawPrice) {
88-
/** @var \Magento\Catalog\Api\Data\SpecialPriceInterface $price */
106+
/** @var SpecialPriceInterface $price */
89107
$price = $this->specialPriceFactory->create();
90108
$sku = isset($rawPrice['sku'])
91109
? $rawPrice['sku']
@@ -102,7 +120,7 @@ public function get(array $skus)
102120
}
103121

104122
/**
105-
* {@inheritdoc}
123+
* @inheritdoc
106124
*/
107125
public function update(array $prices)
108126
{
@@ -113,7 +131,7 @@ public function update(array $prices)
113131
}
114132

115133
/**
116-
* {@inheritdoc}
134+
* @inheritdoc
117135
*/
118136
public function delete(array $prices)
119137
{
@@ -140,52 +158,13 @@ private function retrieveValidPrices(array $prices)
140158

141159
foreach ($prices as $key => $price) {
142160
if (!$price->getSku() || in_array($price->getSku(), $failedSkus)) {
143-
$this->validationResult->addFailedItem(
144-
$key,
145-
__(
146-
'The product that was requested doesn\'t exist. Verify the product and try again. '
147-
. 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo.',
148-
[
149-
'SKU' => $price->getSku(),
150-
'storeId' => $price->getStoreId(),
151-
'priceFrom' => $price->getPriceFrom(),
152-
'priceTo' => $price->getPriceTo()
153-
]
154-
),
155-
[
156-
'SKU' => $price->getSku(),
157-
'storeId' => $price->getStoreId(),
158-
'priceFrom' => $price->getPriceFrom(),
159-
'priceTo' => $price->getPriceTo()
160-
]
161-
);
161+
$errorMessage = 'The product that was requested doesn\'t exist. Verify the product and try again.';
162+
$this->addFailedItemPrice($price, $key, $errorMessage, []);
162163
}
164+
$this->checkStore($price, $key);
163165
$this->checkPrice($price, $key);
164166
$this->checkDate($price, $price->getPriceFrom(), 'Price From', $key);
165167
$this->checkDate($price, $price->getPriceTo(), 'Price To', $key);
166-
try {
167-
$this->storeRepository->getById($price->getStoreId());
168-
} catch (NoSuchEntityException $e) {
169-
$this->validationResult->addFailedItem(
170-
$key,
171-
__(
172-
'Requested store is not found. '
173-
. 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo.',
174-
[
175-
'SKU' => $price->getSku(),
176-
'storeId' => $price->getStoreId(),
177-
'priceFrom' => $price->getPriceFrom(),
178-
'priceTo' => $price->getPriceTo()
179-
]
180-
),
181-
[
182-
'SKU' => $price->getSku(),
183-
'storeId' => $price->getStoreId(),
184-
'priceFrom' => $price->getPriceFrom(),
185-
'priceTo' => $price->getPriceTo()
186-
]
187-
);
188-
}
189168
}
190169

191170
foreach ($this->validationResult->getFailedRowIds() as $id) {
@@ -195,77 +174,98 @@ private function retrieveValidPrices(array $prices)
195174
return $prices;
196175
}
197176

177+
/**
178+
* Check that store exists and is global when price scope is global and otherwise add error to aggregator.
179+
*
180+
* @param SpecialPriceInterface $price
181+
* @param int $key
182+
* @return void
183+
*/
184+
private function checkStore(SpecialPriceInterface $price, int $key): void
185+
{
186+
if ($this->catalogData->isPriceGlobal() && $price->getStoreId() !== 0) {
187+
$errorMessage = 'Could not change non global Price when price scope is global.';
188+
$this->addFailedItemPrice($price, $key, $errorMessage, []);
189+
}
190+
191+
try {
192+
$this->storeRepository->getById($price->getStoreId());
193+
} catch (NoSuchEntityException $e) {
194+
$errorMessage = 'Requested store is not found.';
195+
$this->addFailedItemPrice($price, $key, $errorMessage, []);
196+
}
197+
}
198+
198199
/**
199200
* Check that date value is correct and add error to aggregator if it contains incorrect data.
200201
*
201-
* @param \Magento\Catalog\Api\Data\SpecialPriceInterface $price
202+
* @param SpecialPriceInterface $price
202203
* @param string $value
203204
* @param string $label
204205
* @param int $key
205206
* @return void
206207
*/
207-
private function checkDate(\Magento\Catalog\Api\Data\SpecialPriceInterface $price, $value, $label, $key)
208+
private function checkDate(SpecialPriceInterface $price, $value, $label, $key)
208209
{
209210
if ($value && !$this->isCorrectDateValue($value)) {
210-
$this->validationResult->addFailedItem(
211-
$key,
212-
__(
213-
'Invalid attribute %label = %priceTo. '
214-
. 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo.',
215-
[
216-
'label' => $label,
217-
'SKU' => $price->getSku(),
218-
'storeId' => $price->getStoreId(),
219-
'priceFrom' => $price->getPriceFrom(),
220-
'priceTo' => $price->getPriceTo()
221-
]
222-
),
223-
[
224-
'label' => $label,
225-
'SKU' => $price->getSku(),
226-
'storeId' => $price->getStoreId(),
227-
'priceFrom' => $price->getPriceFrom(),
228-
'priceTo' => $price->getPriceTo()
229-
]
230-
);
211+
$errorMessage = 'Invalid attribute %label = %priceTo.';
212+
$this->addFailedItemPrice($price, $key, $errorMessage, ['label' => $label]);
231213
}
232214
}
233215

234216
/**
235-
* Check that provided price value is not empty and not lower then zero and add error to aggregator if price
217+
* Check price.
218+
*
219+
* Verify that provided price value is not empty and not lower then zero and add error to aggregator if price
236220
* contains not valid data.
237221
*
238-
* @param \Magento\Catalog\Api\Data\SpecialPriceInterface $price
222+
* @param SpecialPriceInterface $price
239223
* @param int $key
240224
* @return void
241225
*/
242-
private function checkPrice(\Magento\Catalog\Api\Data\SpecialPriceInterface $price, $key)
226+
private function checkPrice(SpecialPriceInterface $price, int $key): void
243227
{
244228
if (null === $price->getPrice() || $price->getPrice() < 0) {
245-
$this->validationResult->addFailedItem(
246-
$key,
247-
__(
248-
'Invalid attribute Price = %price. '
249-
. 'Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo.',
250-
[
251-
'price' => $price->getPrice(),
252-
'SKU' => $price->getSku(),
253-
'storeId' => $price->getStoreId(),
254-
'priceFrom' => $price->getPriceFrom(),
255-
'priceTo' => $price->getPriceTo()
256-
]
257-
),
258-
[
259-
'price' => $price->getPrice(),
260-
'SKU' => $price->getSku(),
261-
'storeId' => $price->getStoreId(),
262-
'priceFrom' => $price->getPriceFrom(),
263-
'priceTo' => $price->getPriceTo()
264-
]
265-
);
229+
$errorMessage = 'Invalid attribute Price = %price.';
230+
$this->addFailedItemPrice($price, $key, $errorMessage, ['price' => $price->getPrice()]);
266231
}
267232
}
268233

234+
/**
235+
* Adds failed item price to validation result
236+
*
237+
* @param SpecialPriceInterface $price
238+
* @param int $key
239+
* @param string $message
240+
* @param array $firstParam
241+
* @return void
242+
*/
243+
private function addFailedItemPrice(
244+
SpecialPriceInterface $price,
245+
int $key,
246+
string $message,
247+
array $firstParam
248+
): void {
249+
$additionalInfo = [];
250+
if ($firstParam) {
251+
$additionalInfo = array_merge($additionalInfo, $firstParam);
252+
}
253+
254+
$additionalInfo['SKU'] = $price->getSku();
255+
$additionalInfo['storeId'] = $price->getStoreId();
256+
$additionalInfo['priceFrom'] = $price->getPriceFrom();
257+
$additionalInfo['priceTo'] = $price->getPriceTo();
258+
259+
$this->validationResult->addFailedItem(
260+
$key,
261+
__(
262+
$message . ' Row ID: SKU = %SKU, Store ID: %storeId, Price From: %priceFrom, Price To: %priceTo.',
263+
$additionalInfo
264+
),
265+
$additionalInfo
266+
);
267+
}
268+
269269
/**
270270
* Retrieve SKU by product ID.
271271
*

0 commit comments

Comments
 (0)