Skip to content

Instantly share code, notes, and snippets.

@0-Sony
Created July 24, 2023 13:35
Show Gist options
  • Save 0-Sony/32b438c93b6541ad8149cd96edd0b246 to your computer and use it in GitHub Desktop.
Save 0-Sony/32b438c93b6541ad8149cd96edd0b246 to your computer and use it in GitHub Desktop.
Magento 2 : Update Product Attribute from DataPatch
<?php
declare(strict_types=1);
namespace MyNamespace\MyModule\Setup\Patch\Data;
use Magento\Catalog\Api\Data\EavAttributeInterface;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\Patch\DataPatchInterface;
class UpdateProductAttribute implements DataPatchInterface
{
public function __construct(
private readonly EavSetupFactory $eavSetupFactory
) {
}
/**
* @inheritDoc
*/
public static function getDependencies(): array
{
return [];
}
/**
* @inheritDoc
*/
public function getAliases(): array
{
return [];
}
/**
* @inheritDoc
*/
public function apply(): void
{
$eavSetup = $this->eavSetupFactory->create();
$eavSetup->updateAttribute(
ProductAttributeInterface::ENTITY_TYPE_CODE, // Entity 'catalog_product'
ProductAttributeInterface::CODE_SKU, // your product attribute code
EavAttributeInterface::USED_FOR_SORT_BY, // product attribute field
1 // value
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment