0

I have created custom category attribute by below setup.

namespace Hello\Catattribute\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

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]);
        if (version_compare($context->getVersion(), '1.0.0') < 0){
            $eavSetup -> removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'right_top');
            $eavSetup -> addAttribute(\Magento\Catalog\Model\Category :: ENTITY, 'right_top', [
                        'type' => 'text',
                        'label' => 'Top Right Html',
                        'input' => 'textarea',
                        'required' => false,
                        'sort_order' => 110,
                        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                        'wysiwyg_enabled' => true,
                        'is_html_allowed_on_front' => true,
                        'group' => 'General Information'
                    ]
            );
        }

    }
}

Now I want to add some more custom attributes for category but without creating a new module, to do so I have created an UpgradeSchema and changed the setup version in module.xml.

namespace Hello\Catattribute\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class UpgradeSchema implements UpgradeSchemaInterface {

    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) {
        $eavSetup = $setup->create(['setup' => $setup]);
        if(version_compare($context->getVersion(), '1.0.1') < 0) {
            $eavSetup -> removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'background_img');
            $eavSetup -> addAttribute(\Magento\Catalog\Model\Category :: ENTITY, 'background_img', [
                        'type' => 'varchar',
                        'label' => 'Background Image',
                        'input' => 'image',
                        'backend' => \Magento\Catalog\Model\Category\Attribute\Backend\Image::class,
                        'required' => false,
                        'sort_order' => 115,
                        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                        'group' => 'General Information',
                    ]
            );



        }
        $eavSetup->endSetup();
    }
}

But it's not working. Please help me or suggest me that how can I upgrade EAV setup schema.

3

Try below code. You should created UpgradeData.php instead of creating UpgradeSchema.php.

<?php

namespace Hello\Catattribute\Setup;

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Setup\CategorySetupFactory;

class UpgradeData
    implements UpgradeDataInterface
{
    /**
     * Category setup factory
     *
     * @var CategorySetupFactory
     */
    private $categorySetupFactory;

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

    public function upgrade(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();
        if (version_compare($context->getVersion(), '0.0.2', '<')) {
            $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

            $categorySetup->addAttribute(
            'catalog_category',
            'page_not_found',
            array(
                'type' => 'int',
                'label' => 'Display on 404 Page',
                'input' => 'select',
                'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                'sort_order' => 3,
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'group' => 'General Information',
            ));
        }
    }   
}

Please check the version of your module and change accordingly in UpgradeData.php and module.xml file

Don't forget to run setup:upgrade command

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.