Skip to content

Instantly share code, notes, and snippets.

@timneutkens
Created November 21, 2016 11:38
Show Gist options
  • Save timneutkens/287aaf85ffe8273a71ee6ddd0294404a to your computer and use it in GitHub Desktop.
Save timneutkens/287aaf85ffe8273a71ee6ddd0294404a to your computer and use it in GitHub Desktop.
Magento2 | Install custom category attribute
<?php
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
$setup->startSetup();
/** @var \Magento\Eav\Setup\EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Category::ENTITY);
$attributeSetId = $eavSetup->getDefaultAttributeSetId($entityTypeId);
/**
* Add attribute to the eav/attribute
*/
$eavSetup->addAttribute(
$entityTypeId,
'custom_attribute',
[
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Custom Attribute Label',
'input' => 'select',
'class' => '',
'source' => '',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
'visible' => false,
'required' => false,
'user_defined' => true,
'default' => 'none',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'unique' => false,
'apply_to' => 'category'
]
);
/**
* Add attribute to the 'General Information' attribute group
*/
$eavSetup->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$eavSetup->getAttributeGroupId($entityTypeId, $attributeSetId, 'general-information'),
'custom_attribute',
null
);
$setup->endSetup();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment