0

I am trying to add some custom attributes to my Category page, without success. I created a custom module with its own setup script, after running:

php bin/magento setup:upgrade

There are no attributes added to my databsase. Here are are my module files:

app/code/Forza/CategoriesBanner/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Forza_CategoriesBanner',
    __DIR__
);

app/code/Forza/CategoriesBanner/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Forza_CategoriesBanner" setup_version="1.0.0">
    </module>
</config>

app/code/Forza/CategoriesBanner/Setup/InstallData.php

<?php
namespace Forza\CategoriesBanner\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
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
            $setup->startSetup();

            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

            $eavSetup->addAttribute(
                    \Magento\Catalog\Model\Category::ENTITY,
                    'banner_image',
                    [
                        'type'          => 'varchar',
                        'label'         => 'Banner Image',
                        'input'         => 'image',
                        'required'      => false,
                        'sort_order'    => 6,
                        'backend'       => 'Forza\CategoriesBanner\Model\Category\Attribute\Backend\CategoryBannerImage',
                        'global'        => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                        'group'         => 'General Information',
                    ]
                );
            $setup->endSetup();
    }
}

?>

I am not getting errors on setup:upgrade, so don't really know where to look. What could be the cause of this?

1

if you want to add Image attribute then try below code in InstallData.php

<?php
namespace Namespace\Module\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Setup\CategorySetupFactory;
class InstallData
    implements InstallDataInterface
{
    /**
     * Category setup factory
     *
     * @var CategorySetupFactory
     */
    private $categorySetupFactory;
    /**
     * Init
     *
     * @param CategorySetupFactory $categorySetupFactory
     */
    public function __construct(
        CategorySetupFactory $categorySetupFactory
    ) {
        $this->categorySetupFactory = $categorySetupFactory;
    }
    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();
        /** @var \Magento\Catalog\Setup\CategorySetup $categorySetup */
        $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
        $categorySetup->addAttribute(
            'catalog_category',
            'banner_image',
            [
                'type' => 'varchar',
                'label' => 'Additional Image',
                'input' => 'image',
                'backend' => 'Magento\Catalog\Model\Category\Attribute\Backend\Image',
                'required' => false,
                'sort_order' => 100,
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'group' => 'General Information',
            ]
        );
        $setup->endSetup();
    }
}
  • Hi, thanks for your comment. I tried the code above, but no attribute is added to the eav_attributes table. Any suggestions? – perrivdb Aug 25 '17 at 14:37
  • did you replace first line namespace Namespace\Module\Setup; with your module and namespace? – Abhishek Panchal Aug 25 '17 at 14:39
  • Didnt have a core_resources table, but deleted the 'Forza_CategoriesBanner' row from the setup_module table and tried a setup:upgrade again, now the attribute is added to my database! Thanks a lot! – perrivdb Aug 25 '17 at 14:48

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.