cancel
Showing results for 
Search instead for 
Did you mean: 

Create a custom Attribute for a category

Create a custom Attribute for a category

Hi all

I am new to magento and i am developing a tool to sync magento catalog with an ERP

It would be convenient to map Magento Categories to ERP Categories

 

I can do this my storing on the ERP side the IDs of the categories but I would prefer  to store the ERP ID (string) on a custom_attribute in the category.

 

I can't find any option on the backoffice to assign an attribute to the Categories.

Is this possible?

Thanks in advance

3 REPLIES 3

Re: Create a custom Attribute for a category


You have to create a custom category attribute in your custom module.

Follow the below steps:

 

1) Create Vendor/Module/registration.php

 

 

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);

 

 

2) Create Vendor/Module/etc/module.xml

 

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>


3) Create Vendor/Module/Setup/InstallData.php

 

 

<?php

namespace Vendor\Module\Setup;

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;

/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;

/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}

/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
if (version_compare($context->getVersion(), '1.0.0') < 0){

$eavSetup -> removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'erp_id');

$eavSetup -> addAttribute(\Magento\Catalog\Model\Category :: ENTITY, 'erp_id', [
'type' => 'varchar',
'label' => 'ERP ID',
'input' => 'text',
'required' => false,
'sort_order' => 110,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'group' => 'General Information',
"default" => "",
"class" => "",
"note" => ""
]
);
}
}
}

 


4) 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="general">
<field name="erp_id">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="sortOrder" xsi:type="number">110</item>
<item name="dataType" xsi:type="string">string</item>
<item name="formElement" xsi:type="string">input</item>
<item name="label" xsi:type="string" translate="true">ERP ID</item>
<item name="notice" xsi:type="string" translate="true"></item>
<item name="additionalClasses" xsi:type="string"></item>
</item>
</argument>
</field>
</fieldset>
</form>
 

 

If you find my answer useful, Please click Kudos & Accept as Solution.

Re: Create a custom Attribute for a category

Seems simple enough I will try it

 

But just to be sure, there's no way to do it in the backoffice ?

I need to have a module ?

 

 

Thank you

Re: Create a custom Attribute for a category

Magento doesn't provide any inbuilt functionality to create category attribute from admin.

You must create the module.

Magento community edition has an inbuilt feature for creating product attribute only.

Magento enterprise edition has a feature for creating Product and Customer attribute but the category attribute cannot be created.

If you find my answer useful, Please click Kudos & Accept as Solution.