The text attributes are stores in the table catalog_product_entity_text
. Their values are stores in the value
column that is of type text
.
You can see here the max lengths allowed for all column types.
This is what's important to you:
BLOB, TEXT L + 2 bytes, where L < 2^16 (64 Kilobytes)
You can also use long text for an attribute, but this is more complicated. You would have to create a table similar to catalog_product_entity_text
but with the column value
having the type LONGTEXT.
THe max storage for this is (from the same source)
LONGBLOB, LONGTEXT L + 4 bytes, where L < 2^32
But you will also have to search through the code to see where you need to tell magento to consider your new table. I have no idea what this will take.
I suggest using the TEXT attribute. It should be enough storage. If you plan to keep more than 64k in one column you might need to rethink your architecture.
I have use this script to add custom attribute in products.
It's working fine for me.
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'shop_by_brand',
[
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Shop by Brand',
'input' => 'textarea',
'class' => '',
'source' => '',
'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => 0,
'searchable' => false,
'filterable' => true,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'is_wysiwyg_enabled' => TRUE,
'unique' => false,
'apply_to' => ''
]
);
Best Answer
Overview of Adding Product Attribute Programmatically
InstallData.php
install()
methodStep 1: Create file
InstallData.php
We will start with the InstallData class which located in
The content for this file:
Step 2: Define the install() method
Step 3: Create custom attribute Here are all lines code of
InstallData.php
to create product attribute programmically.As you can see, all the addAttribute method requires is: The type id of the entity which we want to add attribute The name of the attribute An array of key value pairs to define the attribute such as group, input type, source, label…
All done, please run the upgrade script php bin/magento setup:upgrade to install the module and the product attribute sample_attribute will be created.
If you want to remove product attribute, you can use method removeAttribute instead of addAttribute. It will be like this:
EDIT:
for uninstall create the app/code/Mageplaza/HelloWorld/Setup/Uninstall.php.
Also you can follow below URL for creating custom product attribute.
URL : https://www.mageplaza.com/magento-2-module-development/magento-2-add-product-attribute-programmatically.html