3

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?

1

Create this file in your module: app/code/Vendor/Module/etc/db_schema.xml with the following content for ex:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="inventory_source_item" resource="default" engine="innodb">
        <column xsi:type="int" name="source_item_id" padding="10" unsigned="true" nullable="false" identity="true"/>
        <column xsi:type="varchar" name="source_code" nullable="false" length="255"/>
        <column xsi:type="varchar" name="sku" nullable="false" length="255"/>
        <column xsi:type="decimal" name="quantity" scale="4" precision="12" unsigned="false" nullable="false"
                default="0"/>
        <column xsi:type="smallint" name="status" padding="5" unsigned="true" nullable="false" identity="false"
                default="0"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="source_item_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="INVENTORY_SOURCE_ITEM_SOURCE_CODE_INVENTORY_SOURCE_SOURCE_CODE"
                    table="inventory_source_item" column="source_code" referenceTable="inventory_source"
                    referenceColumn="source_code" onDelete="CASCADE"/>
        <constraint xsi:type="unique" referenceId="INVENTORY_SOURCE_ITEM_SOURCE_CODE_SKU">
            <column name="source_code"/>
            <column name="sku"/>
        </constraint>
        <index referenceId="INVENTORY_SOURCE_ITEM_SKU_SOURCE_CODE_QUANTITY" indexType="btree">
            <column name="sku"/>
            <column name="source_code"/>
            <column name="quantity"/>
        </index>
    </table>
</schema>

and run s:up, you will see that sku will change to 255. Notice that this is the same definition as the original, except that I've changed the length of sku column. You can read more about it in https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/db-schema.html

  • Thank you so much. It's working.. :) – B. Desai Jun 1 '20 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.