0

I built a simple module to create another field for the category description. But I get an error:

The attribute with a "added_category_description" attributeCode doesn't exist. Verify the attribute and try again.

In etc/module.xml:

 <module name="Vendor_Module" setup_version="1.0.3" schema_version="1.0.3">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>

in vendor/module/Setup/InstallData:

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
 
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,
            'added_category_description',
            [
                'type' => 'text',
                'label' => 'Added Description',
                'input' => 'textarea',
                'required' => false,
                'sort_order' => 4,
                'global' => ScopedAttributeInterface::SCOPE_STORE,
                'wysiwyg_enabled' => true,
                'is_html_allowed_on_front' => true,
                'group' => 'General Information',
            ]
        );
    }
}

in adminhtml/ui_component/category_form.xml

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="content">
        <field name="added_category_description" template="ui/form/field" sortOrder="60" formElement="wysiwyg">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="wysiwygConfigData" xsi:type="array">
                        <item name="height" xsi:type="string">100px</item>
                        <item name="add_variables" xsi:type="boolean">false</item>
                        <item name="add_widgets" xsi:type="boolean">false</item>
                        <item name="add_images" xsi:type="boolean">true</item>
                        <item name="add_directives" xsi:type="boolean">true</item>
                    </item>
                    <item name="source" xsi:type="string">category</item>
                </item>
            </argument>
            <settings>
                <label translate="true">Added Category Description</label>
                <dataScope>added_category_description</dataScope>
            </settings>
            <formElements>
                <wysiwyg class="Magento\Catalog\Ui\Component\Category\Form\Element\Wysiwyg">
                    <settings>
                        <rows>8</rows>
                        <wysiwyg>true</wysiwyg>
                    </settings>
                </wysiwyg>
            </formElements>
        </field>
    </fieldset>
</form>

in frontend/layout/catalog_category_view.xml:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Magento\Catalog\Block\Category\View" name="added.category.description" template="Vendor_Module::product/list/added_category_description.phtml" before="-"/>
        </referenceContainer>
    </body>
</page>

and finally in frontend/templates/product/list/added_category_description.phtml:

<?php if ($_addedCategoryDescription = $block->getCurrentCategory()->getAddedCategoryDescription()): ?>
    <div class="category-added-category-description">
        <?= /* @escapeNotVerified */ $this->helper('Magento\Catalog\Helper\Output')->categoryAttribute($block->getCurrentCategory(), $_addedCategoryDescription, 'added_category_description') ?>
    </div>
<?php endif; ?>

I install the module, run upgrade, compile, deploy content, clear/flush cache, reindex and I get that error:

The attribute with a "added_category_description" attributeCode doesn't exist. Verify the attribute and try again.

What am I missing?

Thanks, Stan

7
  • I had mentioned I already did that so that isn't it. Anyone have other thoughts other than running commands I have already run?
    – stanhook
    Sep 23 '20 at 12:36
  • 1
    check in eav_attribute table if attribute exists
    – SebastianT
    Sep 24 '20 at 15:18
  • 1
    Try this: remove your module record from setup_module table. then run setup:upgrade again. This will install your module again. Maybe you create module, install it and then you create InstallData file. InstallData file run only onece when you install module. After upgrade check if your module is enable (php bin/magento module:status)
    – SebastianT
    Sep 25 '20 at 7:00
  • 1
    I also have in install method at the beginning: $setup->startSetup(); and at the end $setup->endSetup();. Try to add this, remove from setup_module and install again. And let me know if this help you .
    – SebastianT
    Sep 25 '20 at 7:20
  • 1
    @SebastianT Thanks Sebastian, that got it working.
    – stanhook
    Sep 25 '20 at 15:18
1

InstalData.php

<?php
 
namespace Vendor\Module\Setup;
 
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\InstallDataInterface;
 
class InstallData implements InstallDataInterface
{
    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
 
    /**
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }
 
    /**
     * Installs data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
 
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
           
            'category_delivery_description',
            [
                'type'         => 'text',
                'label'        => 'Delivery Description',
                'input'        => 'textarea',
                'sort_order'   => '',
                'source'       => '',
                'global'       => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'visible'      => true,
                'required'     => false,
                'user_defined' => false,
                'default'      => null,
                'wysiwyg_enabled' => true,
                'is_html_allowed_on_front' => true,
                'group'        => '',
                'backend'      => ''
            ]
        );
        $setup->endSetup();
    }
}

This is working script to add category attribute. After running this script attribute should show in eav_attribute table.
Also you have to remember that InstallData.php run only once. When you install your module, when you run php bin/magento setup:upgrade. If you create your module, run php bin/magento setup:upgrade and then you want to add custom attribute, you have to delete record realated to your module from DB, from setup_module table .
If you want to add another custom attribute after some time you can use UpgradeData.php (Helpful link)

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.