4

I have a problem updating the special price dates of a simple product. I've tried 2 versions of code, but without results. In both cases the special price will be update, but no affect to the start and end dates.

Version 1

$set_product = $objectManager->create('Magento\Catalog\Model\Product')->load($idSimpleProduct);
$set_product->setSpecialPrice(120.00); // this work
$set_product->setSpecialFromDate('1-1-2018'); //also tried with d/m/Y, m/d/Y, Y-m-d
$set_product->setSpecialToDate('2-2-2018');
$set_product->getResource()->saveAttribute($set_product, 'special_price');
$set_product->getResource()->saveAttribute($set_product, 'special_from_date');
$set_product->getResource()->saveAttribute($set_product, 'special_to_date');
$set_product->save();

Version 2

$set_product = $objectManager->create('Magento\Catalog\Model\Product')->load($idSimpleProduct);
$set_product->setCustomAttributes(array(
            'special_from_date' => date("m-d-Y", strtotime($promoFromDate)), 
            'special_to_date' => date("m-d-Y", strtotime($promoToDate)), 
            'special_price' => $promoPrice
       )
);
$set_product->save();

Where I wrong? Thanks in advance

0

The correct date format is:

$product1->setSpecialFromDate(date('Y-m-d H:i:s'));

Taken from \Magento\Catalog\Model\ResourceModel\Product\Indexer\Eav\SourceTest::testReindexMultiselectAttribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.