Magento 1 : The code for change price of product, you can put on controller, model, helper or any where
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//load product by id $product = Mage::getModel('catalog/product')->load($productId); //change base price if($product->getId()){ //increase base price + 5 $newPrice = $product->getPrice()+5; $product->setPrice($newPrice); $product->save(); } //change special price if($product->getSpecialPrice()){ //increase special price + 5 $newPrice = $product->getSpecialPrice()+5; $product->setSpecialPrice($newPrice); $product->save(); } |
Magento 2 : To change price of product you need to follow these functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//instance of object manager $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //create product model via object manager $product = $objectManager->create('Magento\Catalog\Model\Product'); //load product by id $product->load($productId) //change base price if($product->getId()){ //increase base price + 5 $newPrice = $product->getPrice()+5; $product->setPrice($newPrice); $product->save(); } //change special price if($product->getSpecialPrice()){ //increase special price + 5 $newPrice = $product->getSpecialPrice()+5; $product->setSpecialPrice($newPrice); $product->save(); } |
Thank for reading this post, Hope it helps.