Introduction Imagine similar situation as I described in the previous article Magento 2: How to Change Category Attribute Value Programmatically
But now we need to change product attribute setting.
And, as in previuos example, we can do it via admin panel.
Buuut… would be better…
Solution To use Data Patch.
<?php declare(strict_types=1); namespace Vendor\ModuleName\Setup\Patch\Data; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Catalog\Model\Product; class UpdateColorAttribute implements DataPatchInterface { /** @var ModuleDataSetupInterface */ private ModuleDataSetupInterface $moduleDataSetup; /** * @var EavSetupFactory */ private EavSetupFactory $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() { /** @var EavSetup $eavSetup */ $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); $eavSetup->updateAttribute(Product::ENTITY, 'color', ['used_in_product_listing' => 1]); } /** * {@inheritdoc} */ public static function getDependencies() { return []; } /** * {@inheritdoc} */ public function getAliases() { return []; } } Explanation We use updateAttribute method of the EavSetup model to update color product attribute....