Question

M2 Enterprise uses a schedule method to update the price and special price. Would like to know how we can achieve the same functionality by coding programatically.

Found this Link but that does not have he solution.

We need to create a console command to update the scheduled price.

Was it helpful?

Solution

You can use the below code it worked for me. I used it on the Console commad as it will run in a cron job.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get('\Magento\Framework\App\State');
$productRepository = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface');
$versionManager = $objectManager->get('\Magento\Staging\Model\VersionManager');
$productStaging = $objectManager->get('\Magento\CatalogStaging\Api\ProductStagingInterface');
$updateRepository = $objectManager->get('\Magento\Staging\Api\UpdateRepositoryInterface');
$updateFactory = $objectManager->get('\Magento\Staging\Api\Data\UpdateInterface');
$timezone = $objectManager->get('\Magento\Framework\Stdlib\DateTime\Timezone');
$storeManger = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$storeId = $storeManger->getStore()->getId();

$timezonefill = $timezone->getConfigTimezone(\Magento\Store\Model\ScopeInterface::SCOPE_STORES, $storeId);
$currentDate = new \DateTime('now', new \DateTimeZone($timezonefill));
$date = $currentDate->format("Y-m-d");
$state->setAreaCode('frontend');


/** @var \Magento\Staging\Api\Data\UpdateInterface $schedule */
$schedule = $updateFactory;
$schedule->setName("Price Update #");
$schedule->setStartTime("2020-06-22 00:00:00");
$schedule->setEndTime("2020-07-23 23:59:59");
$stagingRepo = $updateRepository->save($schedule);

$versionManager->setCurrentVersionId($stagingRepo->getId());
$product = $productRepository->get("123456"); // Product SKU
$product->setPrice("10"); // Price
$productStaging->schedule($product, $stagingRepo->getId());
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top