From 67d5501c3645e560b9aad55745888a5c8ca736db Mon Sep 17 00:00:00 2001 From: Federico Rivollier Date: Wed, 28 Feb 2018 00:54:06 -0300 Subject: [PATCH 1/2] WIP: Removed logic for generating unique SKU when trying to save an existing one --- .../Model/Product/Attribute/Backend/Sku.php | 57 ++----------------- 1 file changed, 6 insertions(+), 51 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Product/Attribute/Backend/Sku.php b/app/code/Magento/Catalog/Model/Product/Attribute/Backend/Sku.php index a652d0ef902..04ced8d5e27 100644 --- a/app/code/Magento/Catalog/Model/Product/Attribute/Backend/Sku.php +++ b/app/code/Magento/Catalog/Model/Product/Attribute/Backend/Sku.php @@ -60,32 +60,16 @@ public function validate($object) __('SKU length should be %1 characters maximum.', self::SKU_MAX_LENGTH) ); } - return true; - } - /** - * Generate and set unique SKU to product - * - * @param Product $object - * @return void - */ - protected function _generateUniqueSku($object) - { $attribute = $this->getAttribute(); $entity = $attribute->getEntity(); - $attributeValue = $object->getData($attribute->getAttributeCode()); - $increment = null; - while (!$entity->checkAttributeUniqueValue($attribute, $object)) { - if ($increment === null) { - $increment = $this->_getLastSimilarAttributeValueIncrement($attribute, $object); - } - $sku = trim($attributeValue); - if (strlen($sku . '-' . ++$increment) > self::SKU_MAX_LENGTH) { - $sku = substr($sku, 0, -strlen($increment) - 1); - } - $sku = $sku . '-' . $increment; - $object->setData($attribute->getAttributeCode(), $sku); + if (!$entity->checkAttributeUniqueValue($attribute, $object)) { + throw new \Magento\Framework\Exception\LocalizedException( + __('SKU is already in use.') + ); } + + return true; } /** @@ -96,35 +80,6 @@ protected function _generateUniqueSku($object) */ public function beforeSave($object) { - $this->_generateUniqueSku($object); return parent::beforeSave($object); } - - /** - * Return increment needed for SKU uniqueness - * - * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute - * @param Product $object - * @return int - */ - protected function _getLastSimilarAttributeValueIncrement($attribute, $object) - { - $connection = $this->getAttribute()->getEntity()->getConnection(); - $select = $connection->select(); - $value = $object->getData($attribute->getAttributeCode()); - $bind = ['attribute_code' => trim($value) . '-%']; - - $select->from( - $this->getTable(), - $attribute->getAttributeCode() - )->where( - $attribute->getAttributeCode() . ' LIKE :attribute_code' - )->order( - ['entity_id DESC', $attribute->getAttributeCode() . ' ASC'] - )->limit( - 1 - ); - $data = $connection->fetchOne($select, $bind); - return abs((int)str_replace($value, '', $data)); - } } From 3d21225751751f37dc5c987986a3c7f7aca1bb39 Mon Sep 17 00:00:00 2001 From: David Manners Date: Wed, 30 May 2018 10:37:16 +0000 Subject: [PATCH 2/2] magento-engcom/import-export-improvements#101: update functional test to expect the newer exception message --- .../Test/Constraint/AssertProductUrlDuplicateErrorMessage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/functional/tests/app/Magento/CatalogUrlRewrite/Test/Constraint/AssertProductUrlDuplicateErrorMessage.php b/dev/tests/functional/tests/app/Magento/CatalogUrlRewrite/Test/Constraint/AssertProductUrlDuplicateErrorMessage.php index 0741a226cea..7d18c0f994d 100644 --- a/dev/tests/functional/tests/app/Magento/CatalogUrlRewrite/Test/Constraint/AssertProductUrlDuplicateErrorMessage.php +++ b/dev/tests/functional/tests/app/Magento/CatalogUrlRewrite/Test/Constraint/AssertProductUrlDuplicateErrorMessage.php @@ -19,7 +19,7 @@ class AssertProductUrlDuplicateErrorMessage extends AbstractConstraint /** * Text title of the error message to be checked. */ - const ERROR_MESSAGE_TITLE = 'The value specified in the URL Key field would generate a URL that already exists.'; + const ERROR_MESSAGE_TITLE = 'SKU is already in use.'; /** * Assert that success message is displayed after product save.