Skip to content

Instantly share code, notes, and snippets.

@timneutkens
Last active August 30, 2016 12:35
Show Gist options
  • Save timneutkens/7d4a970967f21fc36f950c620d979d6a to your computer and use it in GitHub Desktop.
Save timneutkens/7d4a970967f21fc36f950c620d979d6a to your computer and use it in GitHub Desktop.
Magento2 add product attribute programatically. Based on https://www.atwix.com/magento/adding-attribute-programatically-magento2/
<?php namespace TimNeutkens\InstallProductAttributes\Setup;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* {@inheritdoc}
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttribute(
Product::ENTITY,
'test_attribute',
[
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Extra information',
'input' => '',
'class' => '',
'source' => '',
'global' => Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => '',
'frontend_input' => 'textarea',
'is_html_allowed_on_front' => true,
'is_wysiwyg_enabled' => true,
]
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment