{"id":1529,"date":"2021-01-17T04:00:45","date_gmt":"2021-01-17T04:00:45","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/update-product-attribute-value-programmatically-magento-2\/"},"modified":"2025-07-17T10:08:02","modified_gmt":"2025-07-17T04:38:02","slug":"update-product-attribute-value-programmatically-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/update-product-attribute-value-programmatically-magento-2\/","title":{"rendered":"How to Update Product Attribute Value Programmatically in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2\u00a0CMS has a powerful feature of product attributes. It is a property of a product that describes the product more specifically. For example, price, color, weight, etc.<strong><br><\/strong><br>One can\u00a0<a href=\"https:\/\/meetanshi.com\/blog\/create-product-attribute-in-magento-2\/\">create a product attribute in Magento 2\u00a0\u00a0<\/a>or use the default attribute of Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The admin can&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/update-product-attributes-in-bulk-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">update product attributes in bulk in Magento 2<\/a>&nbsp;or do it programmatically with the method given here.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Update product attribute value programmatically in Magento 2&nbsp;when, for instance, there is a price change, offer discounts, change product labels, etc., using the below method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Update Product Attribute Value Programmatically in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><strong>Data.php<\/strong><\/em>&nbsp;file at&nbsp;<strong>Vendor\\Extension\\Helper<\/strong>&nbsp;and use the below code.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendore\\Extension\\Helper;\n\nuse Magento\\Catalog\\Model\\Product\\Action as ProductAction;\nuse Magento\\Catalog\\Model\\ResourceModel\\Product\\CollectionFactory;\nuse Magento\\Framework\\App\\Helper\\AbstractHelper;\nuse Magento\\Framework\\App\\Helper\\Context;\nuse Magento\\Store\\Model\\StoreManagerInterface;\n\nclass Data extends AbstractHelper\n{\n    protected $messageManager;\n    private $productCollection;\n    private $productAction;\n    private $storeManager;\n\n    public function __construct(\n        Context $context,\n        CollectionFactory $collection,\n        ProductAction $action,\n        StoreManagerInterface $storeManager\n    )\n    {\n        $this->productCollection = $collection;\n        $this->productAction = $action;\n        $this->storeManager = $storeManager;\n        parent::__construct($context);\n    }\n\n    public function setAttributeData($value)\n    {\n        try {\n            $collection = $this->productCollection->create()->addFieldToFilter('*');\n            $storeId = $this->storeManager->getStore()->getId();\n            $ids = [];\n            $i = 0;\n            foreach ($collection as $item) {\n                $ids[$i] = $item->getEntityId();\n                $i++;\n            }\n            $this->productAction->updateAttributes($ids, array('attribute_code' => $value), $storeId);\n\n        } catch (\\Exception $e) {\n            $this->messageManager->addError($e->getMessage());\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, pass the updated value to&nbsp;<strong>$value.<br><\/strong><br>That\u2019s all!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with Magento community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2\u00a0CMS has a powerful feature of product attributes. It is a property of a product that describes the product more specifically. For example, price,&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1529","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1529"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1529\/revisions"}],"predecessor-version":[{"id":14552,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1529\/revisions\/14552"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}