2

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?

asked May 28, 2018 at 6:34
2
  • 1
    Try this => default' => '1', Commented May 28, 2018 at 6:44
  • How to mention option value which needs to be set as default? Commented May 28, 2018 at 6:55

5 Answers 5

3

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'))
 );
 }
}
Ashish Viradiya
1,5802 gold badges20 silver badges39 bronze badges
answered May 28, 2018 at 11:02
5
  • can we create without installer? Commented Jan 8, 2019 at 5:45
  • If we need programmatically need to have installer scripts. Commented Jan 8, 2019 at 15:54
  • can we not create with injecting classes? in controller Commented Jan 9, 2019 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. Commented Jan 9, 2019 at 12:52
  • what "sort_order" does here? Commented Oct 9, 2019 at 11:33
1

Follow the Below Steps:-

Create registration.php in your module

<?php
/**
 * Register your module
 */
\Magento\Framework\Component\ComponentRegistrar::register(
 \Magento\Framework\Component\ComponentRegistrar::MODULE,
 'Test_Demo',
 __DIR__
);

Create etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
 <module name="Test_Demo" setup_version="0.0.1">
 </module>
</config>

Create Setup/InstallData.php

<?php
/**
 * Creating Product attribute
 */
namespace Test\Demo\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
 * Class InstallData
 * @package Test\Demo\Setup
 */
class InstallData implements InstallDataInterface
{
 /**
 * EAV setup factory
 *
 * @var EavSetupFactory
 */
 private $eavSetupFactory;
 /**
 * Init
 *
 * @param EavSetupFactory $eavSetupFactory
 */
 public function __construct(EavSetupFactory $eavSetupFactory)
 {
 $this->eavSetupFactory = $eavSetupFactory;
 }
 /**
 * {@inheritdoc}
 */
 public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
 {
 /** @var EavSetup $eavSetup */
 $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
 $eavSetup->addAttribute(
 \Magento\Catalog\Model\Product::ENTITY,
 'test_demo',
 [
 'type' => 'varchar',
 'label' => 'Test Demo TextBox',
 'input' => 'text',
 'required' => false,
 'sort_order' => 3,
 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
 'group' => 'General Information',
 ]
 );
 }
}

Run :- php bin/magento setup:upg Done!!

answered Dec 31, 2019 at 4:54
1

In Magento 2, Frist You Have to Create vendor/module/registration.php file

<?php
use \Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(ComponentRegistrar::MODULE, '<vendor>_<module>', __DIR__);

Create vendor/module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
 <module name="<vendor>_<module>" setup_version="0.0.1">
 </module>
</config>

Create vendor/module/Setup/InstallData.php

<?php
namespace vendor\module\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
 /**
 * EAV setup factory
 *
 * @var EavSetupFactory
 */
 private $eavSetupFactory;
 /**
 * Init
 *
 * @param EavSetupFactory $eavSetupFactory
 */
 public function __construct(EavSetupFactory $eavSetupFactory)
 {
 $this->eavSetupFactory = $eavSetupFactory;
 }
 /**
 * {@inheritdoc}
 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
 */
 public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
 {
 /* @var EavSetup $eavSetup /
 $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
 /**
 * Add attributes to the eav/attribute
 */
 $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'call_for_price_active');
 $eavSetup->addAttribute(
 \Magento\Catalog\Model\Product::ENTITY, 'text_demo',
 [
 'type' => 'varchar',
 'backend' => '',
 'frontend' => '',
 'label' => 'Text Demo',
 'input' => 'boolean',
 'class' => '',
 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
 'visible' => true,
 'required' => false,
 'user_defined' => false,
 'default' => 0,
 'searchable' => false,
 'filterable' => false,
 'comparable' => false,
 'visible_on_front' => false,
 'used_in_product_listing' => true,
 'unique' => false
 ]
 );
 }
}
answered Jun 8, 2020 at 11:15
1
  • Why remove an attribute? Commented Aug 12, 2021 at 13:20
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

answered Nov 4, 2019 at 14:00
0

There are four major steps to create custom product attributes programmatically:

1: Create the Setup File InstallData.php

 <?php
namespace Company\Mymodule\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
 private $eavSetupFactory;
 public function __construct(EavSetupFactory $eavSetupFactory)
 {
 $this->eavSetupFactory = $eavSetupFactory;
 }
 
}

2: Define the install() Method

3: Create Product Attribute

4: Upgrade

For more details, visit this blog:

Magento 2 Add Product Attribute Programmatically

Mohit Patel
4,0484 gold badges27 silver badges57 bronze badges
answered Oct 16, 2020 at 6:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.