I have one of my Magento 2 projects running with version 2.3.3. There are so many simple products for one single configurable product and as a result, there is an issue with the length of the SKU. To solve It, I have taken all possible steps.
1) Override file : Magento\Catalog\Model\Product\Attribute\Backend\Sku and change const SKU_MAX_LENGTH = 255;
Check below code for this
<?php
namespace Custom\Skusize\Model\Product\Attribute\Backend;
class Sku extends \Magento\Catalog\Model\Product\Attribute\Backend\Sku
{
/**
* Maximum SKU string length
*
* @var string
*/
const SKU_MAX_LENGTH = 255;
/**
* Validate SKU
*
* @param Product $object
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function validate($object)
{
$attrCode = $this->getAttribute()->getAttributeCode();
$value = $object->getData($attrCode);
if ($this->getAttribute()->getIsRequired() && strlen($value) === 0) {
throw new \Magento\Framework\Exception\LocalizedException(
__('The "%1" attribute value is empty. Set the attribute and try again.', $attrCode)
);
}
if ($this->string->strlen($object->getSku()) > self::SKU_MAX_LENGTH) {
throw new \Magento\Framework\Exception\LocalizedException(
__('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);
}
}
2) Change eav_attribute
table , find sku in attribute_code
and change frontend_class
3) Change different tables column's length to 255 containing sku
like catalog_product_entity
, inventory_low_stock_notification_configuration
, inventory_reservation
, inventory_source_item
The above 2 steps are working fine. But for the last steps whenever I run "setup:upgrade", it reverts to SKU with 64 length (For all tables)
So that I have created Installation schema also for same
namespace Custom\Skusize\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class InstallSchema implements InstallSchemaInterface
{
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$setup->getConnection()->changeColumn(
$setup->getTable('catalog_product_entity'),
'sku',
'sku',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 225
]
);
$setup->getConnection()->changeColumn(
$setup->getTable('inventory_low_stock_notification_configuration'),
'sku',
'sku',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 225
]
);
$setup->getConnection()->changeColumn(
$setup->getTable('inventory_reservation'),
'sku',
'sku',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 225
]
);
$setup->getConnection()->changeColumn(
$setup->getTable('inventory_source_item'),
'sku',
'sku',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 225
]
);
}
}
After this when I run "setup:upgrade" first time, it’s working fine and above all, SKU column remain with 255 length
But if I execute the same command again then it’s again reverted to 64
I also tried a schema patch
<?php
/**
* Copyright © 2019 Magenest. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Custom\Skusize\Setup\Patch\Schema;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\Patch\SchemaPatchInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class ChangeColumn implements SchemaPatchInterface
{
private $moduleDataSetup;
public function __construct(
ModuleDataSetupInterface $moduleDataSetup
) {
$this->moduleDataSetup = $moduleDataSetup;
}
public static function getDependencies()
{
return [];
}
public function getAliases()
{
return [];
}
public function apply()
{
$this->moduleDataSetup->startSetup();
$this->moduleDataSetup->getConnection()->changeColumn(
$this->moduleDataSetup->getTable('catalog_product_entity'),
'sku',
'sku',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 225,
'comment' => 'Product sku lenght is 225'
]
);
$this->moduleDataSetup->getConnection()->changeColumn(
$this->moduleDataSetup->getTable('inventory_low_stock_notification_configuration'),
'sku',
'sku',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 225,
'comment' => 'Product sku lenght is 225'
]
);
$this->moduleDataSetup->getConnection()->changeColumn(
$this->moduleDataSetup->getTable('inventory_reservation'),
'sku',
'sku',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 225,
'comment' => 'Product sku lenght is 225'
]
);
$this->moduleDataSetup->getConnection()->changeColumn(
$this->moduleDataSetup->getTable('inventory_source_item'),
'sku',
'sku',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 225,
'comment' => 'Product sku lenght is 225'
]
);
$this->moduleDataSetup->endSetup();
}
}
Still I have the same issue. How can I achieve this please help?