I've create a module to add a custom attribute to my categories, however my code doesn't seem to be display the attribute on frontend. i dont know where i am wrong.
app/code/Vendor/Module/Setup/InstallData.php
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
/**
* Constructor
*
* @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
*/
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* {@inheritdoc}
*/
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Category::ENTITY,
'attribute_code',
[
'type' => 'int',
'label' => 'Attribute Label',
'input' => 'select',
'sort_order' => 333,
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'default' => 0,
'visible_on_front' => true,
'group' => 'General Information'
]
);
}
}
/app/code/Vendor/Module/view/adminhtml/ui_component/category_form.xml
<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="attribute_code" sortOrder="20" formElement="checkbox">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">category</item>
<item name="default" xsi:type="number">0</item>
</item>
</argument>
<settings>
<validation>
<rule name="required-entry" xsi:type="boolean">false</rule>
</validation>
<dataType>boolean</dataType>
<label translate="true">Attribute Label</label>
</settings>
<formElements>
<checkbox>
<settings>
<valueMap>
<map name="false" xsi:type="string">0</map>
<map name="true" xsi:type="string">1</map>
</valueMap>
<prefer>toggle</prefer>
</settings>
</checkbox>
</formElements>
</field>
</fieldset>
$category->getMyOwnCustomAttributeCodeInCategory()