4

I've create a module to add a custom attribute to my categories, however my code doesn't seem to be creating the attribute in my eav_attribute table, the field shows on my category pages but it doesn't save the values?

Can anyone advise why this isn't working.

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

<?php
namespace Autosmart\CategoryAttributes\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Catalog\Model\Category;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * @var EavSetupFactory
     */
private $eavSetupFactory;

/**
 *
 * @param EavSetupFactory $eavSetupFactory
 */
public function __construct(EavSetupFactory $eavSetupFactory)
{
    $this->eavSetupFactory = $eavSetupFactory;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    /** @var EavSetup $eavSetup */
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    $eavSetup->addAttribute(
        Category::ENTITY,
        'category_short_description',
        [
            'type' => 'text',
            'label' => 'Category Short Description',
            'input' => 'textarea',
            'visible_on_front' => true,
            'required' => false,
            'sort_order' => 1,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'General Information',
        ]
    );
}
}

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

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="autosmart_additonal_content">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Autosmart Additonal Content</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="sortOrder" xsi:type="number">100</item>
        </item>
    </argument>
    <field name="category_short_description">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">1</item>
                <item name="dataType" xsi:type="string">string</item>
                <item name="formElement" xsi:type="string">textarea</item>
                <item name="label" xsi:type="string" translate="true">Category Short Description</item>
                <item name="required" xsi:type="boolean">false</item>
            </item>
        </argument>
    </field>

</fieldset>

4 Answers 4

1

Managed to get this working now :)

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

<?php
namespace Autosmart\CategoryAttributes\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)
    {
        $setup->startSetup();
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        //Category Attribute Create Script
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            'category_short_description',
            [
                'group' => 'autosmart_category_fields',
                'label' => 'Category Short Description',
                'type'  => 'text',
                'input' => 'textarea',
                'required' => false,
                'sort_order' => 1,
                'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
                'used_in_product_listing' => true,
                'visible_on_front' => false
            ]
        );

        $setup->endSetup();
    }
}

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

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="autosmart_category_fields">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Autosmart Additional Settings</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="sortOrder" xsi:type="number">100</item>
        </item>
    </argument>
    <field name="category_short_description">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">1</item>
                <item name="dataType" xsi:type="string">string</item>
                <item name="formElement" xsi:type="string">textarea</item>
                <item name="label" xsi:type="string" translate="true">Category Short Description</item>
            </item>
        </argument>
    </field>
</fieldset>
</form>

Hope this helps anyone else having issues :)

EDIT -- Diff for the Lazy Web

enter image description here

enter image description here

6
  • what did u change
    – OZZIE
    Jul 18, 2017 at 14:46
  • I changed the two files mentioned above?
    – lky
    Jul 18, 2017 at 16:02
  • ok will use a diff tool then...
    – OZZIE
    Jul 19, 2017 at 7:45
  • This worked!!! Thanks! I pretty much had all this code, but something was off in my InstallData.php. I actually got to display this on the frontend too!!
    – Samyer
    Sep 21, 2018 at 15:09
  • Has anyone tried in M2.3.3? It worked in my 2.3.0, but value doesn't save in 2.3.3. Any idea? Nov 5, 2019 at 5:57
0

Magento 2 change custom category attribute value


<?php
//increase the max execution time
@ini_set('max_execution_time', -1);
//memory_limit
@ini_set('memory_limit', -1);

error_reporting(E_ALL);
ini_set('display_errors', '1');

use \Magento\Framework\App\Bootstrap;

include('app/bootstrap.php');

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        

$appState = $objectManager->get('\Magento\Framework\App\State');

$appState->setAreaCode('frontend');

$categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');

$categories = $categoryCollection->create();

$categories->addAttributeToSelect('*');

$categories->load();

if (count($categories) > 0):

        foreach($categories as $category):

            $catId = $category->getId();

                $category = $objectManager->create('Magento\Catalog\Model\CategoryFactory')->create()->setStoreId(0)->load($catId);
                $CategoryName = $category->getName();

                $metaTitle = "Buy ".$CategoryName." from My Site";

                $category->setMetaTitle($metaTitle);

                $category->save();


        endforeach;
     else: echo "No Results";
    endif;

?>
0

Answer is little different for me.

Re-run installation of eavSetup because adding attribute was failing

0
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->create('Magento\Catalog\Model\CategoryFactory')->create()->load($categoryId);    
$category->setCustomAttribute("test");
$category->getResource()->saveAttribute($category, 'custom_attribute');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.