I have created custom attribute which type is Select. That attribute is available inside custom eav entity type. I want to add attribute options inside that attribute. How to do that?
Any help would be appreciated.
- 
 webkul.com/blog/programmatically-add-options-attribute-magento2 please check this, if it is helpful for youShailesh Katarmal– Shailesh Katarmal2019年05月06日 13:34:59 +00:00Commented May 6, 2019 at 13:34
1 Answer 1
You can create options for select element while creating the attribute.
Let's take an example:
app/code/Namespace/Modulename/Setup/InstallData.php
<?php
namespace Namespace\Modulename\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;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
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, 'product_select_attribute');
 $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'product_custom_attribute');
 $statusOptions = 'Namespace\Modulename\Model\Config\Source\StatusOptions';
 $eavSetup->addAttribute(
 \Magento\Catalog\Model\Product::ENTITY,
 'product_select_attribute',
 [
 'group' => 'Custom Product Attribute',
 'type' => 'int',
 'backend' => '',
 'frontend' => '',
 'label' => 'Product Status',
 'input' => 'select',
 'class' => '',
 'source' => $statusOptions,
 'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
 'visible' => true,
 'required' => false,
 'user_defined' => false,
 'default' => '',
 'searchable' => false,
 'filterable' => false,
 'comparable' => false,
 'is_used_in_grid' => true,
 'visible_on_front' => false,
 'used_in_product_listing' => true,
 'unique' => false
 ]
 );
 }
}
In above code i used  'source' => $statusOptions, it is defind select option dynamically.
You need to create Model file for define our custom option of select box, $statusOptions = ‘Namespace\Modulename\Model\Config\Source\StatusOptions’;
create StatusOptions.php file under Model and define our enable and disable option value.
app/code/Namespace/Modulename/Model/Config/Source/StatusOptions.php
<?php
namespace Namespace\Modulename\Model\Config\Source;
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
class StatusOptions extends AbstractSource
{
 /**
 * Get all options
 *
 * @return array
 */
 public function getAllOptions()
 {
 if (null === $this->_options) {
 $this->_options=[
 ['label' => __('Enable'), 'value' => 1],
 ['label' => __('Disable'), 'value' => 0]
 ];
 }
 return $this->_options;
 }
}
Run below command :
php bin/magento setup:upgrade
php bin/magento cache:flush
php bin/magento indexer:reindex
Update :
Change below function code for dynamic data
public function getAllOptions()
{
 $collection = $this->CollectionFactory->create();
 foreach ($collection as $item) {
 $this->_options[] = [
 'label' => __($item['title']),
 'value' => $item['id'],
 ];
 }
 return $this->_options;
}
I hope it helps!
- 
 Is it possible to add from admin panel? as like product attribute.Emipro Technologies Pvt. Ltd.– Emipro Technologies Pvt. Ltd.2019年05月06日 13:29:55 +00:00Commented May 6, 2019 at 13:29
- 
 Because, I want to make it dynamically so.Emipro Technologies Pvt. Ltd.– Emipro Technologies Pvt. Ltd.2019年05月06日 13:30:07 +00:00Commented May 6, 2019 at 13:30
- 
 If you want to modify select option value then you can change inStatusOptions.phpfileChirag Patel– Chirag Patel2019年05月06日 13:32:41 +00:00Commented May 6, 2019 at 13:32
- 
 And you may write dynamic logic insideStatusOptions.phpChirag Patel– Chirag Patel2019年05月06日 13:33:15 +00:00Commented May 6, 2019 at 13:33
- 
 Hmmm...!!! Maybe possible.Emipro Technologies Pvt. Ltd.– Emipro Technologies Pvt. Ltd.2019年05月06日 13:37:55 +00:00Commented May 6, 2019 at 13:37