3

select attribute programmatically in Magento 2, is there any way to make the options specified in the code as default so that whenever I create a product I don't have to select options manually.

Following is the code that I have used.

$eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'my_custom_attribute_code',
        [
            'type' => 'text',
            'label' => 'My Custom Attribute',
            'input' => 'multiselect',
            'required' => false,
            'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'global' => \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_GLOBAL,
            'visible' => true,
            'user_defined' => false,
            'searchable' => false,
            'filterable' => true,
            'filterable_in_search' => true,
            'comparable' => false,
            'visible_on_front' => false,
            'unique' => false,
            'group' => 'General',
            'is_used_in_grid' => true,
            'is_visible_in_grid' => false,
            'is_filterable_in_grid' => true,
            'option' => [
                'values' => [
                    'Left',
                    'Right',
                    'Up',
                    'Down',
                    'All'
                ]
            ]
        ]
    );

2 Answers 2

5

You need to add 'default' => 0 property in your code : it means you are giving this value by default so you don'need to add manually

Try below code :

$eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'my_custom_attribute_code',
            [
                'type' => 'text',
                'backend' => '',
                'frontend' => '',
                'label' => 'My Custom Attribute',
                'input' => 'multiselect',
                'class' => '',
                'source' => '',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => 0,
                'searchable' => true,
                'filterable' => true,
                'comparable' => true,
                'visible_on_front' => true,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );
1
  • can i use this 'default' => [0,1] with array ? Dec 1, 2017 at 14:29
-1

you try this

$eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'my_custom_attribute_code',
            [
                'type' => 'text',
                'backend' => '',
                'frontend' => '',
                'label' => 'My Custom Attribute',
                'input' => 'multiselect',
                'class' => '',
                'source' => '',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => 0,
                'searchable' => true,
                'filterable' => true,
                'comparable' => true,
                'visible_on_front' => true,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );

I hop this code helpful for you

1
  • so you added 'default' => 0 , will that make all the options default?
    – Nausif
    Nov 10, 2016 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.