Skip to content
This repository has been archived by the owner on Apr 29, 2019. It is now read-only.

SKU uniqueness validation #102

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 6 additions & 51 deletions app/code/Magento/Catalog/Model/Product/Attribute/Backend/Sku.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down