Skip to content

Add custom Category Attribute to Category view on Front end

I’ve created a module that adds a custom attribute for a category, Its a bolean that I will use to show/not show some text on frontend.

The attribute its being saved into the Database and I can get it and modify it in the backend, but I cannot get it in the frontend, I’ve really try many ways and nothing. Also when I do a Print_r of the category the attribute its not there.

I have some other files, but i Didnt add them for making the question not that long.This is what I have so far, please anyone can help me. Thanks

File: vendor/Module/Setup/InstallData.php

<?php

namespace vendormoduleSetup;

use MagentoFrameworkSetup{
    ModuleContextInterface,
    ModuleDataSetupInterface,
    InstallDataInterface
};

use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory) {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(MagentoCatalogModelCategory::ENTITY, 'rr_show', [
            'type'     => 'int',
            'label'    => 'Desplegar Bloque RR',
            'input'    => 'boolean',
            'source'   => 'MagentoEavModelEntityAttributeSourceBoolean',
            'visible'  => true,
            'default'  => '1',
            'required' => false,
            'global'   => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
            'group'    => 'General Information',
            'sort_order' => 909,
        ]);
    }
}

Also
File: vendor/module/view/adminhtml/cateogry_form.xml

c<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="rr_show">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">boolean</item>
                    <item name="formElement" xsi:type="string">checkbox</item>
                    <item name="label" xsi:type="string" translate="true">Desplegar Bloque RR</item>
                    <item name="prefer" xsi:type="string">toggle</item>
                    <item name="valueMap" xsi:type="array">
                        <item name="true" xsi:type="string">1</item>
                        <item name="false" xsi:type="string">0</item>
                    </item>
                    <item name="default" xsi:type="number">1</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

File: vendor/module/etc/catalog_attribute.xml

?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
    <group name="catalog_category">
        <attribute name="rr_show"/>
    </group>
</config>

File: vendor/module/view/frontend/templates/category.phtml

<?php

$isEnable = $block->getCurrentCategory()->getData('rr_show');
$isEnabletoString = $isEnable ? 'true' : 'false';
$categoryId = $block->getCurrentCategory()->getId();

print_r($categoryId);
?>
<?php 
<div isenable="<?=$isEnabletoString;?>"></div>
?>

This print_r show many attributes except mine, So this cageory.phtml its being called.

Thanks!