1

I have create product attribute of type select select with below configurations:

$setup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, 'attribute_code', [
            'attribute_model' => NULL,
            'backend' => '',
            'type' => 'int',
            'table' => '',
            'frontend' => '',
            'input' => 'select',
            'label' => 'Custom Attribute',
            'frontend_class' => '',
            'source' => '',
            'required' => true,
            'user_defined' => '1',
            'default' => 0,
            'unique' => '0',
            'note' => '',
            'input_renderer' => NULL,
            'global' => '1',
            'visible' => '1',
            'searchable' => '0',
            'filterable' => '1',
            'comparable' => '1',
            'visible_on_front' => '1',
            'is_html_allowed_on_front' => '0',
            'is_used_for_price_rules' => '1',
            'filterable_in_search' => '1',
            'used_in_product_listing' => '0',
            'used_for_sort_by' => '0',
            'is_configurable' => '1',
            'position' => '1',
            'wysiwyg_enabled' => '0',
            'used_for_promo_rules' => '1',
            'option' =>
                array(
                    'values' => $options,
                ),
            ]
        );

How to set default value of this custom attribute?

  • 1
    Try this => default' => '1', – Abdul May 28 '18 at 6:44
  • How to mention option value which needs to be set as default? – DEEP JOSHI May 28 '18 at 6:55
0

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

$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
       $eavSetup->addAttribute(
               \Magento\Catalog\Model\Product::ENTITY,
           'international',
           [
               'group' => 'General',
               'type' => 'int',
               'label' => 'International',
               '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'))
    );
   }
}
  • can we create without installer? – jafar pinjar Jan 8 at 5:45
  • If we need programmatically need to have installer scripts. – Sairam Sigirisetty Jan 8 at 15:54
  • can we not create with injecting classes? in controller – jafar pinjar Jan 9 at 2:40
  • I think we can create, I have not done but it may involves lot of efforts and may have obstacles when compared to setup scripts. – Sairam Sigirisetty Jan 9 at 12:52
  • what "sort_order" does here? – jafar pinjar Oct 9 at 11:33
0

For Magento 2.3.3 You can take new approach to add new attributes. Here is my answer to somehow simillar question: https://magento.stackexchange.com/a/294521/59161

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.