{"id":1519,"date":"2021-01-12T05:06:06","date_gmt":"2021-01-12T05:06:06","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/update-product-price-programmatically-magento-2\/"},"modified":"2025-05-22T11:14:37","modified_gmt":"2025-05-22T05:44:37","slug":"update-product-price-programmatically-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/update-product-price-programmatically-magento-2\/","title":{"rendered":"How to Update Product Price Programmatically in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">After you have successfully created a store,&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/create-new-root-category-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">created categories<\/a>,&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/create-simple-product-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">added products in Magento 2<\/a>, set up initial prices, and voila!, you are ready to market them and wait for the sales!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You priced the products choosing a strategy but selecting a optimal price strategy is a difficult task. There are many factors affecting the product prices, some of them being:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Fluctuation in market<\/li>\n\n\n\n<li>Arrival of new competitors<\/li>\n\n\n\n<li>Change in demand<\/li>\n\n\n\n<li>Inflation<\/li>\n\n\n\n<li>Increased production cost, and many more<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Product price updation is an ongoing process. You cannot pick a strategy, set a price, leave it and expecting to be profitable, and thus, you may require to update the product prices.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Manually updating the product prices from the admin is tiresome and time consuming task. Rather, you should&nbsp;<strong>update product price programmatically in Magento 2<\/strong>&nbsp;to quicken up the process. You can also&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/auto-update-currency-rates-in-magento-2\/\">update currency rates automatically<\/a>&nbsp;which will save your time rather than doing it manually.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here I\u2019ve come up with the solution to&nbsp;<em>update product price programmatically in Magento 2<\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Update Product Price Programmatically in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use the below code.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\n\n$productId = 1;\n$price = 100;\n$store = 1;\n\n$product = $objectManager->create('Magento\\Catalog\\Model\\Product')->load($productId);\n\ntry {\n    $product->setStoreId($store);\n    $product->setPrice($price);\n    $product->save();\n\n} catch (\\Exception $e) {\n    echo \"Error Id : \" . $productId;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This code will automatically update the price of a product with Id 1.&nbsp;<strong>setPrice&nbsp;<\/strong>function plays a vital role in updating the price.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Simple,,, Right?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with Magento 2 community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After you have successfully created a store,&nbsp;created categories,&nbsp;added products in Magento 2, set up initial prices, and voila!, you are ready to market them and&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1519","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1519"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1519\/revisions"}],"predecessor-version":[{"id":14558,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1519\/revisions\/14558"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}