0

I have added custom attribute to category.

<?php 
namespace ................

use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Setup\CategorySetupFactory;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * Category setup factory
     *
     * @var CategorySetupFactory
     */
    private $categorySetupFactory;

    /**
     * Init
     *
     * @param CategorySetupFactory $categorySetupFactory
     */
    public function __construct(CategorySetupFactory $categorySetupFactory)
    {
        $this->categorySetupFactory = $categorySetupFactory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;

        $installer->startSetup();

        $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
        $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Category::ENTITY);
        $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);

        $menu_attributes = [
            'custom_name_enable' => [
                'type' => 'int',
                'label' => 'Enable',
                'input' => 'select',
                'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                'required' => false,
                'sort_order' => 10,
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'group' => 'Custom'
            ]
        ];

        foreach($menu_attributes as $item => $data) {
            $categorySetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, $item, $data);
        }

        $idg =  $categorySetup->getAttributeGroupId($entityTypeId, $attributeSetId, 'Custom');

        foreach($menu_attributes as $item => $data) {
            $categorySetup->addAttributeToGroup(
                $entityTypeId,
                $attributeSetId,
                $idg,
                $item,
                $data['sort_order']
            );
        }

        $installer->endSetup();
    }
}

And I have added this in 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="Custom Tab Name">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Enable</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">100</item>
            </item>
        </argument>

        <field name="custom_name_enable" sortOrder="10" formElement="checkbox">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">1</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">textarea</item>
                    <item name="label" xsi:type="string" translate="true">Enable</item>
                    <item name="required" xsi:type="boolean">false</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

It saves in my fresh install instance Magento 2.3.3 but it never saves in my migrated instance Magento 1 > Magento 2.3.3.

I am suspecting it might be something to do with my migrated category data or attributes, but I had gone to check them one by one and they seem alright in eav_attribute filter type by 3.

Anyone had a similar problem? I am frustrated debugging it for 2 days already. Nothing works.

I have also gone ad added a new row in catalog_category_entity_int for my attribute and I can get the value in frontend with $category->getData('custom_name_enable'), but it never shows up in backend while editing category.

I have also checked by logging $this->loadedData in Magento\Catalog\Model\Category\DataProvider::getData(), but I never see my custom attribute in the log.

Any suggestions? I have run out of my debugging options now.

UPDATE 1 $category->getCustomAttributes() doesn't give me my custom attribute at all.

0

First, you have to check attribute in the table catalog_eav_attribute. In that, you need to add your atribute custome and remember to set is_global = 0, is_visible = 1.

Tell us if it works!

  • I confirm this is how the row looks in catalog_eav_attribute. I have said in qst, I will say again, it works in FRESH INSTALL but not after data migration in M2.3.3 – Adarsh Khatri Nov 8 at 8:31
0

This is strange, but after spending 3 days on this, I got frustrated and uninstalled my extension and then install it again.

My uninstall script had removed the attribute and it installed the attribute again. And now, amazingly it is saving.

This is Turn Off and Turn On kind of thing. I still don't know why this didn't work before and works now.

I am adding this answer so that anyone having this weird problem might get some help.

I will not mark it as the correct answer, will wait for more insight on it.

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.