Created
July 24, 2023 13:35
-
-
Save 0-Sony/32b438c93b6541ad8149cd96edd0b246 to your computer and use it in GitHub Desktop.
Magento 2 : Update Product Attribute from DataPatch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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