1

I use following code to add custom attribute in product magento 2

<?php

namespace Marketplace\Attribute\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;


class InstallData implements InstallDataInterface
{

private $eavSetupFactory;

public function __construct(EavSetupFactory $eavSetupFactory)
{
    $this->eavSetupFactory = $eavSetupFactory;
}


public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{

    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    $eavSetup->addAttribute(
      \Magento\Catalog\Model\Product::ENTITY,
      'storevendor_id',
      [
        'type' => 'int',
        'backend' => '',
        'frontend' => '',
        'label' => 'Sample Atrribute',
        'input' => '',
        'class' => '',
        'source' => '',
        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
        'visible' => true,
        'required' => false,
        'user_defined' => false,
        'default' => '',
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'visible_on_front' => false,
        'used_in_product_listing' => true,
        'unique' => false,
        'apply_to' => ''
      ]
    );
  }
}

but when after install it I got error when open detail product page in admin page

enter image description here

is there something wrong with my script ?

5
  • Please keep 'input' => 'text', – Nagaraju K Aug 11 '17 at 8:23
  • Please use this tool which is very easy and simple to create a custom attribute and it will help you lot prntscr.com/g74z8a – Nagaraju K Aug 11 '17 at 8:33
  • mage2gen.com – Nagaraju K Aug 11 '17 at 8:33
  • thank for reply @NagarajuKasa, I will try first this app – mrfizh Aug 11 '17 at 8:39
  • Please get with your comments and doubts so that i will assist you accordingly thanks – Nagaraju K Aug 11 '17 at 8:49
1

Please try with this code in local magento 2

<?php

namespace Namespace\ModuleName\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;

class InstallData implements InstallDataInterface
{

    private $eavSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'review_data',
            [
                'type' => 'varchar',
                'backend' => '',
                'frontend' => '',
                'label' => 'Review Data',
                'input' => 'text',
                'class' => '',
                'source' => '',
                'global' => 1,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => null,
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => true,
                'apply_to' => '',
                'system' => 1,
                'group' => 'General',
                'option' => array('values' => array(""))
            ]
        );
    }
}
3
  • thank you very much, it works. I just know if there is generator extension for magento2, your post answer is same with the output generate from link. So I deleted my old custom attribute and follow your script, now I'm not getting this error again.. :) – mrfizh Aug 11 '17 at 9:04
  • Glad to hear please raise upvote so that it will help you for other developers also thanks – Nagaraju K Aug 11 '17 at 9:20
  • Ok I will raise upvote tomorrow. Because today, my dailly voted has reached limit :D – mrfizh Aug 11 '17 at 9:24

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.