0

For my custom modules I need some new fields for product attributes in eav_attribute. Does anyone know how to add these fields?

1
  • Use product attribute and the values will be saved in type specific table like eav_entity_varchar etc.., Feb 27 '19 at 6:38
1

Use below code in: Vendor/Module/Setup/InstallData.php

$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
       $eavSetup->addAttribute(
               \Magento\Catalog\Model\Product::ENTITY,
           'attribute_code',
           [
               'group' => 'General',
               'type' => 'int',
               'label' => 'attribute_label',
               'backend' => '',
               'input' => 'select',
               'wysiwyg_enabled'   => false,
               'source' => 'Namespace\ModuleName\Model\Config\Source\YesNo',
               'required' => true,
               'sort_order' => 15,
               'global' => \Magento\Catalog\Model\ResourceModel\Eav
                           \Attribute::SCOPE_GLOBAL,
               'used_in_product_listing' => false,
               'visible_on_front' => false,
       ]
   );

   $setup->endSetup();

Source file mentioned above so by defaulty value will be set

class YesNo extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
    protected $_options;

    /**
     * getAllOptions
     *
     * @return array
     */
    public function getAllOptions()
    {
        if ($this->_options === null) {
            $this->_options = [
                ['value' => '0', 'label' => __('No')],
                ['value' => '1', 'label' => __('Yes')]
            ];
        }
        return $this->_options;
    }
    final public function toOptionArray()
    {
       return array(
        array('value' => '0', 'label' => __('No')),
        array('value' => '1', 'label' => __('Yes'))
    );
   }
}

Use Below Cose in: Vendor/Module/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="general">
        <field name="Your_attribute_name">
            <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                    <item name="formElement" xsi:type="string">boolean</item>
                    <item name="sortOrder" xsi:type="number">20</item>
                    <item name="label" xsi:type="string" translate="true">Your_Label</item>
                </item>
            </argument>
        </field>s
    </fieldset>
</form>
3
  • Thanks for the answers, but I dont need new attributes, I need to add new table fields in the table eav_attribute. Feb 27 '19 at 7:07
  • Ups, I understand now, instead of adding in the table eav_attribute, I add the required table fields as attributes, right? Feb 27 '19 at 7:15
  • yes you need to create attribute
    – Rk Rathod
    Feb 27 '19 at 7:25

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.