0

I'm working on a custom import script and I want to check if the product I'm going to import has an attribute that does not exist in Magento, the script will create the attribute

use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
if ( $_POST['product'] && validate_key($_POST['key'] )) {

    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();    
    $state = $objectManager->get('Magento\Framework\App\State');
    $state->setAreaCode('frontend');
    $_product = $objectManager->create('Magento\Catalog\Model\Product');
    .
    .
    .
    $eav = $objectManager->create('\Magento\Eav\Model\Entity\Attribute');
    if ( !$eav->load($code)->getId()  ) {
        $eavFac = $objectManager->create("\Magento\Eav\Setup\EavSetupFactory");

        $eavFac->addAttribute( \Magento\Catalog\Model\Product::ENTITY, $code,
                        [
                            'group'                     => 'Product Details',/* Group name in which you want to display your custom attribute */
                            'type'                      => 'int',/* Data type in which formate your value save in database*/
                            'backend'                   => '',
                            'frontend'                  => '',
                            'label'                     => $t, /* lablel of your attribute*/
                            'input'                     => 'select',
                            'class'                     => '',
                            'source'                    => 'ModuleNameSpace\YourModuleName\Model\Config\Source\Options',
                            'global'                    => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,  /*Scope of your attribute */
                            'visible'                   => true,
                            'required'                  => false,
                            'user_defined'              => false,
                            'default'                   => '',
                            'searchable'                => true,
                            'filterable'                => true,
                            'comparable'                => false,
                            'visible_on_front'          => true,
                            'used_in_product_listing'   => true,
                            'unique'                    => false
                        ]
                    );
                    $all_attrs[] = $eav->load($code)->getId();

                }
}

its giving the error

Call to undefined method Magento\Eav\Setup\EavSetupFactory::addAttribute()

  • what the issue you are facing? – Dhiren Vasoya Oct 12 '17 at 13:13
  • Call to undefined method Magento\Eav\Setup\EavSetupFactory::addAttribute() – Hassan Nisar Khan Oct 12 '17 at 13:15
0

you need to replace your code.

$eavFac = $objectManager->create("\Magento\Eav\Setup\EavSetupFactory");
$eavFac->addAttribute( \Magento\Catalog\Model\Product::ENTITY, $code,

Replace with this

$setup = $objectManager->get('Magento\Framework\Setup\ModuleDataSetupInterface');
$eav_setup_factory = $objectManager->get('Magento\Eav\Setup\EavSetupFactory');

$setup->startSetup();
$eavSetup = $eav_setup_factory->create(['setup' => $setup]);

$eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $code,

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.