Skip to content

Commit 0a02873

Browse files
author
Viktor Kopin
committed
MC-37665: Updating a category through the REST API will uncheck "Use Default Value" on a bunch of attributes
1 parent e7f62cb commit 0a02873

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

app/code/Magento/Catalog/Model/CategoryRepository/PopulateWithValues.php

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
1616
use Magento\Framework\Api\FilterBuilder;
1717
use Magento\Framework\Api\SearchCriteriaBuilder;
18+
use Magento\Store\Model\Store;
1819

1920
/**
2021
* Add data to category entity and populate with default values
@@ -68,31 +69,34 @@ public function __construct(
6869
*/
6970
public function execute(CategoryInterface $category, array $existingData): void
7071
{
71-
$storeId = $existingData['store_id'];
72-
$overriddenValues = array_filter(
73-
$category->getData(),
74-
function ($key) use ($category, $storeId) {
75-
/** @var Category $category */
76-
return $this->scopeOverriddenValue->containsValue(
77-
CategoryInterface::class,
78-
$category,
79-
$key,
80-
$storeId
81-
);
82-
},
83-
ARRAY_FILTER_USE_KEY
84-
);
85-
$defaultValues = array_diff_key($category->getData(), $overriddenValues);
86-
array_walk(
87-
$defaultValues,
88-
function (&$value, $key) {
89-
$attributes = $this->getAttributes();
90-
if (isset($attributes[$key]) && !$attributes[$key]->isStatic()) {
91-
$value = null;
72+
$storeId = $existingData['store_id'] ?? Store::DEFAULT_STORE_ID;
73+
if ((int)$storeId !== Store::DEFAULT_STORE_ID) {
74+
$overriddenValues = array_filter(
75+
$category->getData(),
76+
function ($key) use ($category, $storeId) {
77+
/** @var Category $category */
78+
return $this->scopeOverriddenValue->containsValue(
79+
CategoryInterface::class,
80+
$category,
81+
$key,
82+
$storeId
83+
);
84+
},
85+
ARRAY_FILTER_USE_KEY
86+
);
87+
$defaultValues = array_diff_key($category->getData(), $overriddenValues);
88+
array_walk(
89+
$defaultValues,
90+
function (&$value, $key) {
91+
$attributes = $this->getAttributes();
92+
if (isset($attributes[$key]) && !$attributes[$key]->isStatic()) {
93+
$value = null;
94+
}
9295
}
93-
}
94-
);
95-
$category->addData($defaultValues);
96+
);
97+
$category->addData($defaultValues);
98+
}
99+
96100
$category->addData($existingData);
97101
$useDefaultAttributes = array_filter(
98102
$category->getData(),

0 commit comments

Comments
 (0)