How to update product – specific attribute value in magento2

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.

When you use this way there is a chance to get delay while updating the values (apprx for one product 40 to 50 sec it may take). In our case we want to update only one attribute value. To render enter collection & update the value might be will take some ms delay. To update only one attribute we can go with below way. Here for demo we will update the Width of product attribute value as like below ways

Ex: 

$item->setWidth(10);

$item->save();

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

In this method we have to pass 3 parameters.

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

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

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

Refer following file path : Magento\Catalog\Model\Product\Action

 

Save Environment ! Save Earth !!!

Leave a Reply