0

My extension I'm working on requires a couple of product attributes to function so I added them to the install scripts. One of the attributes is a select attribute and I thought it'd be a good idea to pre-populate some starting options. Unfortunately doing this seems to make the attribute options immutable and I need to be able to add and remove these options at a later date like you would with a normal select attribute.

The part of the install script in question is this:

if (version_compare($context->getVersion(), '0.0.5') < 0) {
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

    $eavSetup->removeAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'google_shopping_taxonomy'
    );

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'google_shopping_taxonomy',
        [
            'type' => 'int',
            'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'frontend' => '',
            'label' => 'Google Shopping Taxonomy',
            'input' => 'select',
            'class' => 'select',
            'source' => 'Tainted\Googleshopping\Model\Entity\Attribute\Source\Taxonomy',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'default' => '',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => false,
            'unique' => false,
            'apply_to' => ''
        ]
    );
};

And the Taxonomy it references in the source looks like this:

namespace Tainted\Googleshopping\Model\Entity\Attribute\Source;

use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;

class Taxonomy extends AbstractSource
{
    public function getAllOptions()
    {
        return [
            '0' => [
                'label' => 'Furniture > Outdoor Furniture > Outdoor Furniture Sets',
                'value' => '0'
            ],
            '1' => [
                'label' => 'Home & Garden > Kitchen & Dining > Kitchen Appliances > Outdoor Grills',
                'value' => '1'
            ]
        ];
    }
}

Is there a way to modify the install script so that it does use the default options but also allows additional options to be added to the attribute in future?

1 Answer 1

2

Try adding your attribute like this:

$eavSetup->addAttribute(
    \Magento\Catalog\Model\Product::ENTITY,
    'google_shopping_taxonomy',
    [
        'type' => 'int',
        'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
        'frontend' => '',
        'label' => 'Google Shopping Taxonomy',
        'input' => 'select',
        'class' => 'select',
        'source' => '\Magento\Eav\Model\Entity\Attribute\Source\Table'
        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
        'visible' => true,
        'required' => false,
        'user_defined' => true,
        'default' => '',
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'visible_on_front' => false,
        'used_in_product_listing' => false,
        'unique' => false,
        'apply_to' => '',
        'option'     => array (
            'values' => array(
                0 => 'Furniture > Outdoor Furniture > Outdoor Furniture Sets',
                1 => 'Home & Garden > Kitchen & Dining > Kitchen Appliances > Outdoor Grills',
            )
         ),
    ]
);

Notice I removed the source

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.