I take from https://github.com/magento/magento2/blob/2.3/dev/tests/integration/testsuite/Magento/Swatches/_files/product_visual_swatch_attribute.php like example, but my own color attribute patch didn’t create any options if I add those parameters. This is my code:
<?php
namespace VendorModuleSetupPatchData;
use MagentoCatalogModelProduct;
use MagentoEavModelEntityAttributeScopedAttributeInterface;
use MagentoEavModelEntityAttributeSourceTable;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupPatchDataPatchInterface;
use VendorModuleModelAttributeBackendColor as Backend;
use VendorModuleModelAttributeFrontendColor as Frontend;
class AddColorAttribute implements DataPatchInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* @param ModuleDataSetupInterface $moduleDataSetup
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory,
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* @inheritdoc
*/
public function apply()
{
$this->moduleDataSetup->startSetup();
$eavSetup = $this->eavSetupFactory->create();
$eavSetup->addAttribute(Product::ENTITY, 'visual_swatch_color_attribute', (
'type' => 'int',
'label' => 'Visual Swatch Color Attribute',
'input' => 'select',
'frontend' => Frontend::class,
'backend' => Backend::class,
'source' => Table::class,
'required' => false,
'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => true,
'visible_on_front' => true,
'used_in_product_listing' => true,
'is_html_alowed_on_front' => true,
'unique' => false,
'swatch_input_type' => 'visual',
'swatchvisual' => (
'value' => (
'Obsidian' => '#3a322d',
'Pure' => '#0000ff',
'Crimson' => '#dc143c',
'Emerald' => '#50c878',
'Ametyst' => '#9966cc',
),
),
'optionvisual' => (
'values' => (
'Obsidian' => ('Obsidian'),
'Pure' => ('Pure'),
'Crimson' => ('Crimson'),
'Emerald' => ('Emerald'),
'Ametyst' => ('Ametyst'),
),
),
'default' => '1'
));
$eavSetup->addAttributeToGroup(
MagentoCatalogModelProduct::ENTITY,
'Default',
'Product Details',
'visual_swatch_color_attribute',
18
);
$this->moduleDataSetup->endSetup();
}
/**
* @inheritdoc
*/
public static function getDependencies(): array
{
return ();
}
/**
* @inheritdoc
*/
public function getAliases(): array
{
return ();
}
}