Skip to content

Commit 0747149

Browse files
committed
MAGETWO-83706: Scheduled Update to existing Group Price / Special Price removes the previously configured price, or results in changes not being saved
1 parent e6c8042 commit 0747149

File tree

2 files changed

+48
-23
lines changed
  • app/code/Magento/Catalog/Model/Product/Attribute/Backend/TierPrice
  • dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Attribute/Backend

2 files changed

+48
-23
lines changed

app/code/Magento/Catalog/Model/Product/Attribute/Backend/TierPrice/UpdateHandler.php

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -84,33 +84,13 @@ public function execute($entity, $arguments = [])
8484
$websiteId = $this->storeManager->getStore($entity->getStoreId())->getWebsiteId();
8585
$isGlobal = $attribute->isScopeGlobal() || $websiteId === 0;
8686
$identifierField = $this->metadataPoll->getMetadata(ProductInterface::class)->getLinkField();
87-
$priceRows = array_filter($priceRows);
8887
$productId = $entity->getData($identifierField);
89-
$old = [];
90-
$new = [];
9188

92-
// prepare original data for compare
89+
// prepare original data to compare
9390
$origPrices = $entity->getOrigData($attribute->getName());
94-
if (is_array($origPrices)) {
95-
foreach ($origPrices as $data) {
96-
if ($isGlobal === $this->isWebsiteGlobal((int)$data['website_id'])) {
97-
$key = $this->getPriceKey($data);
98-
$old[$key] = $data;
99-
}
100-
}
101-
}
102-
91+
$old = $this->prepareOriginalDataToCompare($origPrices, $isGlobal);
10392
// prepare data for save
104-
foreach ($priceRows as $data) {
105-
if (empty($data['delete'])
106-
&& (!empty($data['price_qty'])
107-
|| isset($data['cust_group'])
108-
|| $isGlobal === $this->isWebsiteGlobal((int)$data['website_id']))
109-
) {
110-
$key = $this->getPriceKey($data);
111-
$new[$key] = $this->prepareTierPrice($data);
112-
}
113-
}
93+
$new = $this->prepareNewDataForSave($priceRows, $isGlobal);
11494

11595
$delete = array_diff_key($old, $new);
11696
$insert = array_diff_key($new, $old);
@@ -279,4 +259,48 @@ private function isWebsiteGlobal(int $websiteId): bool
279259
{
280260
return $websiteId === 0;
281261
}
262+
263+
/**
264+
* @param $origPrices
265+
* @param bool $isGlobal
266+
* @return array
267+
*/
268+
private function prepareOriginalDataToCompare($origPrices, $isGlobal = true): array
269+
{
270+
$old = [];
271+
if (is_array($origPrices)) {
272+
foreach ($origPrices as $data) {
273+
if ($isGlobal === $this->isWebsiteGlobal((int)$data['website_id'])) {
274+
$key = $this->getPriceKey($data);
275+
$old[$key] = $data;
276+
}
277+
}
278+
}
279+
280+
return $old;
281+
}
282+
283+
/**
284+
* @param $priceRows
285+
* @param bool $isGlobal
286+
* @return array
287+
* @throws \Magento\Framework\Exception\LocalizedException
288+
*/
289+
private function prepareNewDataForSave($priceRows, $isGlobal = true): array
290+
{
291+
$new = [];
292+
$priceRows = array_filter($priceRows);
293+
foreach ($priceRows as $data) {
294+
if (empty($data['delete'])
295+
&& (!empty($data['price_qty'])
296+
|| isset($data['cust_group'])
297+
|| $isGlobal === $this->isWebsiteGlobal((int)$data['website_id']))
298+
) {
299+
$key = $this->getPriceKey($data);
300+
$new[$key] = $this->prepareTierPrice($data);
301+
}
302+
}
303+
304+
return $new;
305+
}
282306
}

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Attribute/Backend/TierpriceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Test class for \Magento\Catalog\Model\Product\Attribute\Backend\Tierprice.
1212
*
1313
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
14+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1415
*/
1516
class TierpriceTest extends \PHPUnit\Framework\TestCase
1617
{

0 commit comments

Comments
 (0)