0

I created a custom category attribute attribute - Category Heading - That I want to display on Category pages (specifically taking the place of the category H1s in title.phtml). The new attribute shows in the admin and it saves in the database, but I am not sure how to call it on a category page.

Attribute is saved in the catalog_category_entity_varchar table, but doesn't seem to be associated with category data - $category->getData()

I have tried to get the attribute with the below code, but it has not been successful:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
        $categoryHeading = $category->getData('category_heading');

Here is my Setup/InstallData.php

<?php
namespace WeekendsOnly\CategoryName\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
 private $eavSetupFactory;
    
 public function __construct(EavSetupFactory $eavSetupFactory) {
     $this->eavSetupFactory = $eavSetupFactory;
 }
 public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
    $setup->startSetup();
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

   $eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'category_heading', [
            'type' => 'varchar',
            'label' => 'Category Heading',
            'input' => 'text',
            'source' => '',
            'visible'  => true,
            'sort_order' => 40,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'required' => false,
            'group' => 'General Information',
        ]
    );
     $setup->endSetup();
 }
}

And here is my category_form.xml

<?xml version="1.0" ?>
<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="category_heading">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="required" xsi:type="boolean">false</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                    <item name="sortOrder" xsi:type="number">40</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="label" translate="true" xsi:type="string">Category Heading</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.