0

I want to create a second category assignment for my products which is limited to one category. The select should be similar to the default category assignment.

What I want is setting a main-category for each product which is used for tagging a product in diverse feeds.

I have absolutely no idea how to realize this and it would be great to get some help.

1

Step 1: You have to Create InstallData.php

Create file InstallData.php

We will start with the InstallData class which located in app/code/VendoreName/ModuleName/Setup/InstallData.php. The content for this file:

<?php
namespace VendoreName\ModuleName\Setup;

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;
    }

}

Step 2: Define the install() method

<?php

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{

}

Step 3: Create the category attribute

?php
namespace VendoreName\ModuleName\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
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,
            'mp_new_attribute',
            [
                'type'         => 'varchar',
                'label'        => 'Custom Attribute',
                'input'        => 'text',
                'sort_order'   => 100,
                'source'       => '',
                'global'       => 1,
                'visible'      => true,
                'required'     => false,
                'user_defined' => false,
                'default'      => null,
                'group'        => '',
                'backend'      => ''
            ]
            );
    }
}    

Step 4: Display the category attribute

The category UI Component is rendered with configuration from the category_form.xml file. All files with that name are merged together. As a result, We will add a field by creating a category_form.xml file in the app/code/VendoreName/ModuleName/view/adminhtml/ui_component/ directory.

<?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="mp_new_attribute">
            <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">Custom Main Attribute</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

All done, please run upgrade, flush cache and check the result.

**Flush Cache Run flush cache here**

**Run upgrade command line:**

Then go to Product > Categories from the admin to check the result.

This type you can easily create main_category attribute for a product.

  • Hello Harshad, thank you for your basic product attribute kind of text. But unfortunately it's just a textfield and not based on the actual categories in my store. I want to have a select like the default category assignment. – ecommerce Sep 11 at 12:25
  • There is a simple solution which Magento 2.x provides by default. Go to Products > Categories. Click on the (parent) category in which you want to show the products of the sub-category. Under Display Settings, set Yes for Anchor field. Save the category, flush Magento's cache and reindex. Please let me know if the solution works for you. – Harshad Bindra Sep 11 at 13:27
  • Harshad... no. This is not what I wanted. Please read my question. I want to set one specific main category for my product. I want to select the main category by a select similiar to the default magento ones. – ecommerce Sep 12 at 6:02

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.