1

I've create a module to add a custom attribute to my categories,the field shows on my category pages but it doesn't save the values?

Working on Magento ver. 2.2.2

Can anyone advise why this isn't working.

/app/code/NameSpace/CategoryAttributes/Setup/InstallData.php

<?php 

// File: Namespace/Module/Setup/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, 'ss_category_color', [
            'type'          => 'varchar',
            'label'         => 'Category Color',
            'input'         => 'text',
            'source'        => '',
            'visible'       => true,
            'default'       => '',
            'required'      => false,
            'user_defined'  => false,
            'global'        => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'group'         => 'General Information',
        ]);
    }
}

/app/code/NameSpace/CategoryAttributes/view/adminhtml/ui_component/category_form.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!-- Namespace/Module/view/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="general_information">
        <field name="ss_category_color">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">100</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="label" xsi:type="string" translate="true">Category Color</item>
                    <item name="required" xsi:type="boolean">false</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>
3

app/code/Ravindra/Categoryattributes/view/adminhtml/ui_component/category_form.xml

<?xml version="1.0" ?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="general">
        <field name="ss_category_color">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="required" xsi:type="boolean">false</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                    <item name="sortOrder" xsi:type="number">333</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="label" translate="true" xsi:type="string">Category Color</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

app/code/Ravindra/Categoryattributes/Setup/UpgradeData.php

<?php


namespace Ravindra\Categoryattributes\Setup;

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

class UpgradeData implements UpgradeDataInterface
{

    /**
     * {@inheritdoc}
     */
    public function upgrade(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();
        if (version_compare($context->getVersion(), "2.2.2", "<")) {
        //Your upgrade script
        }
        $setup->endSetup();
    }
}

app/code/Ravindra/Categoryattributes/Setup/InstallData.php

    <?php


namespace Ravindra\Categoryattributes\Setup;

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

class InstallData implements InstallDataInterface
{

    private $eavSetupFactory;

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        //Your install script

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

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            'ss_category_color',
            [
                'type' => 'varchar',
                'label' => 'Category Color',
                'input' => 'text',
                'sort_order' => 333,
                'source' => '',
                'global' => 1,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => null,
                'group' => 'General Information',
                'backend' => ''
            ]
        );
    }

    /**
     * Constructor
     *
     * @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }
}

app/code/Ravindra/Categoryattributes/Setup/InstallSchema.php

<?php


namespace Ravindra\Categoryattributes\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface
{

    /**
     * {@inheritdoc}
     */
    public function install(
        SchemaSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $installer = $setup;
        $installer->startSetup();

        $setup->endSetup();
    }
}

app/code/Ravindra/Categoryattributes/Setup/UpgradeSchema.php

<?php


namespace Ravindra\Categoryattributes\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{

    /**
     * {@inheritdoc}
     */
    public function upgrade(
        SchemaSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();
        if (version_compare($context->getVersion(), "2.2.2", "<")) {
        //Your upgrade script
        }
        $setup->endSetup();
    }
}

Now check screen short

enter image description here

  • Hi, thank for your answer, just i need to edit the install function right? or i need to create an upgrade script? – mahmoudismail Jan 18 '18 at 8:55
  • I've create a new module and it's working perfectly but i want to update nad fix the problem in my existing module how i can do that please? – mahmoudismail Jan 18 '18 at 9:05
  • now check my updated code. – Ravindrasinh Zala Jan 18 '18 at 9:11
  • most welcome @mahmoudismail – Ravindrasinh Zala Jan 18 '18 at 9:14
  • please my answer upvote @mahmoudismail – Ravindrasinh Zala Jan 18 '18 at 9:27
0

Try this

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY, 
            'ss_category_color', 
            [
                'type'          => 'varchar',
                'label'         => 'Category Color',
                'input'         => 'text',
                'visible'       => true,
                'required'      => false,
                'user_defined'  => false,
                'global'        => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'group'         => 'General Information'
            ]);
    }
  • Hi, thanks for your answer but still not working. – mahmoudismail Jan 18 '18 at 8:42

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.