skip to Main Content

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 :
enter image description here

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.

enter image description here

3

Answers


  1. I don’t see why you need “extends OptionProvider”

    try change to “extends MagentoEavModelEntityAttributeSourceAbstractSource”

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. $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' => '',
                    'option' => [
                        'values' => [
                            __('typ1')->getText(),
                            __('typ2')->getText(),
                            __('typ3')->getText(),
                            __('typ4')->getText(),
                            __('typ5')->getText()
                        ]
                    ]
            );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search