0

In my InstallData class I have the following code, it perfectly creates the attribute for me but this creates it as Dropdown and I need it to be Swatch Text.

What option am I missing?

$productTypes = [
        \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE,
        \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL,
        \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE,
        \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE,
    ];
    $productTypes = join(',', $productTypes);

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'web_volumen',
        [
            'type' => 'int',
            'group' => 'Module',
            'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
            'label' => 'VOLUMEN',
            'input' => 'select',
            'class' => '',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'default' => '',
            'searchable' => true,
            'filterable' => true,
            'comparable' => false,
            'visible_on_front' => true,
            'used_in_product_listing' => true,
            'apply_to' => $productTypes,
        ]
    );
0

There isn't any option missing in your code. I think there isn't a way to create a Text Swatch attribute directly. We need to create it as a Dropdown attribute first and then convert it to the Text Swatch attribute.

Here is how Magento does it for attribute size: https://github.com/magento/magento2-sample-data/blob/2.3.4/app/code/Magento/SwatchesSampleData/Model/Swatches.php#L87

I hope that it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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