Skip to content

How to create custom Text Swatch product attribute using Data Patch in Magento 2

This code only saves the value (description).
But I want to save values in ADMIN and DESCRIPTION both, can anyone help me here:

 private function addVisualSwatchAttribute()
        {
            /** @var EavSetup $eavSetup */
            $eavSetup = $this->eavSetupFactory->create();
    
            $eavSetup->addAttribute(
                Product::ENTITY,
                'custom_visual_swatch',
                [
                    'type' => 'int',
                    'label' => 'Custom Visual Swatch',
                    'input' => 'swatch_text',
                    'source' => '',
                    'frontend' => '',
                    'backend' => '',
                    'required' => false,
                    'user_defined' => true,
                    'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
                    'visible' => true,
                    'searchable' => false,
                    'filterable' => true,
                    'comparable' => false,
                    'visible_on_front' => true,
                    'used_in_product_listing' => false,
                    'unique' => false,
                    'is_html_allowed_on_front' => true,
                    'note' => 'Custom Visual Swatch Attribute Note', // You can add a description here
                ]
            );
    
            // Add attribute options
            $optionsData = [
                ['label' => 'Option 1', 'value' => 'Option 1'],
                ['label' => 'Option 2', 'value' => 'Option 2'],
                ['label' => 'Option 3', 'value' => 'Option 3'],
                ['label' => 'Option 4', 'value' => 'Option 4'],
            ];
    
            foreach ($optionsData as $option) {
                $eavSetup->addAttributeOption([
                    'attribute_id' => $eavSetup->getAttributeId(Product::ENTITY, 'custom_visual_swatch'),
                    'value' => [
                        'option_' . $option['value'] => [$option['label']],
                    ],
                ]);
            }
    
        }

enter image description here