0
votes

I created a new Product drop-down attribute (Typ) programmatically :

    $eavSetupFactory->addAttribute(
            Product::ENTITY,
            $name,
            [
                'type' => 'int',
                'label' => $name,
                'input' => 'select',
                'required' => true,
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                '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' => TAC\Migration\Setup\Configuration\Source\TypOptionProvider::class
            ]
        );

here is the TypOptionProvider class :

    <?php
namespace BAG\Migration\Setup\Configuration\Source;
class TypOptionProvider extends OptionProvider
{
protected $eavConfig;

public function __construct( \Magento\Eav\Model\Config $eavConfig){
    $this->eavConfig = $eavConfig;
}

public function getAllOptions()
{
    $factory = $this->eavConfig->getAttribute(\Magento\Catalog\Model\Product::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

2

2 Answers

0
votes

I don't see why you need "extends OptionProvider"

try change to "extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource"

0
votes

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.