Categories: How ToMagento 2

How to Update Product Attribute Value for All Product Quickly in Magento 2

Because all the customers don’t have the same requirements so store owner has to sell the same product with different options from which customer can pick the product according to their requirements. Having powerful CMS like Magento 2 allows the store owner to create, store and proceed your own custom attribute in the database. The attribute is nothing but one kind of the property of a product which can be used for fulfilling customer needs.
Several times it happens that you want thousands of products in your Magento 2 stores and you want to update attribute value of products. At that time, manually updating attribute value takes so much time and efforts. Generally, you have to load product collection and then you have to update all product attribute value by using a loop that takes an extensive amount of time. So we are back with another blog that allows you to update products attribute value in very less time compared to product collection update.

For example, you want to update weight (10gm) attribute for all the products in your Magento 2 stores. At that time you have to create one php script file using the below code in the root folder of your Magento 2 Installation.

<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
 
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
 
try
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
 $collection = $productCollection->addAttributeToSelect('*')
        ->load();
 $idArray = [];
 foreach ($collection as $product){
        $idArray[] =$product->getId();
 }
 $value = 10; //amount
    $productActionObject = $objectManager->create('Magento\Catalog\Model\Product\Action');
    $productActionObject->updateAttributes($idArray, array('weight' => $value), 0);
 
}
catch(\Exception $e)
{
   echo $e->getMessage();
   exit;
}

That’s it! You have successfully update product attribute value for your Magento 2 store products. If you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends.
And, Let us know if you are facing an issue while implementing this code.
Happy Updating!

Click to rate this post!
[Total: 25 Average: 3.5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

6 days ago