0

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.

2 Answers 2

2

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());
2
  • Do you know in which tables scheduled data is stored?
    – amitshree
    Commented Jul 12, 2021 at 11:05
  • You will find schedules in staging_update table. Each products has a created_in and updated_in timestamp (in catalog_product_entity) that tells Magento which product to pick.
    – Hackinet
    Commented Nov 19, 2021 at 7:53
1

I found I needed a tad more validation when it comes to these fickle scheduled updates:

<?php

/**
 * @param \Magento\Catalog\Model\Product $product
 * @param float $specialPrice
 * @param string $startTime (Y-m-d H:i:s)
 * @param string $endTime (Y-m-d H:i:s)
 * @param int $storeId
 * @return bool
 */
private function buildProductScheduledPriceUpdate(
    \Magento\Catalog\Api\Data\ProductInterface $product,
    float $specialPrice,
    string $startTime,
    string $endTime,
    string $name,
    int $storeId = 0
): bool {

    /** @var \Magento\Staging\Api\Data\UpdateInterfaceFactory */
    $stagingUpdate = $this->stagingUpdateFactory
        ->create()
        ->setName($name)
        ->setStartTime($startTime)
        ->setEndTime($endTime);

    $product
        ->setStoreId($storeId)
        ->setSpecialPrice($specialPrice);

    try {

        /** @var \Magento\Staging\Api\UpdateRepositoryInterface */
        $stagingUpdate = $this->stagingUpdateRepository->save($stagingUpdate);
    } catch (\Magento\Framework\Exception\ValidatorException $e) {
        $this->logger->warning(
            __(
                "Price Schedule Validation Exception - %1",
                $e->getMessage()
            )
        );

        return false;
    }

    if ($stagingUpdate->getId()) {

        /** @var \Magento\Staging\Model\VersionManager */
        $this->stagingVersionManager->setCurrentVersionId(
            $stagingUpdate->getId()
        );

        try {

            /** @var \Magento\CatalogStaging\Api\ProductStagingInterface */
            return $this->stagingProductUpdate->schedule($product, $stagingUpdate->getId());
        } catch (\Magento\Framework\Exception\ValidatorException $e) {
            $this->logger->warning(
                __(
                    "Price Schedule Validation Exception - %1",
                    $e->getMessage()
                )
            );

            return false;
        }
    } else {
        return false;
    }

    return false;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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