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

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

8 hours ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

1 day ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

3 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

5 days ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

6 days ago

Best Beginners Guide to Shopify Balance Account

If you are a Shopify admin, using a Shopify Balance Account for your business revenue…

6 days ago