4

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

attribute type should be textarea with editor.

Like Product description.

  • Can refer vendor\magento\module-cms\Setup\InstallSchema.php – Ankit Shah Oct 6 '16 at 8:38
8

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

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.