How can we add custom attribute using setup script in magento 2
attribute type should be textarea with editor.
Like Product description.
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' => ''
]
);
It's working fine for me.
$eavSetup->addAttribute(
Product::ENTITY,
'custom_attribute',
[
'group' => 'Product Details',
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Custom Attribute',
'input' => 'textarea',
'class' => '',
'source' => '',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'wysiwyg_enabled' => true,
'is_html_allowed_on_front' => true,
'visible' => true,
'required' => true,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'wysiwyg_enabled' => true,
'unique' => false,
'apply_to' => ''
]
);
This works for me
$eavSetup = $this->_eavSetupFactory->create(['setup' => $setup]);
$eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY,'size_n_fit');
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'size_n_fit',
[
'group' => 'Custom Attribute',
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Size And Fit',
'input' => 'textarea',
'class' => '',
'source' => '',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => true,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'wysiwyg_enabled' => true,
'unique' => false,
'apply_to' => ''
]
);
the option is_wysiwyg_enabled is setted in table catalog_eav_attribute, so you can create model Magento\Catalog\Model\ResourceModel\Eav\Attribute and effect is_wysiwyg_enabled as 1
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavResult = $eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
self::ATTRIBUTE_CODE,
[
'type' => 'text',
'frontend' => '',
'label' => 'More Description',
'input' => 'textarea',
'class' => '',
'source' => '',
'backend' => '',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
'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' => '',
'group' => 'content'
]
);
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $catalogAttributeModel */
$catalogAttributeModel = $this->attributeFactory->create();
$catalogAttributeModel->load($eavResult->getAttributeId(\Magento\Catalog\Model\Product::ENTITY,self::ATTRIBUTE_CODE));
$catalogAttributeModel->setData("is_wysiwyg_enabled",1)->save();
vendor\magento\module-cms\Setup\InstallSchema.php