Skip to content

Commit 5f7f950

Browse files
committed
MC-40054: Tier Prices get removed when the schedule update is created for a specific website
1 parent 63434d2 commit 5f7f950

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ public function execute($entity, $arguments = [])
8686
$identifierField = $this->metadataPoll->getMetadata(ProductInterface::class)->getLinkField();
8787
$priceRows = array_filter($priceRows);
8888
$productId = (int) $entity->getData($identifierField);
89+
$pricesStored = $this->getPricesStored($priceRows);
90+
$pricesMerged = $this->mergePrices($priceRows, $pricesStored);
8991

9092
// prepare and save data
91-
foreach ($priceRows as $data) {
93+
foreach ($pricesMerged as $data) {
9294
$isPriceWebsiteGlobal = (int)$data['website_id'] === 0;
9395
if ($isGlobal === $isPriceWebsiteGlobal
9496
|| !empty($data['price_qty'])
@@ -109,4 +111,51 @@ public function execute($entity, $arguments = [])
109111

110112
return $entity;
111113
}
114+
115+
/**
116+
* Merge prices
117+
*
118+
* @param array $prices
119+
* @param array $pricesStored
120+
* @return array
121+
*/
122+
private function mergePrices(array $prices, array $pricesStored): array
123+
{
124+
if (!$pricesStored) {
125+
return $prices;
126+
}
127+
$pricesId = [];
128+
$pricesStoredId = [];
129+
foreach ($prices as $price) {
130+
if (isset($price['price_id'])) {
131+
$pricesId[$price['price_id']] = $price;
132+
}
133+
}
134+
foreach ($pricesStored as $price) {
135+
if (isset($price['price_id'])) {
136+
$pricesStoredId[$price['price_id']] = $price;
137+
}
138+
}
139+
$pricesAdd = array_diff_key($pricesStoredId, $pricesId);
140+
foreach ($pricesAdd as $price) {
141+
$prices[] = $price;
142+
}
143+
return $prices;
144+
}
145+
146+
/**
147+
* Get stored prices
148+
*
149+
* @param array $prices
150+
* @return array
151+
*/
152+
private function getPricesStored(array $prices): array
153+
{
154+
$pricesStored = [];
155+
$price = reset($prices);
156+
if (isset($price['product_id']) && $price['product_id']) {
157+
$pricesStored = $this->tierPriceResource->loadPriceData($price['product_id']);
158+
}
159+
return $pricesStored;
160+
}
112161
}

app/code/Magento/Catalog/Model/ResourceModel/Product/Attribute/Backend/Tierprice.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected function _loadPriceDataColumns($columns)
3535
$columns = parent::_loadPriceDataColumns($columns);
3636
$columns['price_qty'] = 'qty';
3737
$columns['percentage_value'] = 'percentage_value';
38+
$columns['product_id'] = $this->getProductIdFieldName();
3839
return $columns;
3940
}
4041

0 commit comments

Comments
 (0)