0

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 vendor\module\Setup;

use Magento\Framework\Setup\{
    ModuleContextInterface,
    ModuleDataSetupInterface,
    InstallDataInterface
};

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;

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(\Magento\Catalog\Model\Category::ENTITY, 'rr_show', [
            'type'     => 'int',
            'label'    => 'Desplegar Bloque RR',
            'input'    => 'boolean',
            'source'   => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            'visible'  => true,
            'default'  => '1',
            'required' => false,
            'global'   => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::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!

1 Answer 1

1

I have create category attribute(is_home_page) same you and its working for me

File : InstallData.php

$eavSetup -> addAttribute(\Magento\Catalog\Model\Category :: ENTITY, 'is_home_page', [
        'type' => 'int',
        'label' => 'Is Home Page',
        'input' => 'select',
        'required' => false,
        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
        'sort_order' => 110,
        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
        'group' => 'General Information',
        "default" => "",
        "class"    => "",
        "note"       => ""
]);

Also File: vendor/module/view/adminhtml/cateogry_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="is_home_page">
            <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">boolean</item>
                    <item name="formElement" xsi:type="string">checkbox</item>
                    <item name="source" xsi:type="string">category</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="string">0</item>
                    <item name="label" xsi:type="string" translate="true">Is Home Page</item>
                    <item name="notice" xsi:type="string" translate="true"></item>
                    <item name="additionalClasses" xsi:type="string"></item>
                    
                </item>
            </argument>
        </field>                  
    </fieldset>
</form>

I did not create this file : vendor/module/etc/catalog_attribute.xml

enter image description here

2
  • Hello, thanks but my problem is that i cannot get it in the front end category. Thanks Commented Sep 2, 2023 at 19:26
  • Try this once ............. $categoryId = 5; $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $object_manager = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId); echo "<pre>"; print_r($object_manager->getData('rr_show'));
    – MP Raj
    Commented Sep 4, 2023 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.