Skip to content

how to change product attribute’s frontend input to dropdown programmatically

i have tried following but it’s not working.

<?php

namespace VendorExtensionSetupPatchData;

use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupPatchDataPatchInterface;
use MagentoFrameworkSetupPatchPatchRevertableInterface;

class UpdateProductAttribute implements DataPatchInterface, PatchRevertableInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Constructor
     *
     * @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->getConnection()->startSetup();
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

        // Get entity type ID for catalog products
        $entityType = $eavSetup->getEntityTypeId('catalog_product');

        // Check if the attribute exists
        $attribute = $eavSetup->getAttribute($entityType, 'color');
        
        if ($attribute) {
            // Log current frontend_input type
            echo "Current frontend_input type: " . $attribute['frontend_input'] . "n";

            // Update the 'color' attribute's frontend_input type to 'select'
            $eavSetup->updateAttribute($entityType, 'color', 'frontend_input', 'select');

            // Log success message
            echo "Successfully changed 'color' attribute frontend_input to 'select'.n";
        } else {
            echo "Attribute 'color' does not exist.n";
        }

        $this->moduleDataSetup->getConnection()->endSetup();
    }

    
    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }
}