0

I have created a custom category select attribute to list active categories, after choosing a category from the list and saving there is an error message displayed "attribute value is empty. Set the attribute and try again" I have checked the values of the selected category it seems like good


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_category_attribute" formElement="select" sortOrder="20">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">xxxx\CustomCategoryAttribute\Model\Config\Source\CustomOptionList</item>
            </argument>
            <settings>
                <dataType>text</dataType>
                <label translate="true">Category</label>
                <dataScope>category</dataScope>
            </settings>
        </field>
    </fieldset>
</form>

Patch file

<?php

namespace xxxx\CustomCategoryAttribute\Setup\Patch\Data;

use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Catalog\Model\Category;


class CategoryAttribute implements DataPatchInterface
{

    protected $_moduleDataSetup;
    protected $_eavSetupFactory;

    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->_moduleDataSetup = $moduleDataSetup;
        $this->_eavSetupFactory = $eavSetupFactory;
    }

    public function apply()
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->_eavSetupFactory->create(['setup' => $this->_moduleDataSetup]);

        $eavSetup->addAttribute(Category::ENTITY, 'category_category_attribute', [
            'type' => 'int',
            'label' => 'category list',
            'input' => 'select',
            'default' => '',
            'source' => 'xxxx\CustomCategoryAttribute\Model\Config\Source\CustomOptionList',
            'sort_order' => 3,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'General Information',
            'visible_on_front' => true
        ]);
    }

    public static function getDependencies()
    {
        return [];
    }

    public function getAliases()
    {
        return [];
    }
}


2 Answers 2

0

Update eav attribute creation code to this and then try

$eavSetup->addAttribute(Category::ENTITY, 'category_category_attribute', [
                'type' => 'varchar',
                'label' => 'category list',
                'input' => 'select',
                'default' => '',
                'source' => 'xxxx\CustomCategoryAttribute\Model\Config\Source\CustomOptionList',
                'sort_order' => 3,
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'group' => 'General Information',
                'visible_on_front' => true,
                'backend' => '',
                'visible' => 1,
                'required' => 0,
                'user_defined' => 1,
            ]);
1
  • required =>0 is not a solution for this , I tried but no changes. when I inspected, I found that the corresponding categories id set on the value of the select UI component but not saving
    – R J
    Commented Mar 30, 2022 at 10:58
0

<?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="promo_category_category_attribute">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">
                    xxxx\PromoCategory\Model\Config\Source\PromoCategoryOptionList
                </item>
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Promo Category</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="source" xsi:type="string">promo_category_category_attribute</item>
                    <item name="dataScope" xsi:type="string">promo_category_category_attribute</item>
                    <item name="sortOrder" xsi:type="number">20</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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