Skip to content

Instantly share code, notes, and snippets.

@techguysourav
Created July 12, 2016 04:08
Show Gist options
  • Save techguysourav/a3766539447ba9d6705501971bb6411e to your computer and use it in GitHub Desktop.
Save techguysourav/a3766539447ba9d6705501971bb6411e to your computer and use it in GitHub Desktop.
Magento2 : Adding custom attributes to categories
<?php
namespace Paarth\HelloWorld\Setup;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class UpgradeData implements UpgradeDataInterface
{
protected $resourceConfig , $categorySetupFactory ;
public function __construct
(\Magento\Config\Model\ResourceModel\Config $resourceConfig,
\Magento\Catalog\Setup\CategorySetupFactory $categorySetupFactory
) {
$this->resourceConfig = $resourceConfig;
$this->categorySetupFactory = $categorySetupFactory;
}
public function upgrade(ModuleDataSetupInterface $setup,
ModuleContextInterface $context) {
if (version_compare($context->getVersion(), '2.0.4') < 0) {
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
$entityTypeId = $categorySetup->getEntityTypeId
(\Magento\Catalog\Model\Category::ENTITY);
$categorySetup->addAttribute($entityTypeId,
'custom_text', array(
'type' => 'varchar',
'label' => 'HeloWorld label',
'input' => 'text',
'required' => false,
'group' => 'HelloWorld'
));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment