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',
]);
}
}
3 Answers 3
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>
-
hi, @Hassan It is showing in admin now, but as a textbox not as yes/no, i created yes/no attribute through InstallDataJafar Pinjar– Jafar Pinjar2018年11月09日 07:35:10 +00:00Commented Nov 9, 2018 at 7:35
-
you need dropdown ?Hassan Ali Shahzad– Hassan Ali Shahzad2018年11月09日 07:42:47 +00:00Commented Nov 9, 2018 at 7:42
-
yes, dropdown or switcherJafar Pinjar– Jafar Pinjar2018年11月09日 07:43:40 +00:00Commented Nov 9, 2018 at 7:43
-
updated my answer added: <item name="options" xsi:type="object">Magento\Eav\Model\Entity\Attribute\Source\Boolean</item>Hassan Ali Shahzad– Hassan Ali Shahzad2018年11月09日 07:47:06 +00:00Commented Nov 9, 2018 at 7:47
-
No, still it is showing as textbox. flushed cache and checkedJafar Pinjar– Jafar Pinjar2018年11月09日 07:49:30 +00:00Commented Nov 9, 2018 at 7:49
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>
-
this is the correct answer for the yes/no togglepixiemedia– pixiemedia2019年04月13日 14:07:51 +00:00Commented Apr 13, 2019 at 14:07
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();
-
can i use this code for existing module? please update me code of InstallData.phpJafar Pinjar– Jafar Pinjar2018年11月06日 09:36:45 +00:00Commented Nov 6, 2018 at 9:36
-
In my given link, there is already InstallData.php file.Kishan Patadia– Kishan Patadia2018年11月06日 09:39:47 +00:00Commented Nov 6, 2018 at 9:39
-
where to add the attribute code? $eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'is_featured', [ ?Jafar Pinjar– Jafar Pinjar2018年11月06日 12:02:46 +00:00Commented Nov 6, 2018 at 12:02
-
I have used your code, but attribute is not created for categories? I have posted my InstallData.php file to questionJafar Pinjar– Jafar Pinjar2018年11月06日 12:15:16 +00:00Commented Nov 6, 2018 at 12:15
-
Its created now, but i can't see it in admin panel inside category viewJafar Pinjar– Jafar Pinjar2018年11月06日 14:12:12 +00:00Commented Nov 6, 2018 at 14:12
Explore related questions
See similar questions with these tags.