Skip to content

Commit 8625852

Browse files
committed
ACP2E-2672: [Cloud] Special price API endpoint returns error when updating large numbers of products concurrently
- Fix PAT build
1 parent ceaa83e commit 8625852

File tree

2 files changed

+61
-45
lines changed

2 files changed

+61
-45
lines changed

app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,40 @@ protected function processNewAndExistingImages($product, array &$images)
326326

327327
$data[$this->metadata->getLinkField()] = (int)$product->getData($this->metadata->getLinkField());
328328

329-
if ($isNew || $this->hasGalleryStoreValueChanged($data, $existingGalleryStoreValues)) {
330-
$this->saveGalleryStoreValue($product, $data, $isNew);
329+
if (!$isNew) {
330+
$data += (array) $this->getExistingGalleryStoreValue(
331+
$existingGalleryStoreValues,
332+
$data['value_id'],
333+
$data['store_id']
334+
);
331335
}
336+
337+
$this->saveGalleryStoreValue($data, $isNew);
332338
}
333339
}
334340
}
335341

342+
/**
343+
* Returns existing gallery store value by value id and store id
344+
*
345+
* @param array $existingGalleryStoreValues
346+
* @param int $valueId
347+
* @param int $storeId
348+
* @return array|null
349+
*/
350+
private function getExistingGalleryStoreValue(array $existingGalleryStoreValues, int $valueId, int $storeId): ?array
351+
{
352+
foreach ($existingGalleryStoreValues as $existingGalleryStoreValue) {
353+
if (((int) $existingGalleryStoreValue['value_id']) === $valueId
354+
&& ((int) $existingGalleryStoreValue['store_id']) === $storeId
355+
) {
356+
return $existingGalleryStoreValue;
357+
}
358+
}
359+
360+
return null;
361+
}
362+
336363
/**
337364
* Get existing gallery store values
338365
*
@@ -345,50 +372,18 @@ private function getExistingGalleryStoreValues(Product $product): array
345372
$existingMediaGalleryValues = [];
346373
if (!$product->isObjectNew()) {
347374
$productId = (int)$product->getData($this->metadata->getLinkField());
348-
foreach ($this->mediaGalleryValue->getAllByEntityId($productId) as $data) {
349-
$existingMediaGalleryValues[] = [
350-
'value_id' => (int) $data['value_id'],
351-
'store_id' => (int) $data['store_id'],
352-
'label' => $data['label'] ?: null,
353-
'position' => $data['position'] !== null ? (int)$data['position'] : null,
354-
'disabled' => (int) $data['disabled'],
355-
];
356-
}
375+
$existingMediaGalleryValues = $this->mediaGalleryValue->getAllByEntityId($productId);
357376
}
358377
return $existingMediaGalleryValues;
359378
}
360379

361-
/**
362-
* Check if gallery store value has changed
363-
*
364-
* @param array $data
365-
* @param array $existingGalleryStoreValues
366-
* @return bool
367-
*/
368-
private function hasGalleryStoreValueChanged(array $data, array $existingGalleryStoreValues): bool
369-
{
370-
foreach ($existingGalleryStoreValues as $existingGalleryStoreValue) {
371-
if ($existingGalleryStoreValue['value_id'] === $data['value_id']
372-
&& $existingGalleryStoreValue['store_id'] === $data['store_id']
373-
&& $existingGalleryStoreValue['label'] === $data['label']
374-
&& $existingGalleryStoreValue['position'] === $data['position']
375-
&& $existingGalleryStoreValue['disabled'] === $data['disabled']
376-
) {
377-
return false;
378-
}
379-
}
380-
381-
return true;
382-
}
383-
384380
/**
385381
* Save media gallery store value
386382
*
387-
* @param Product $product
388383
* @param array $data
389384
* @param bool $isNewImage
390385
*/
391-
private function saveGalleryStoreValue(Product $product, array $data, bool $isNewImage): void
386+
private function saveGalleryStoreValue(array $data, bool $isNewImage): void
392387
{
393388
$items = [];
394389
$items[] = $data;
@@ -401,14 +396,7 @@ private function saveGalleryStoreValue(Product $product, array $data, bool $isNe
401396
}
402397

403398
foreach ($items as $item) {
404-
if (!$product->isObjectNew()) {
405-
$this->resourceModel->deleteGalleryValueInStore(
406-
$item['value_id'],
407-
$item[$this->metadata->getLinkField()],
408-
$item['store_id']
409-
);
410-
}
411-
$this->resourceModel->insertGalleryValueInStore($item);
399+
$this->mediaGalleryValue->saveGalleryStoreValue($item);
412400
}
413401
}
414402

app/code/Magento/Catalog/Model/ResourceModel/Product/MediaGalleryValue.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,37 @@ public function getAllByEntityId(int $entityId): array
4545
$metadata = $this->metadataPool->getMetadata(ProductInterface::class);
4646
$connection = $this->galleryResource->getConnection();
4747
$select = $connection->select()
48-
->from(Gallery::GALLERY_VALUE_TABLE)
48+
->from($this->galleryResource->getTable(Gallery::GALLERY_VALUE_TABLE))
4949
->where($metadata->getLinkField() . ' = ?', $entityId);
5050

5151
return $connection->fetchAll($select);
5252
}
53+
54+
/**
55+
* Create or update media gallery value record.
56+
*
57+
* @param array $data
58+
* @return void
59+
* @throws \Exception
60+
*/
61+
public function saveGalleryStoreValue(array $data): void
62+
{
63+
$metadata = $this->metadataPool->getMetadata(ProductInterface::class);
64+
$connection = $this->galleryResource->getConnection();
65+
$fields = $connection->describeTable($this->galleryResource->getTable(Gallery::GALLERY_VALUE_TABLE));
66+
if (isset($data['record_id'])) {
67+
$id = (int) $data['record_id'];
68+
$data = array_intersect_key($data, $fields);
69+
unset($data['record_id'], $data['value_id'], $data['store_id'], $data[$metadata->getLinkField()]);
70+
$connection->update(
71+
$this->galleryResource->getTable(Gallery::GALLERY_VALUE_TABLE),
72+
$data,
73+
[
74+
'record_id = ?' => $id,
75+
]
76+
);
77+
} else {
78+
$this->galleryResource->insertGalleryValueInStore($data);
79+
}
80+
}
5381
}

0 commit comments

Comments
 (0)