How to Update Product Attribute Value Programmatically in Magento 2
Magento 2 CMS 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.
One can create a product attribute in Magento 2 or use the default attribute of Magento 2.
The admin can update product attributes in bulk in Magento 2 or do it programmatically with the method given here.
Update product attribute value programmatically in Magento 2 when, for instance, there is a price change, offer discounts, change product labels, etc., using the below method.
Steps to Update Product Attribute Value Programmatically in Magento 2
Create Data.php file at Vendor\Extension\Helper and use the below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php namespace Vendore\Extension\Helper; use Magento\Catalog\Model\Product\Action as ProductAction; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Store\Model\StoreManagerInterface; class Data extends AbstractHelper { protected $messageManager; private $productCollection; private $productAction; private $storeManager; public function __construct( Context $context, CollectionFactory $collection, ProductAction $action, StoreManagerInterface $storeManager ) { $this->productCollection = $collection; $this->productAction = $action; $this->storeManager = $storeManager; parent::__construct($context); } public function setAttributeData($value) { try { $collection = $this->productCollection->create()->addFieldToFilter('*'); $storeId = $this->storeManager->getStore()->getId(); $ids = []; $i = 0; foreach ($collection as $item) { $ids[$i] = $item->getEntityId(); $i++; } $this->productAction->updateAttributes($ids, array('attribute_code' => $value), $storeId); } catch (\Exception $e) { $this->messageManager->addError($e->getMessage()); } } } |
Here, pass the updated value to $value.
That’s all!
If you have any doubts, do mention them in the Comments section below.
I would be glad to help.
Feel free to share the solution with Magento community via social media.
Thank You.
Related Posts:
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
6 Comments
it’s not working for a single value, do you have any other solution for it!!!!!
Hello Prince,
The above solution is working proper from our end.
Please check the whole solution again and check your code.
Thank You
Hi! If i have not a one $value but many $values for instance a prices $values?
Hello,
You can update only one value, not multiple values.
Thanks.
This helper runs automatically and update for all products ?
Hello,
No, the helper is not called automatically.
You have to create an object of the helper and call its setAttributeData() function, then update all the products.
Thank You.