How to create category attribute of type int in Magento 2?
-
22-02-2021 - |
题
I am using Magento 2.3
I have created the Custom extension for category attribute "Popular category" and I have installed this extension successfully but the issue is I have set the default value as 0 but when I saw the attribute in admin it is already selected.
Here are the extension files:
app/code/Custom/CategoryAttributes/Setup/InstallData.php
<?php
namespace Custom\CategoryAttributes\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,
'popular_category',
[
'type' => 'int',
'label' => 'Popular Category',
'input' => 'select',
'required' => false,
'sort_order' => 100,
'global' => ScopedAttributeInterface::SCOPE_STORE,
'group' => 'General Information',
'default' => 0,
'visible_on_front' => true,
]
);
}
}
app/code/Custom/CategoryAttributes/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="include_in_menu" 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="string">0</item>
</item>
</argument>
<settings>
<validation>
<rule name="required-entry" xsi:type="boolean">false</rule>
</validation>
<dataType>boolean</dataType>
<label translate="true">Popular Category</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>
Attribute in admin:
As you can see attribute is already selected to "Yes" but I want it to be selected as "No" by default. So, how can I achieve this?
And also I am not able to use this attribute in the front end. So, How can I do this?
解决方案
I tried your InstallData.php file and it is working perfectly.
The issue is in your
app/code/Custom/CategoryAttributes/view/adminhtml/ui_component/category_form.xml
you use "include_in_menu" as the name in field tag, use your custom attribute name "popular_category" instead of "include_in_menu".
Below is my code which I tried
app/code/Mycompany/CategoryAttribute/Setup/InstallData.php
<?php
namespace Mycompany\CategoryAttribute\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetupFactory;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$installer = $setup;
$installer->startSetup();
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Category::ENTITY,
'is_popular_category',
[
'type' => 'int',
'label' => 'Popular Category',
'input' => 'select',
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
'default' => 0,
'sort_order' => 10,
'group' => 'General Information',
'visible_on_front' => true,
]
);
}
}
and change in tag "field" name
app/code/Mycompany/CategoryAttribute/view/adminhtml/ui_component/category_form.xml
<field name="is_popular_category" 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="string">0</item>
</item>
</argument>
<settings>
<validation>
<rule name="required-entry" xsi:type="boolean">false</rule>
</validation>
<dataType>boolean</dataType>
<label translate="true">Popular Category</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>
其他提示
Try this,
Instead of
'default' => 0
give as null and that select no by default
'default' => null
and input should be boolean not select
'input' => 'boolean'
instead of
'input' => 'select'
NOTE : Remove your module from setup_module then run php bin/magento setup:upgrade and also delete your attribute from eav_attribute table.
Query to remove the module from setup_module table in M2
DELETE from setup_module where module = 'Custom_CategoryAttributes';
and to delete it from eav_attribute table
DELETE from eav_attribute where attribute_code = 'popular_category';
Hope this helps :)