1

I'm trying to figure out how to create custom attributes for categories and how to display these on the frontend within my .phtml files.

I've been following the tutorial on https://www.atwix.com/magento-2/adding-new-category-attributes/ which adds my custom attribute to the category edit screen within the admin, however it doesn't seem to save the values and I am unsure how to make the value display on the frontend.

I've created a module to handle this and I am trying to add some additional text and image attributes to my categories.

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

<?php
namespace Auto\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_icon',
        [
            'type' => 'varchar',
            'label' => 'Image',
            'input' => 'image',
            'backend' => 'Magento\Catalog\Model\Category\Attribute\Backend\Image',
            'required' => false,
            'sort_order' => 5,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'General Information',
        ]
    );
}
}

File: app/code/Auto/CategoryAttributes/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_icon">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">string</item>
                <item name="source" xsi:type="string">category</item>
                <item name="label" xsi:type="string" translate="true">Category Icon</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="formElement" xsi:type="string">fileUploader</item>
                <item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
                <item name="previewTmpl" xsi:type="string">Magento_Catalog/image-preview</item>
                <item name="required" xsi:type="boolean">false</item>
                <item name="sortOrder" xsi:type="number">40</item>
                <item name="uploaderConfig" xsi:type="array">
                    <item name="url" xsi:type="url" path="catalog/category_image/upload"/>
                </item>
            </item>
        </argument>
    </field>
</fieldset>

The above adds my image field to the category edit screen in the admin, however it doesn't upload the image :( "The file was not uploaded."

 {"error":"The file was not uploaded.","errorcode":666}

I've tried the solution mentioned on magento 2.1 create category attribute thumbnail however I still receive the same error?

Can anyone please advise how I can create category attributes and how I would display these on the frontend. I need to add both text and image attributes but just can't seem to get them to work, or save?

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.