Set Special Price From Date and Special Price To Date programmatically in Magento2

<?php

use Magento\Framework\App\Bootstrap;
require '/srv/users/serverpilot/apps/serverpilot/public/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$now = new \DateTime();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$skustring = '5555318223,5555318250,';
$explod = explode(',', $skustring);
foreach ($explod as $value) {
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $_product = $objectManager->create('Magento\Catalog\Model\Product')->loadByAttribute('sku', $value);

    if ($_product) {
        $_children = $_product->getTypeInstance()->getUsedProducts($_product);

        foreach ($_children as $child) {
            $_cproduct = $objectManager->create('Magento\Catalog\Model\Product')->loadByAttribute('sku', $child->getSku());
            $_cproduct->setSpecialToDate('2029-10-29 00:00:00');
            $_cproduct->save();
            echo '==' . $value;
        }
    }
}

?>

Comments