0

I have created a custom attribute for my category. Below is my code. In InstallData.php

<?php
/* file: app/code/Atwix/CategoryAttribute/Setup/InstallData.php */

   namespace Vidushi\KeyFeature\Setup;

  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;
  use Magento\Catalog\Model\Category;
  use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;

/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
 * @var EavSetupFactory
 */
private $eavSetupFactory;

/**
 *
 * @param EavSetupFactory $eavSetupFactory
 */
public function __construct(EavSetupFactory $eavSetupFactory)
{
    $this->eavSetupFactory = $eavSetupFactory;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    /** @var EavSetup $eavSetup */
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    $eavSetup->addAttribute(
        Category::ENTITY,
        'keyfeature_1',
        [
            'type' => 'varchar',
            'label' => 'Key Feature 1',
            'input' => 'text',
            'required' => false,
            'sort_order' => 100,
            'global' => ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'General Information',
        ]
    );
    $eavSetup->addAttribute(
        Category::ENTITY,
        'keyfeature_2',
        [
            'type' => 'varchar',
            'label' => 'Key Feature 2',
            'input' => 'text',
            'required' => false,
            'sort_order' => 100,
            'global' => ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'General Information',
        ]
    );
    $eavSetup->addAttribute(
        Category::ENTITY,
        'keyfeature_3',
        [
            'type' => 'varchar',
            'label' => 'Key Feature 3',
            'input' => 'text',
            'required' => false,
            'sort_order' => 100,
            'global' => ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'General Information',
        ]
    );
}

}

An in category_form.xml

<?xml version="1.1" encoding="UTF-8"?>
<!-- file: app/code/Atwix/CategoryAttribute/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="Key_Features">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Key Features</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="sortOrder" xsi:type="number">100</item>
        </item>
    </argument>
    <field name="keyfeature_1">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">10</item>
                <item name="dataType" xsi:type="string">string</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="label" xsi:type="string" translate="true">Key Feature 1</item>
                <item name="required" xsi:type="boolean">false</item>
            </item>
        </argument>
    </field>
     <field name="keyfeature_2">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">20</item>
                <item name="dataType" xsi:type="string">string</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="label" xsi:type="string" translate="true">Key Feature 2</item>
                <item name="required" xsi:type="boolean">false</item>
            </item>
        </argument>
    </field>
     <field name="keyfeature_3">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">30</item>
                <item name="dataType" xsi:type="string">string</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="label" xsi:type="string" translate="true">Key Feature 3</item>
                <item name="required" xsi:type="boolean">false</item>
            </item>
        </argument>
    </field>
</fieldset>
</form>

Now i am trying to get this value as

<?php 
                         $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                         $PCategory = $objectManager->get('Magento\Catalog\Model\CategoryFactory')->create()->load(185);
                          //here get your attribute
                         ?>
                        <div><?php echo $PCategory->getKeyFeature1(); ?></div>

But this is not working, i am not getting what will come $PCategory->getKeyFeature1(); in place of getKeyFeature1(); i think this is wrong. Can anyone give me correct name based on my above code

  • Try to get like this <?php echo $PCategory->getKeyfeature1(); ?> – Virang Jethva Mar 12 at 8:49
0

Please try following code

    $categoryId = 185
    $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
    $categoryData = $categoryFactory->create()->load($categoryId);
    $getCategoryTitle1 = $categoryData->getData('keyfeature_1');

hope this will help you

|improve this answer|||||
  • No. Its not working. It appears blank – Nafisa Mar 12 at 8:41
  • You have to use store wise for category attribute value. – Chirag Parmar Mar 12 at 8:48
  • @ChiragParmar Can u please explain – Nafisa Mar 12 at 8:53
  • Answer is already given by Msquare @Nafisa – Chirag Parmar Mar 12 at 8:55
  • @ChiragParmar Can u plz guide. the problem still not resolved – Nafisa Mar 12 at 11:53
0

Please avoid use of ObjectManager

protected $_storeManager;

protected $_categoryNameFactory;


public function __construct(
    ...........................................
    \Magento\Catalog\Model\CategoryFactory $categoryNameFactory,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    ...........................................
)
{
    ...........................................
    $this->_categoryNameFactory = $categoryNameFactory;
    $this->_storeManager = $storeManager;
    ...........................................
}


public function getCategoryName($categoryId)
{
    $category = $this->_categoryNameFactory->create()->load($categoryId)->setStore($this->_storeManager->getStore());
    //echo $category->getKeyfeature1();
    //echo $category->getKeyfeature2();
    //echo  $category->getKeyfeature3();
    // exit();
    return $category;
}

Call in your phtml

$categoryId = 12;
$data = $block->getCategoryName($categoryId);
print_r($data->getData()); // update this

your getKeyfeature3 is wrong only first and after _( underscore ) character is Uppercase. remianing is lowercase

Note: please use varible name without digit for best utilization.

|improve this answer|||||
  • $category->getKeyfeature1(); this still does not work for me – Nafisa Mar 12 at 8:56
  • it may be value is not present for that particular category or check your store view is different. check it to print your category collection like print_r($data->getData()); and find your atribute name and value. – Msquare Mar 12 at 9:08
  • getting this error Error filtering template: Type Error occurred when creating object: Amasty\Shopby\Block\Product\ProductList\ChildrenCategoryList – Nafisa Mar 12 at 9:11
  • disable unused module temparary for check your attribute value. php bin/magento module:d VendoreName_ModuleName – Msquare Mar 12 at 9:18
  • when i do print_r($data) it gives me error Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 1608519680 bytes) in /var/www/wazoodl/public_html/app/design/frontend/Infortis/ultimo/Amasty_Shopby/templates/product/productlist/children_category_list.phtml on line 73 – Nafisa Mar 12 at 10:42
0

Go to Catalog -> Categories -> Select any category

Now click on Scope:All Store Views

You can find many stores here. Now save it one by one.

enter image description here

|improve this answer|||||
  • It is stored store wise only. still not able to retrieve its vale – Nafisa Mar 13 at 4:51

Your Answer

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

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