0

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>
asked Oct 30, 2020 at 7:46
5
  • With this file /app/code/view/adminhtml/ui_component "adminhtml" scope it should be in admin side. Commented Oct 30, 2020 at 8:01
  • @HamendraSunthwal Thank you for reply, but i didn't get you. Commented Oct 30, 2020 at 8:42
  • @FenaShah What he is trying to explain is that your category_form.xml only add the field in the admin part. If you want to add this field in front you need extra modification depending on what you try to do. Commented Oct 30, 2020 at 9:11
  • @Claims ohh got it. but attribute is saving value in database so i can get it in category collection on frontend. Commented Oct 30, 2020 at 9:45
  • Depending on where you want in the frontend it will always be possible to get the attribute value indeed. Usually you have a magical setter / getter on your attribute depending on it's code. $category->getMyOwnCustomAttributeCodeInCategory() Commented Oct 30, 2020 at 10:25

2 Answers 2

1

app/code/Vendor/Module/Setup

InstallData.php

<?php
namespace Vendor\Module\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
 protected $eavSetupFactory;
 public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory)
 {
 $this->eavSetupFactory = $eavSetupFactory;
 }
 public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
 {
 $setup->startSetup();
 if (version_compare($context->getVersion(), '0.0.1', '<=')) {
 $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
 $eavSetup->addAttribute(
 \Magento\Catalog\Model\Category::ENTITY,
 'is_enable_attribute11',
 [
 'type' => 'int',
 'label' => 'Display Category in Homepage',
 'input' => 'Boolean',
 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
 'visible' => true,
 'required' => false,
 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
 'group' => 'General Information',
 
 ]
 );
 }
 $setup->endSetup();
 }
}

/app/code/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="is_enable_attribute11">
 <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">Display Category in Homepage</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>
 </argument>
 </field>
 </fieldset>
</form>

Try to get value from it like this

<?php
namespace Vendor\Module\Block;
use Magento\Catalog\Model\Product;
class Categoryimage extends \Magento\Framework\View\Element\Template
{
 protected $_categoryFactory;
 protected $_storeManager;
 protected $_categoryNameFactory;
 public function __construct(
 \Magento\Framework\View\Element\Template\Context $context,
 \Magento\Catalog\Model\CategoryFactory $categoryNameFactory,
 \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $collecionFactory,
 \Magento\Store\Model\StoreManagerInterface $storeManager,
 \Magento\Framework\Registry $registry,
 array $data = []
 )
 {
 $this->_coreRegistry = $registry;
 $this->_categoryNameFactory = $categoryNameFactory;
 $this->_categoryFactory = $collecionFactory;
 $this->_storeManager = $storeManager;
 parent::__construct($context, $data);
 }
 public function getEnableCategory()
 {
 // this return all category with enable attribute values
 $category = $this->_categoryFactory->create()->addAttributeToFilter('is_enable_attribute11',1)->setStore($this->_storeManager->getStore());
 return $category;
 }
 public function getCategoryName($categoryId)
 {
 $category = $this->_categoryNameFactory->create()->load($categoryId)->setStore($this->_storeManager->getStore());
 //$categoryName = $category->getName();
 return $category;
 }
}

Also try this to get values from collection

$category->getMyCustomAttribute()
// or
$category->getData('my_custom_attribute');
// or
$category->addAttributeToSelect('my_custom_attribute')
answered Oct 30, 2020 at 9:23
1
1

Add the Below code in your block class

<?php 
 
 namespace Vendor\Module\Block;
 use Magento\Framework\View\Element\Template;
 class CategoryCollection extends \Magento\Framework\View\Element\Template{
 
 protected $categoryRepository;
 public function __construct(
 Template\Context $context,
 \Magento\Catalog\Model\CategoryRepository $categoryRepository,
 array $data = []
 )
 {
 $this->categoryRepository = $categoryRepository;
 parent::__construct($context, $data);
 }
 public function getCategoryAttribute($id){
 return $this->categoryRepository->get($id);
 }
 
 }

Add the Below Code in your phtml file

<?php
 /**
 *
 * @var $block \vendor\Module\Block\CategoryCollection 
 */
 
 $id = 10; //your category id.
 
 
 $customAttribute= $block->getCategoryAttribute($id);
 echo $customAttribute->getData('attribute_code');
?>
answered Oct 31, 2020 at 5:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.