Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Special Price Programmatically In Magento

Tags:

magento

I am trying to write a script that will set a special price on a product with a start and an end date. When I run my script it does successfully set the special price, but the start and end date do not populate in the admin panel.

The code I am running is as follows:

$product = Mage::getModel('catalog/product')->load(114912);
$product->setSpecialPrice( ($product->getPrice() * .90)   );

$product->setSpecialFromDate('2010-11-01');
$product->setSpecialFromDateIsFormated(true);

$product->setSpecialToDate('2010-11-30');
$product->setSpecialToDateIsFormated(true);

$product->save();

Does anyone know what I am doing wrong here?

like image 635
Josh Pennington Avatar asked Nov 12 '10 18:11

Josh Pennington


People also ask

How do I get special value in Magento 2?

All you need to is inject \Magento\Catalog\Model\ProductRepository class in your construct. Using getSpecialPriceByProId($proId) function with pass product ID as Parameter, you can get the product's special price by product id.

How to update Special price in Magento 2?

Magento Open Source includes simple start and end date options in the Advanced Pricing options. Open the product in edit mode. Scroll down to the Price field, click Advanced Pricing, and enter the Special Price amount. ) to choose the Start Date and End Date for the special price promotion.

What is special price in Magento?

Final words. Special Price in Magento 2 enables you to create exclusive discounts for your products in a certain time period. It helps enormously by using FOMO as a way to urge people to act immediately. Setting up a Special Price in Magento 2 only requires a few steps as above, so you can do it easily.


1 Answers

I have just tried your code on my catalog and it worked with a little adjustement.

You should pay attention to the loaded store; it is not allowed to update certain product fields if the ADMIN store is not the currently loaded (Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);).

<?php

require_once('app/Mage.php');

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

...

$product->save();
?>
like image 61
Fabrizio D'Ammassa Avatar answered Sep 21 '22 05:09

Fabrizio D'Ammassa