What is the right way to create a product attribute in a non install or upgrade scripts in Magento2?
3 Answers
For Product Attribute
<?php
namespace Namespace\Modulename\Setup;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
class UpgradeData implements UpgradeDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), '1.0.1') < 0) {
$eavSetup= $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->removeAttribute(ProductAttributeInterface::ENTITY_TYPE_CODE,'product_tags');
$eavSetup->addAttribute(
ProductAttributeInterface::ENTITY_TYPE_CODE,
'product_tags',
[
'group' => 'Product Details',
'type' => 'varchar',
'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
'frontend' => '',
'label' => 'Product Tags',
'input' => 'multiselect',
'class' => '',
'source' => 'Namespace\Module\Model\Config\Source\Options',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => 1,
'required' => false,
'user_defined' => 1,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => false,
'unique' => false
]
);
}
}
}
-
that's the code for an upgrade script. I need to create attribute without passing by an upgrade script Commented Aug 8, 2017 at 14:55
-
instead of UpgradeData. you can use InstallData. Just follow below tutorial to get add new attribute programatically. atwix.com/magento/adding-attribute-programatically-magento2 Commented Aug 8, 2017 at 15:01
-
Please check the below script for creating a product multiple product attribute from root file.
<?php
use Magento\Framework\App\Bootstrap;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$storeManager = $obj->get('\Magento\Store\Model\StoreManagerInterface');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$eavSetup = $objectManager->get('Magento\Eav\Setup\EavSetupFactory');
$setup = $objectManager->get('Magento\Framework\Setup\ModuleDataSetupInterface');
$eavSetup1 = $eavSetup->create(['setup' => $setup]);
$attribute = array('Firstattributename','Secondattributename','Thirdattributename','Fourthattributename');
$variable = array(array('Dropdown1','Dropdown12'),array('Dropdown1','Dropdown12'),array('Dropdown1','Dropdown12'),array('Dropdown1','Dropdown12'));
foreach ($variable as $key=>$value) {
$eavSetup1->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
$attribute[$key],
[
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => $attribute[$key],
'input' => 'select',
'class' => '',
'source' => '',
'global' => 0,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => null,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => false,
'unique' => false,
'apply_to' => '',
'system' => 1,
'group' => 'General',
'option' => ['values' => $value]
]
);
}
?>
can try following code, in $attributeData - set your settings for new attribute
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$attributeData = array(
'attribute_code' => $attributeCode,
'is_global' => 1,
'frontend_label' => $attributeName,
'frontend_input' => 'select',
'default_value_text' => '',
'default_value_yesno' => 0,
'default_value_date' => '',
'default_value_textarea' => '',
'is_unique' => 0,
'apply_to' => 0,
'is_required' => 1,
'is_configurable' => 1,
'is_searchable' => 0,
'is_comparable' => 1,
'is_visible_in_advanced_search' => 1,
'is_used_for_price_rules' => 0,
'is_wysiwyg_enabled' => 0,
'is_html_allowed_on_front' => 1,
'is_visible_on_front' => 1,
'used_in_product_listing' => 1,
'used_for_sort_by' => 0,
'is_filterable' => 1,
'is_filterable_in_search' => 0,
'backend_type' => 'int',
'option' => array(),
'default' => array()
);
$attributeInterface = $objectManager->create('\Magento\Catalog\Api\Data\ProductAttributeInterface');
$attributeInterface->setData($attributeData);
$attribute = $objectManager->create('\Magento\Catalog\Model\Product\Attribute\Repository');
$savedAttribute = $attribute->save($attributeInterface);
-
can check webkul.com/blog/create-configurable-attribute-magento2 also good example– AlexCommented Jun 8, 2018 at 21:19
-
avoid direct use of object manager - magento.stackexchange.com/questions/117098/… Commented Jul 20, 2018 at 16:21
-