diff --git a/vendor/magento/module-catalog/Controller/Adminhtml/Product/Initialization/Helper/AttributeFilter.php b/vendor/magento/module-catalog/Controller/Adminhtml/Product/Initialization/Helper/AttributeFilter.php index 81ab67bdf26..8c2c742293f 100644 --- a/vendor/magento/module-catalog/Controller/Adminhtml/Product/Initialization/Helper/AttributeFilter.php +++ b/vendor/magento/module-catalog/Controller/Adminhtml/Product/Initialization/Helper/AttributeFilter.php @@ -8,7 +8,8 @@ declare(strict_types=1); namespace Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper; -use \Magento\Catalog\Model\Product; +use Magento\Catalog\Model\Product; +use Magento\Catalog\Model\ResourceModel\Eav\Attribute; /** * Class provides functionality to check and filter data came with product form. @@ -32,8 +33,9 @@ class AttributeFilter { $attributeList = $product->getAttributes(); foreach ($productData as $attributeCode => $attributeValue) { - if ($this->isAttributeShouldNotBeUpdated($product, $useDefaults, $attributeCode, $attributeValue)) { - unset($productData[$attributeCode]); + if ($this->isAttributeShouldNotBeUpdated($product, $useDefaults, $attributeCode, $attributeValue) && + $this->isCustomAttrEmptyValueAllowed($attributeList, $attributeCode, $productData)) { + unset($productData[$attributeCode]); } if (isset($useDefaults[$attributeCode]) && $useDefaults[$attributeCode] === '1') { @@ -63,6 +65,34 @@ class AttributeFilter return $productData; } + /** + * Check if custom attribute with empty value allowed + * + * @param mixed $attributeList + * @param string $attributeCode + * @param array $productData + * @return bool + */ + private function isCustomAttrEmptyValueAllowed( + $attributeList, + string $attributeCode, + array $productData + ): bool { + $isAllowed = true; + if ($attributeList && isset($attributeList[$attributeCode])) { + /** @var Attribute $attribute */ + $attribute = $attributeList[$attributeCode]; + $isAttributeUserDefined = (int) $attribute->getIsUserDefined(); + $isAttributeIsRequired = (int) $attribute->getIsRequired(); + + if ($isAttributeUserDefined && !$isAttributeIsRequired && + empty($productData[$attributeCode])) { + $isAllowed = false; + } + } + return $isAllowed; + } + /** * Prepare default attribute data for product. * @@ -74,7 +104,7 @@ class AttributeFilter private function prepareDefaultData(array $attributeList, string $attributeCode, array $productData): array { if (isset($attributeList[$attributeCode])) { - /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */ + /** @var Attribute $attribute */ $attribute = $attributeList[$attributeCode]; $attributeType = $attribute->getBackendType(); // For non-numeric types set the attributeValue to 'false' to trigger their removal from the db