cancel
Showing results for 
Search instead for 
Did you mean: 

How to add custom category attribute text ?

SOLVED

How to add custom category attribute text ?

Hello,

I'am trying to add a short description to categories (like product short description). I followed this article https://devdocs.magento.com/guides/v2.3/ui_comp_guide/howto/add_category_attribute.html but this is not working. I replaced the boolean attribute in the article by a text attribute.

InstallData.php :

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, 'category_short_description', [
            'type'     => 'varchar',
            'label'    => 'Short description',
            'input'    => 'text',
            'sort_order'    => 45,
            'source'   => '',
            'visible'  => true,
            'default'  => 'null',
            'required' => false,
            'user_defined' => false,
            'global'   => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'group'    => '',
            'backend'      => ''
        ]);
    }
}

And I created category_form.xml :

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, 'category_short_description', [
            'type'     => 'varchar',
            'label'    => 'Short description',
            'input'    => 'text',
            'sort_order'    => 45,
            'source'   => '',
            'visible'  => true,
            'default'  => 'null',
            'required' => false,
            'user_defined' => false,
            'global'   => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'group'    => '',
            'backend'      => ''
        ]);
    }
}

I executed setup:upgrade and clean:cache, then the new attribute is displayed in backend for category edition. But the value set in the new attribute is not saved when I save the category.

I have the following errors messages in debug.log :

[2020-05-11 10:36:18] main.DEBUG: cache_invalidate:  {"method":"POST","url":"https://creativemusicshop.com/cmsadm/catalog/category/save/key/8302823a26af30b0fe6df5417100717e43fe25c471802d1a7ae16c1842004be4/","invalidateInfo":{"tags":["cat_c"],"mode":"matchingAnyTag"}} []
[2020-05-11 10:36:18] main.DEBUG: cache_invalidate:  {"method":"POST","url":"https://creativemusicshop.com/cmsadm/catalog/category/save/key/8302823a26af30b0fe6df5417100717e43fe25c471802d1a7ae16c1842004be4/","invalidateInfo":{"tags":["cat_c_3","FPC"],"mode":"matchingTag"}} []
[2020-05-11 10:36:18] main.DEBUG: cache_invalidate:  {"method":"POST","url":"https://creativemusicshop.com/cmsadm/catalog/category/save/key/8302823a26af30b0fe6df5417100717e43fe25c471802d1a7ae16c1842004be4/","invalidateInfo":{"tags":["cat_c_p_3","FPC"],"mode":"matchingTag"}} []
[2020-05-11 10:36:18] main.DEBUG: cache_invalidate:  {"method":"POST","url":"https://creativemusicshop.com/cmsadm/catalog/category/save/key/8302823a26af30b0fe6df5417100717e43fe25c471802d1a7ae16c1842004be4/","invalidateInfo":{"tags":["cat_c_3","FPC"],"mode":"matchingTag"}} []
[2020-05-11 10:36:18] main.DEBUG: cache_invalidate:  {"method":"POST","url":"https://creativemusicshop.com/cmsadm/catalog/category/save/key/8302823a26af30b0fe6df5417100717e43fe25c471802d1a7ae16c1842004be4/","invalidateInfo":{"tags":["cat_c_3"],"mode":"matchingAnyTag"}} []
[2020-05-11 10:36:18] main.DEBUG: cache_invalidate:  {"method":"POST","url":"https://creativemusicshop.com/cmsadm/catalog/category/save/key/8302823a26af30b0fe6df5417100717e43fe25c471802d1a7ae16c184200

I have no other error messages.
I also tried only the code in the article with the boolean example but this is the same, when I modify the value, this is not saved !
Is some can help me please ?

 

I'am running under magento CE 2.3.4

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to add custom category attribute text ?

Hi @Nishu Jindal ,

 

Many thanks again for your help, finally the issue was about the setup:upgrade which not create the EAV attribute in the database. I resolved the problem by deleting my module in setup_module table and after run setup:upgrade.

 

Regards,

View solution in original post

5 REPLIES 5

Re: How to add custom category attribute text ?

Hi @Yohannento ,

 

Can you please check the below link and do the required changes in your install data file.

https://magento.stackexchange.com/questions/180757/magento2-custom-category-attribute-value-not-savi...

 

I hope this works for you.

 

Thanks!

---------------------------------------

Problem Solved! Click Kudos & Accept as Solution!

Re: How to add custom category attribute text ?

Hi @Nishu Jindal ,

 

Thanks for your reply, I followed the link and there is no solution for Magento 2.3.4. There is another contributor with the same issue on the same version but I cannot comment on Stackexchange because I have no reputation. Thanks for your help, I created an issue on Devdoc Magento Help. I hope that someone can help me.

 

 

Re: How to add custom category attribute text ?

Hi @Yohannento 

 

Thanks for the prompt response. I mentioned the link Url so that you will do the below change in your installData.php file

'used_in_product_listing' => true,

I tried the same code at my end in the same version and it is working fine for me. Please try at your end once.

 

Thanks!

--------------------------------------

Problem Solved! Click Kudos and Accept as Solution!

Re: How to add custom category attribute text ?

Hi @Nishu Jindal ,

 

Many thanks again for your help, finally the issue was about the setup:upgrade which not create the EAV attribute in the database. I resolved the problem by deleting my module in setup_module table and after run setup:upgrade.

 

Regards,

Re: How to add custom category attribute text ?

Hi @Yohannento ,

Glad to know your problem is solved. 

Thanks!

Problem solved! Click Kudos & Accept as Solution.