I created a new Product drop-down attribute (Typ) programmatically :
$eavSetupFactory->addAttribute(
Product::ENTITY,
$name,
[
'type' => 'int',
'label' => $name,
'input' => 'select',
'required' => true,
'backend' => 'MagentoEavModelEntityAttributeBackendArrayBackend',
'global' => ScopedAttributeInterface::SCOPE_STORE,
'visible' => true,
'searchable' => true,
'filterable' => true,
'filterable_in_search' => true,
'comparable' => false,
'visible_on_front' => true,
'unique' => false,
'group' => 'General',
'is_used_in_grid' => true,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => true,
'user_defined' => true,
'set' => 'Default',
'source' => TACMigrationSetupConfigurationSourceTypOptionProvider::class
]
);
here is the TypOptionProvider class :
<?php
namespace BAGMigrationSetupConfigurationSource;
class TypOptionProvider extends OptionProvider
{
protected $eavConfig;
public function __construct( MagentoEavModelConfig $eavConfig){
$this->eavConfig = $eavConfig;
}
public function getAllOptions()
{
$factory = $this->eavConfig->getAttribute(MagentoCatalogModelProduct::ENTITY, 'Typ');
if (!$this->_options) {
$this->_options = [
['label' => __('typ1'), 'value' => 1],
['label' => __('typ2'), 'value' => 2],
['label' => __('typ3'), 'value' => 3],
['label' => __('typ4'), 'value' => 4],
['label' => __('typ5'), 'value' => 5]
];
}
return $this->_options;
}
}
The first problem : The options are not shown in the attribute page :
The second problem : The newly added options for the attribute from Magento backend are not shown in the attribute options Drop-down list
for this particular problem I have some dougbts that the way I implemented getAllOptions() is the reason I’m getting this issue. I am new to Magento world, could you please tell me how I should Implement it in order to get the newly added options.
3
Answers
I don’t see why you need “extends OptionProvider”
try change to “extends MagentoEavModelEntityAttributeSourceAbstractSource”
You could refer to the full example here:
https://devdocs.magento.com/videos/fundamentals/add-new-product-attribute/
However, I have the same issue as your "The first problem". But you could update the array inside
getAllOptions
if you want to add/remove options from the dropdown menu later.