PHP

Magento2: How to update the product price programatically

PHP

Magento2: How to update the product price programatically

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!