0

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.

1 Answer 1

0

Step:1 Create setup file following path {vendorname}/{modulename}/Setup/Patch/Data/CustomAttribute.php

<?php

namespace {vendorname}\{modulename}\Setup\Patch\Data;

use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class CustomAttribute implements DataPatchInterface {
    /**
     * ModuleDataSetupInterface
     *
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;
    /**
     * EavSetupFactory
     *
     * @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() {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->addAttribute('catalog_product', 'product_temp_attribute', [
            'type' => 'int',
            'backend' => '',
            'frontend' => '',
            'label' => 'Custom Atrribute',
            'input' => 'select',
            'class' => '',
            'source' => '{vendorname}\{modulename}\Model\Config\Source\CustomAttributeOptions',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' => true,
            'required' => true,
            'user_defined' => false,
            'default' => '',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique' => false,
            'apply_to' => ''
        ]);
    }
    /**
     * {@inheritdoc}
     */
    public static function getDependencies() {
        return [];
    }
    /**
     * {@inheritdoc}
     */
    public function getAliases() {
        return [];
    }
}

Step:-2 Create option file {vendorname}/{modulename}/Model/Config/Source/CustomAttributeOptions.php

<?php
namespace {vendorname}\{modulename}\Model\Config\Source;

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

class CustomAttributeOptions extends AbstractSource
{
    /**
     * Get all options
     *
     * @return array
     */
    public function getAllOptions()
    {
        if (null === $this->_options) {
            $this->_options=[
                ['label' => __('Option 1'), 'value' => 1],
                ['label' => __('Option 2'), 'value' => 2],
                ['label' => __('Option 3'), 'value' => 3]
            ];
        }
        return $this->_options;
    }
}

Now run the php bin/magento setup:upgrade command and see the eav_attribute table.

2
  • I was looking to use a string instead of integer for the option values. Jan 31 at 9:55
  • So you can change like that ['label' => __('Option 1'), 'value' => "value_1"], Jan 31 at 10:32

Your Answer

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

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