2

I made this code to create the custom attribute:

$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->addAttribute(
    Category::ENTITY,
    'is_tile',
    [
        'type'         => 'int',
        'label'        => 'My Custom Label',
        'input'        => 'boolean',
        'sort_order'   => 100,
        'source'       => '',
        'global'       => ScopedAttributeInterface::SCOPE_STORE,
        'visible'      => true,
        'required'     => false,
        'user_defined' => false,
        'default'      => null,
        'group'        => '',
        'backend'      => '',
        'used_in_product_listing' => true,
        'visible_on_front' => true,
        'is_used_in_grid' => true,
        'is_visible_in_grid' => true
    ]
);

$this->moduleDataSetup->getConnection()->endSetup();

It works, I can save the value of is_tile on the backend side. But I would like to get it in this phtml file:

<block class="Magento\Catalog\Block\Category\View" name="category.products" template="Magento_Catalog::category/products.phtml">

products.phtml:

<?php $category = $block->getCurrentCategory(); ?>
<?php var_dump($category->getData('is_tile')); ?>

But the output of my var_dump is NULL . I've also made this file in my module etc/catalog_attributes.xml, with this content:

<?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="is_tile"/>
    </group>
</config>

But I am still getting the null value. How can I get my custom category attribute value, without reloading the category? like here:

$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('is_tile'));
4
  • In your examples you have a typo - instead of is_title you have is_tile
    – paj
    Commented Jan 6, 2022 at 13:52
  • @paj good catch , but I still have this issue. I updated my post. Commented Jan 6, 2022 at 13:55
  • have u did indexing
    – Amit Bera
    Commented Jan 6, 2022 at 14:32
  • Hey @AmitBera Yes, I did, but still the same :). If I use the object manager to reload the category, it works . But as I said , I would like to avoid this . Commented Jan 6, 2022 at 14:39

1 Answer 1

0

use below solution:

  $resource = $category->getResource();
    $store = $this->storeManager->getStore();
    $ressource->getAttributeRawValue($category->getId(),'is_tile',$store->getId())

here i used resource model.

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.