Milind Daraniya

Magento2: How to update the product price programatically

Published June 30th, 2023 1 min read

Sometimes we need to update product price manually so today we will learn how to update product price programmatically in magento 2.

Here are the Steps to Update Product Price Programmatically in Magento 2

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productId = 10;
$price = 100;
$store = 1;

$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); // load the product by id

try {
    $product->setStoreId($store); // set store id
    $product->setPrice($price); // set product price
    $product->save();

} catch (\Exception $e) {
    echo "Error Id : " . $e->getMessage(); // print the error message
}

This code will work for you. don't forget clear page cache.

Thank You!