0

enter image description here

I added a select category in product form but it doesn't work.After selecting categories and saving the selections are not stored. Please help me with this problem. Maybe i'm missing something someone please give me a more complete description .Thank .

file product_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="product-details">
        <field name="main_categories">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Magento\Catalog\Ui\Component\Product\Form\Categories\Options</item>
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Main Category</item>
                    <item name="componentType" xsi:type="string">field</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="component" xsi:type="string">Magento_Catalog/js/components/new-category</item>
                    <item name="elementTmpl" xsi:type="string">ui/grid/filters/elements/ui-select</item>
                    <item name="dataScope" xsi:type="string">data.main_categories</item>
                    <item name="filterOptions" xsi:type="boolean">true</item>
                    <item name="showCheckbox" xsi:type="boolean">true</item>
                    <item name="disableLabel" xsi:type="boolean">true</item>
                    <item name="multiple" xsi:type="boolean">true</item>
                    <item name="levelsVisibility" xsi:type="number">1</item>
                    <item name="sortOrder" xsi:type="number">70</item>
                    <item name="required" xsi:type="boolean">true</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                    <item name="listens" xsi:type="array">
                        <item name="${ $.namespace }.${ $.namespace }:responseData" xsi:type="string">setParsed</item>
                    </item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>
Setup InstallData.php

....................
   public function install(
      ModuleDataSetupInterface $setup,
      ModuleContextInterface $context
    ) {
      /** @var EavSetup $eavSetup */
      $eavSetup = $this->_eavSetupFactory->create(['setup' => $setup]);
      $setup = $this->categorySetupFactory->create(['setup' => $setup]);         
       $eavSetup->addAttribute(
              \Magento\Catalog\Model\Product::ENTITY,
              'main_categories',
              [
                  'group' => 'General',
                  'type' => 'varchar',
                  'label' => 'Main Category',
                  'input' => 'text',
                  'source' => '',
                  'frontend' => '',
                  'backend' => '',
                  'required' => false,
                  'sort_order' => 11,
                  'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                  'is_used_in_grid' => false,
                  'is_visible_in_grid' => false,
                  'is_filterable_in_grid' => false,
                  'visible' => true,
                  'is_html_allowed_on_front' => true,
                  'visible_on_front' => true
              ]
          );
    }

0

If you just want to add tree input renderer you can add this code in your ui component form xml

<field name="parent">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Magento\Catalog\Ui\Component\Product\Form\Categories\Options</item>
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Parent Category</item>
            <item name="componentType" xsi:type="string">field</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="component" xsi:type="string">Magento_Catalog/js/components/new-category</item>
            <item name="elementTmpl" xsi:type="string">ui/grid/filters/elements/ui-select</item>
            <item name="dataScope" xsi:type="string">data.parent</item>
            <item name="filterOptions" xsi:type="boolean">true</item>
            <item name="showCheckbox" xsi:type="boolean">false</item>
            <item name="disableLabel" xsi:type="boolean">true</item>
            <item name="multiple" xsi:type="boolean">false</item>
            <item name="levelsVisibility" xsi:type="number">1</item>
            <item name="sortOrder" xsi:type="number">20</item>
            <item name="required" xsi:type="boolean">true</item>
            <item name="validation" xsi:type="array">
                <item name="required-entry" xsi:type="boolean">true</item>
            </item>
            <item name="listens" xsi:type="array">
                <item name="${ $.namespace }.${ $.namespace }:responseData" xsi:type="string">setParsed</item>
            </item>
        </item>
    </argument>
</field>```
2
  • i tried it but it doesn't do the save – nhungo duc May 21 at 11:13
  • your answer i read it in a few places but it didn't work so i asked this question – nhungo duc May 21 at 11:15

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.