I've created a module to add a product attribute with a select input using a source for the options. I've used a data patch to add the attribute. I can see the patch in the patch_list
table but it's not showing in the eav_attribute
table after running php bin/magento setup:upgrade && php bin/magento cache:flush
.
Example/ImageSize/Setup/Patch/Data/AddImageSizeAttribute.php
<?php
namespace Example\ImageSize\Setup\Patch\Data;
use Example\ImageSize\Model\Product\Attribute\Source\ImageSizes;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use Zend_Validate_Exception;
class AddImageSizeAttribute implements DataPatchInterface, PatchVersionInterface
{
/**
* @var ModuleDataSetupInterface
*/
private ModuleDataSetupInterface $moduleDataSetup;
/**
* @var EavSetupFactory
*/
private EavSetupFactory $eavSetupFactory;
/**
* PatchInitial constructor.
* @param ModuleDataSetupInterface $moduleDataSetup
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* @return AddImageSizeAttribute|void
* @throws LocalizedException
* @throws Zend_Validate_Exception
*/
public function apply()
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->addAttribute(
Product::ENTITY,
'image_size',
[
'type' => 'varchar',
'label' => 'Image Size',
'input' => 'select',
'source' => ImageSizes::class,
'default' => 'standard',
'required' => false,
'user_defined' => true,
'global' => ScopedAttributeInterface::SCOPE_STORE,
'apply_to' => 'configurable',
'group' => 'General',
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true
]
);
}
/**
* @return array|string[]
*/
public static function getDependencies(): array
{
return [];
}
/**
* @return array|string[]
*/
public function getAliases(): array
{
return [];
}
/**
* @return string
*/
public static function getVersion(): string
{
return '0.0.1';
}
}
Example/ImageSize/Model/Product/Attribute/Source/ImageSizes.php
<?php
namespace Example\ImageSize\Model\Product\Attribute\Source;
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
use Magento\Eav\Model\Entity\Attribute\Source\SourceInterface;
use Magento\Framework\Data\OptionSourceInterface;
class ImageSizes extends AbstractSource implements OptionSourceInterface, SourceInterface
{
/**
* @return array|array[]|null
*/
public function getAllOptions(): ?array
{
if (!$this->_options) {
$this->_options = [
[
'label' => __('Standard'),
'value' => 'standard'
],
[
'label' => __('Large'),
'value' => 'large'
]
];
}
return $this->_options;
}
}
Update
I finally solved this by increasing the patch version number 0.0.1
to 0.0.2
, deleting the patch in patch_list
table and running php bin/magento setup:upgrade && php bin/magento cache:flush
.