1

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

1
  • 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. Commented Jan 24, 2018 at 11:11

3 Answers 3

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
                ]
            );      
        }
    }
}
3
  • 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
  • I need to do it outside the install and upgrade scripts Commented Aug 8, 2017 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]
                ]
            );
}
?>
-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);
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.