Skip to content

Commit 2437059

Browse files
committed
ACP2E-465: Bundle product special price is not updated via REST API at store view scope
1 parent 144edf6 commit 2437059

File tree

2 files changed

+64
-16
lines changed

2 files changed

+64
-16
lines changed

app/code/Magento/Catalog/Model/Attribute/ScopeOverriddenValue.php

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Catalog\Model\Attribute;
88

9+
use Magento\Catalog\Model\AbstractModel;
10+
use Magento\Framework\DataObject;
911
use Magento\Framework\EntityManager\MetadataPool;
1012
use Magento\Eav\Api\AttributeRepositoryInterface as AttributeRepository;
1113
use Magento\Framework\Api\SearchCriteriaBuilder;
@@ -75,7 +77,7 @@ public function __construct(
7577
* Whether attribute value is overridden in specific store
7678
*
7779
* @param string $entityType
78-
* @param \Magento\Catalog\Model\AbstractModel $entity
80+
* @param AbstractModel $entity
7981
* @param string $attributeCode
8082
* @param int|string $storeId
8183
* @return bool
@@ -85,39 +87,41 @@ public function containsValue($entityType, $entity, $attributeCode, $storeId)
8587
if ((int)$storeId === Store::DEFAULT_STORE_ID) {
8688
return false;
8789
}
88-
if (!isset($this->attributesValues[$storeId])) {
90+
$values = $this->getAttributesValues($entityType, $entity);
91+
92+
if (!isset($values[$storeId])) {
8993
$this->initAttributeValues($entityType, $entity, (int)$storeId);
94+
$values = $this->getAttributesValues($entityType, $entity);
9095
}
9196

92-
return isset($this->attributesValues[$storeId])
93-
&& array_key_exists($attributeCode, $this->attributesValues[$storeId]);
97+
return isset($values[$storeId]) && array_key_exists($attributeCode, $values[$storeId]);
9498
}
9599

96100
/**
97101
* Get attribute default values
98102
*
99103
* @param string $entityType
100-
* @param \Magento\Catalog\Model\AbstractModel $entity
104+
* @param AbstractModel $entity
101105
* @return array
102106
*
103107
* @deprecated 101.0.0
104108
*/
105109
public function getDefaultValues($entityType, $entity)
106110
{
107-
if ($this->attributesValues === null) {
111+
$values = $this->getAttributesValues($entityType, $entity);
112+
if (!isset($values[Store::DEFAULT_STORE_ID])) {
108113
$this->initAttributeValues($entityType, $entity, (int)$entity->getStoreId());
114+
$values = $this->getAttributesValues($entityType, $entity);
109115
}
110116

111-
return isset($this->attributesValues[Store::DEFAULT_STORE_ID])
112-
? $this->attributesValues[Store::DEFAULT_STORE_ID]
113-
: [];
117+
return $values[Store::DEFAULT_STORE_ID] ?? [];
114118
}
115119

116120
/**
117121
* Init attribute values.
118122
*
119123
* @param string $entityType
120-
* @param \Magento\Catalog\Model\AbstractModel $entity
124+
* @param AbstractModel $entity
121125
* @param int $storeId
122126
* @throws \Magento\Framework\Exception\LocalizedException
123127
* @return void
@@ -128,6 +132,7 @@ private function initAttributeValues($entityType, $entity, $storeId)
128132
/** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */
129133
$attributeTables = [];
130134
if ($metadata->getEavEntityType()) {
135+
$entityId = $entity->getData($metadata->getLinkField());
131136
foreach ($this->getAttributes($entityType) as $attribute) {
132137
if (!$attribute->isStatic()) {
133138
$attributeTables[$attribute->getBackend()->getTable()][] = $attribute->getAttributeId();
@@ -146,7 +151,7 @@ private function initAttributeValues($entityType, $entity, $storeId)
146151
'a.attribute_id = t.attribute_id',
147152
['attribute_code' => 'a.attribute_code']
148153
)
149-
->where($metadata->getLinkField() . ' = ?', $entity->getData($metadata->getLinkField()))
154+
->where($metadata->getLinkField() . ' = ?', $entityId)
150155
->where('t.attribute_id IN (?)', $attributeCodes)
151156
->where('t.store_id IN (?)', $storeIds);
152157
$selects[] = $select;
@@ -157,9 +162,14 @@ private function initAttributeValues($entityType, $entity, $storeId)
157162
\Magento\Framework\DB\Select::SQL_UNION_ALL
158163
);
159164
$attributes = $metadata->getEntityConnection()->fetchAll((string)$unionSelect);
165+
$values = array_merge(
166+
$this->getAttributesValues($entityType, $entity),
167+
array_fill_keys($storeIds, [])
168+
);
160169
foreach ($attributes as $attribute) {
161-
$this->attributesValues[$attribute['store_id']][$attribute['attribute_code']] = $attribute['value'];
170+
$values[$attribute['store_id']][$attribute['attribute_code']] = $attribute['value'];
162171
}
172+
$this->setAttributesValues($entityType, $entity, $values);
163173
}
164174
}
165175

@@ -188,12 +198,50 @@ private function getAttributes($entityType)
188198
}
189199

190200
/**
191-
* Clear attributes values cache
201+
* Clear entity attributes values cache
202+
*
203+
* @param string $entityType
204+
* @param DataObject $entity
205+
* @return void
206+
* @throws \Exception
207+
*/
208+
public function clearAttributesValues(string $entityType, DataObject $entity): void
209+
{
210+
if (isset($this->attributesValues[$entityType])) {
211+
$metadata = $this->metadataPool->getMetadata($entityType);
212+
$entityId = $entity->getData($metadata->getLinkField());
213+
unset($this->attributesValues[$entityType][$entityId]);
214+
}
215+
}
216+
217+
/**
218+
* Get entity attributes values from cache
192219
*
220+
* @param string $entityType
221+
* @param DataObject $entity
222+
* @return array
223+
* @throws \Exception
224+
*/
225+
private function getAttributesValues(string $entityType, DataObject $entity): array
226+
{
227+
$metadata = $this->metadataPool->getMetadata($entityType);
228+
$entityId = $entity->getData($metadata->getLinkField());
229+
return $this->attributesValues[$entityType][$entityId] ?? [];
230+
}
231+
232+
/**
233+
* Set entity attributes values into cache
234+
*
235+
* @param string $entityType
236+
* @param DataObject $entity
237+
* @param array $values
193238
* @return void
239+
* @throws \Exception
194240
*/
195-
public function clearAttributeValues(): void
241+
private function setAttributesValues(string $entityType, DataObject $entity, array $values): void
196242
{
197-
$this->attributesValues = null;
243+
$metadata = $this->metadataPool->getMetadata($entityType);
244+
$entityId = $entity->getData($metadata->getLinkField());
245+
$this->attributesValues[$entityType][$entityId] = $values;
198246
}
199247
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ protected function _afterSave(DataObject $product)
323323
{
324324
$this->removeNotInSetAttributeValues($product);
325325
$this->_saveWebsiteIds($product)->_saveCategories($product);
326-
$this->scopeOverriddenValue->clearAttributeValues();
326+
$this->scopeOverriddenValue->clearAttributesValues(ProductInterface::class, $product);
327327
return parent::_afterSave($product);
328328
}
329329

0 commit comments

Comments
 (0)