4

How can we add custom attribute using setup script in magento 2

attribute type should be textarea with editor.

Like Product description.

1
  • Can refer vendor\magento\module-cms\Setup\InstallSchema.php
    – Jackson
    Commented Oct 6, 2016 at 8:38

4 Answers 4

11

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' => ''
        ]
    );
1
  • backend appears twice, and second time with ArrayBackend
    – Igosyee
    Commented Jan 23, 2023 at 13:31
2

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' => ''
]
);
1
  • wysiwyg_enabled appears twice in your array
    – DWils
    Commented Aug 6, 2020 at 15:42
0

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' => ''
    ]
);
0

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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