I am workign on custom extension for magento 2 and i need to create category attribute programmatically,
List me exact steps, because i am not sure is the code to be placed in InstallData.php or where?
category-attributemagento2
I am workign on custom extension for magento 2 and i need to create category attribute programmatically,
List me exact steps, because i am not sure is the code to be placed in InstallData.php or where?
Magento2.1, all attribute load via UI_Component. So you need to assign as a form element following way:
Create Vendor/Module/view/adminhtml/ui_component/category_form.xml
<?xml version="1.0" encoding="UTF-8"?> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> <fieldset name="display_settings"> <field name="customattribute"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="class" xsi:type="string">Magento\Catalog\Ui\Component\Category\Form\Element\Wysiwyg</item> <item name="formElement" xsi:type="string">wysiwyg</item> <item name="wysiwygConfigData" xsi:type="array"> <item name="settings" xsi:type="array"> <item name="theme_advanced_buttons1" xsi:type="string">bold,italic,|,justifyleft,justifycenter,justifyright,|,fontselect,fontsizeselect,|,forecolor,backcolor,|,link,unlink,image,|,bullist,numlist,|,code</item> <item name="theme_advanced_buttons2" xsi:type="boolean">false</item> <item name="theme_advanced_buttons3" xsi:type="boolean">false</item> <item name="theme_advanced_buttons4" xsi:type="boolean">false</item> <item name="theme_advanced_statusbar_location" xsi:type="boolean">false</item> </item> <item name="files_browser_window_url" xsi:type="boolean">false</item> <item name="height" xsi:type="string">100px</item> <item name="toggle_button" xsi:type="boolean">false</item> <item name="add_variables" xsi:type="boolean">false</item> <item name="add_widgets" xsi:type="boolean">false</item> <item name="add_images" xsi:type="boolean">false</item> </item> <item name="template" xsi:type="string">ui/form/field</item> <item name="source" xsi:type="string">category</item> <item name="wysiwyg" xsi:type="boolean">true</item> <item name="dataScope" xsi:type="string">customattribute</item> <item name="sortOrder" xsi:type="number">50</item> <item name="rows" xsi:type="number">8</item> <item name="label" xsi:type="string" translate="true">Custom Group</item> </item> </argument> </field> </fieldset> </form>
Please try below code:
app/code/Belong/Categoryattrs/Setup/InstallData.php
<?php
namespace Belong\Categoryattrs\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Setup\CategorySetupFactory;
class InstallData
implements InstallDataInterface
{
/**
* Category setup factory
*
* @var CategorySetupFactory
*/
private $categorySetupFactory;
/**
* Init
*
* @param CategorySetupFactory $categorySetupFactory
*/
public function __construct(
CategorySetupFactory $categorySetupFactory
) {
$this->categorySetupFactory = $categorySetupFactory;
}
/**
* {@inheritdoc}
*/
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$setup->startSetup();
/** @var \Magento\Catalog\Setup\CategorySetup $categorySetup */
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
$categorySetup->addAttribute(
'catalog_category',
'alfasi_category_id',
[
'type' => 'varchar',
'label' => 'Alfasi Category Id',
'input' => 'text',
'required' => false,
'sort_order' => 100,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'group' => 'General Information',
]
);
$setup->endSetup();
}
}
app/code/Belong/Categoryattrs/view/adminhtml/ui_component/category_form.xml
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="alfasi_category_id">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Additional Tab</item>
<item name="collapsible" xsi:type="boolean">true</item>
<item name="sortOrder" xsi:type="string">1000</item>
</item>
</argument>
<field name="alfasi_category_id">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">string</item>
<item name="source" xsi:type="string">category</item>
<item name="label" xsi:type="string" translate="true">Alfasi Category Id</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="required" xsi:type="boolean">false</item>
<item name="sortOrder" xsi:type="number">10</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">false</item>
</item>
</item>
</argument>
</field>
</fieldset>
</form>
Best Answer
From Magento 2.1 and upper Version, You can also refer blog for create attribute programmatically in detais by, Create Custom category attribute in Magento 2
You have to just below code inside
For Magento Version 2.1.*
app/code/{Packagename}/{Modulename}/Setup/InstallData.php
app/code/{Packagename}/{Modulename}/view/adminhtml/ui_component/category_form.xml
This is older version of Magento,
For Magento Version 2.0.*
Set category attribute like below,
app/code/Vendor/Categoryattr/Setup/InstallData.php
file,remove
var/generation
folder and run command,php bin/magento setup:upgrade to working inside category.