Magento 2 - Create product attribute programmatically












1














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



$setup->addAttribute(MagentoCatalogModelProduct::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?










share|improve this question














bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 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
















1














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



$setup->addAttribute(MagentoCatalogModelProduct::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?










share|improve this question














bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 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














1












1








1







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



$setup->addAttribute(MagentoCatalogModelProduct::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?










share|improve this question













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



$setup->addAttribute(MagentoCatalogModelProduct::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?







magento2 product-attribute install-script






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 28 '18 at 6:34









DEEP JOSHIDEEP JOSHI

354424




354424





bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 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














  • 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








1




1




Try this => default' => '1',
– Abdul
May 28 '18 at 6:44




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




How to mention option value which needs to be set as default?
– DEEP JOSHI
May 28 '18 at 6:55










1 Answer
1






active

oldest

votes


















0














$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'international',
[
'group' => 'General',
'type' => 'int',
'label' => 'International',
'backend' => '',
'input' => 'select',
'wysiwyg_enabled' => false,
'source' => 'NamespaceModuleNameModelConfigSourceYesNo',
'required' => true,
'sort_order' => 15,
'global' => MagentoCatalogModelResourceModelEavAttribute::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    MagentoEavModelEntityAttributeSourceAbstractSource
{
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'))
);
}


}






share|improve this answer





















  • can we create without installer?
    – jafar pinjar
    18 mins ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f227679%2fmagento-2-create-product-attribute-programmatically%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'international',
[
'group' => 'General',
'type' => 'int',
'label' => 'International',
'backend' => '',
'input' => 'select',
'wysiwyg_enabled' => false,
'source' => 'NamespaceModuleNameModelConfigSourceYesNo',
'required' => true,
'sort_order' => 15,
'global' => MagentoCatalogModelResourceModelEavAttribute::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    MagentoEavModelEntityAttributeSourceAbstractSource
{
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'))
);
}


}






share|improve this answer





















  • can we create without installer?
    – jafar pinjar
    18 mins ago
















0














$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'international',
[
'group' => 'General',
'type' => 'int',
'label' => 'International',
'backend' => '',
'input' => 'select',
'wysiwyg_enabled' => false,
'source' => 'NamespaceModuleNameModelConfigSourceYesNo',
'required' => true,
'sort_order' => 15,
'global' => MagentoCatalogModelResourceModelEavAttribute::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    MagentoEavModelEntityAttributeSourceAbstractSource
{
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'))
);
}


}






share|improve this answer





















  • can we create without installer?
    – jafar pinjar
    18 mins ago














0












0








0






$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'international',
[
'group' => 'General',
'type' => 'int',
'label' => 'International',
'backend' => '',
'input' => 'select',
'wysiwyg_enabled' => false,
'source' => 'NamespaceModuleNameModelConfigSourceYesNo',
'required' => true,
'sort_order' => 15,
'global' => MagentoCatalogModelResourceModelEavAttribute::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    MagentoEavModelEntityAttributeSourceAbstractSource
{
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'))
);
}


}






share|improve this answer












$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'international',
[
'group' => 'General',
'type' => 'int',
'label' => 'International',
'backend' => '',
'input' => 'select',
'wysiwyg_enabled' => false,
'source' => 'NamespaceModuleNameModelConfigSourceYesNo',
'required' => true,
'sort_order' => 15,
'global' => MagentoCatalogModelResourceModelEavAttribute::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    MagentoEavModelEntityAttributeSourceAbstractSource
{
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'))
);
}


}







share|improve this answer












share|improve this answer



share|improve this answer










answered May 28 '18 at 11:02









Sairam SigirisettySairam Sigirisetty

1865




1865












  • can we create without installer?
    – jafar pinjar
    18 mins ago


















  • can we create without installer?
    – jafar pinjar
    18 mins ago
















can we create without installer?
– jafar pinjar
18 mins ago




can we create without installer?
– jafar pinjar
18 mins ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f227679%2fmagento-2-create-product-attribute-programmatically%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1300-talet

1300-talet

Display a custom attribute below product name in the front-end Magento 1.9.3.8