Magento 2 Update product attribute value

This article is about Magento 2 – Update product attribute value . Updating product attribute value can be tricky sometimes. In this tutorial i will try to explain it swiftly and in a simple way. There can be various conditions in this matter. Like if someone wants to update the attribute values one by one or as a whole. Here, we are looking t update only one attribute value.

We can set all the values into one object (also we can use set for each attribute) & using set method we can save the product attribute with the help of productRepository or product model.

Furthermore When we use this method, there is a chance to get delays while updating the values like it may take 40 to 50 sec approx for one product . In our case we want to update only one attribute value. To render entire collection & updating the value might will take some ms delay.
So to update only one attribute value, we can do so by using the following code.

Consider the example here.

$item->setWidth(10);

$item->save();

We can use “updateAttributes” method to update Specific Attribute for product instead of updating all the update.

Here we have to pass 3 parameters.

Ex: $productIds , $attrData, $storeId
$objectManager->get(‘Magento\Catalog\Model\Product\Action’)

->updateAttributes( [$item],[‘width’ => 10],  $YourStoreID );

Similarly

$this->action->updateAttributes([$productObj->getId()], [‘Yourattribute_code’ => ‘Yourvalue’], $StoreId);

I am also providing the path for reference, it may vary depending upon your settings.

Magento\Catalog\Model\Product\Action

That’s it from this tutorial. I strongly believe there is always room for improvement.So i am open for any suggestion and feed back. Please feel free to leave hat you are thinking in the comments section below. Cheers.

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!

Leave a Reply