1

What is the right way to create a product attribute in a non install or upgrade scripts in Magento2?

Aasim Goriya
  • 5,444
  • 2
  • 28
  • 53
giani.sim
  • 1,473
  • 23
  • 54
  • Did you get any solution for this, I also need this feature in my custom script module but not getting any clue. if you have any alternate solution please let me know. – Jack Jan 24 '18 at 11:11

3 Answers3

0

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
                ]
            );      
        }
    }
}
Abhishek Panchal
  • 4,948
  • 3
  • 21
  • 38
  • that's the code for an upgrade script. I need to create attribute without passing by an upgrade script – giani.sim Aug 08 '17 at 14:55
  • instead of UpgradeData. you can use InstallData. Just follow below tutorial to get add new attribute programatically. https://www.atwix.com/magento/adding-attribute-programatically-magento2/ – Abhishek Panchal Aug 08 '17 at 15:01
  • I need to do it outside the install and upgrade scripts – giani.sim Aug 08 '17 at 15:02
0

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]
                ]
            );
}
?>
Jeeva Chezhiyan
  • 1,498
  • 3
  • 20
  • 39
-1

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);
Alex
  • 486
  • 7
  • 13