1

Is it possible to create the custom attribute for a category called is_featuted category as 'Yes/No' attribute?

I am looking for code how to create the custom attribute for category and get all the categories where the attribute value is 'Yes'.

Can we get all the category collection where is_featured is "Yes"?

Here is the code i used in 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, 'is_featured', [
        'type'     => 'int',
        'label'    => 'Is Home Category',
        'input'    => 'boolean',
        'source'   => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
        'visible'  => true,
        'default'  => '0',
        'required' => false,
        'global'   => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
        'group'    => 'Display Settings',
      ]);
    }
}
2

enter image description here

Setup/InstallData.php

<?php


namespace Vendor\Module\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;

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,
            'is_featured',
            [
                'type' => 'int',
                'label' => 'Is Home Category',
                'input' => 'select',
                'sort_order' => 333,
                'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                'global' => 1,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => null,
                'group' => 'General Information',
                'backend' => ''
            ]
        );
    }
}

view/adminhtml/ui_component/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="is_featured">
                <argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Magento\Eav\Model\Entity\Attribute\Source\Boolean</item>
                    <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">333</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">Is Home Category</item>
                    </item>
                </argument>
            </field>
        </fieldset>
    </form>
|improve this answer|||||
  • hi, @Hassan It is showing in admin now, but as a textbox not as yes/no, i created yes/no attribute through InstallData – jafar pinjar Nov 9 '18 at 7:35
  • you need dropdown ? – Hassan Ali Shahzad Nov 9 '18 at 7:42
  • yes, dropdown or switcher – jafar pinjar Nov 9 '18 at 7:43
  • updated my answer added: <item name="options" xsi:type="object">Magento\Eav\Model\Entity\Attribute\Source\Boolean</item> – Hassan Ali Shahzad Nov 9 '18 at 7:47
  • No, still it is showing as textbox. flushed cache and checked – jafar pinjar Nov 9 '18 at 7:49
4

Add Following Code in ui_component xml file to Add Yes/No Toggle Option in Your admin Category Attribute. File Path :view/adminhtml/ui_component/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="your_attribute" sortOrder="100" 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">Your Attriute</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>
</form>
|improve this answer|||||
  • this is the correct answer for the yes/no toggle – pixiemedia Apr 13 '19 at 14:07
1

You can follow below tutorial. It will add boolean custom attribute to category.

https://devdocs.magento.com/guides/v2.2/ui_comp_guide/howto/add_category_attribute.html

Change

 'label'    => 'Your Category Attribute Name',

to your required label like

 'label'    => 'Is Featured',

To filter category collection by this custom attribute, you can use below code.

use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;

protected $collectionFactory;

public function __construct(
 .....
 $this->collectionFactory = $collectionFactory;
 .....
}

$categories =   $this->collectionFactory->create()
  ->addAttributeToFilter('is_featured',1)->load();
|improve this answer|||||
  • can i use this code for existing module? please update me code of InstallData.php – jafar pinjar Nov 6 '18 at 9:36
  • In my given link, there is already InstallData.php file. – Kishan Patadia Nov 6 '18 at 9:39
  • where to add the attribute code? $eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'is_featured', [ ? – jafar pinjar Nov 6 '18 at 12:02
  • I have used your code, but attribute is not created for categories? I have posted my InstallData.php file to question – jafar pinjar Nov 6 '18 at 12:15
  • Its created now, but i can't see it in admin panel inside category view – jafar pinjar Nov 6 '18 at 14:12

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.