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