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?
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.
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.
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.
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();
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With