Back to Top

Mass Update Products attribute in Magento 2

Updated 15 March 2024

This post demonstrates the most optimized method to update product attributes for a substantial number of products.

For instance, if you’ve added a new attribute with the attribute code ‘wk_shipping’ and need to assign 50 units to it across multiple products.

Use the following code to save attribute value for products:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$array_product = [1,2,3,4,5]; //product Ids
$value = 50; //amount
$productActionObject = $objectManager->create('Magento\Catalog\Model\Product\Action');
$productActionObject->updateAttributes($array_product, array('wk_shipping' => $value), 0);

The function updateAttributes prototype is as such:
/**
* Update attribute values for entity list per store
*
* @param array $productIds
* @param array $attrData
* @param int $storeId
* @return $this
*/

public function updateAttributes($productIds, $attrData, $storeId);

Please check the below images for reference. The value for the product attribute “wk_shipping” was initially set to $99. After running the above code, the value for the same attribute is updated to the given value, that is, $50 in our case.

Searching for an experienced
Magento 2 Company ?
Find out More

Before:

image-10

After:

image-11-1

I have tried to make a standalone script that is ready to use. You can improve the code by directly injecting the dependency of this class Magento\Catalog\Model\Product\Action in constructor. Hope, this may be helpful in your journey with Magento 2.

For any query, do let me know in the comment below. Thanks 🙂

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home