8

I want to set "Special Price" for product from date to date programmatically by code but don't know how, i use Magento 2 EE. I can set in admin use

enter image description here

then enter image description here

but I want use code to set programmatically. If anyone know, please advice.

2
  • Seem that you're using Enterprise Edition? Commented Dec 5, 2016 at 12:43
  • Yes, i using EE version
    – Init
    Commented Dec 6, 2016 at 1:40

2 Answers 2

8

like Igor already mention you can try with the following. First, create scheduled update instance and set name, start and end time.

/**
 * @var \Magento\Staging\Api\UpdateRepositoryInterface
 */
protected $updateRepository;

/**
 * @var \Magento\Staging\Api\Data\UpdateInterfaceFactory
 */
protected $updateFactory;

/**
 * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
 */
protected $localeDate;

/**
 * @var \Magento\Catalog\Api\ProductRepositoryInterface
 */
protected $productRepository;

/**
 * @var \Magento\CatalogStaging\Api\ProductStagingInterface
 */
protected $productStaging;

/**
 * @var \Magento\Staging\Model\VersionManager
 */
protected $versionManager;

/**
 * @param \Magento\Staging\Api\UpdateRepositoryInterface $updateRepositoryInterface
 * @param \Magento\Staging\Api\Data\UpdateInterfaceFactory $updateFactory
 * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
 * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepositoryInterface
 * @param \Magento\CatalogStaging\Api\ProductStagingInterface $productStagingInterface
 * @param \Magento\Staging\Model\VersionManagerFactory $versionManagerFactory
 */
public function __construct(
    \Magento\Staging\Api\UpdateRepositoryInterface $updateRepositoryInterface,
    \Magento\Staging\Api\Data\UpdateInterfaceFactory $updateFactory,
    \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepositoryInterface,
    \Magento\CatalogStaging\Api\ProductStagingInterface $productStagingInterface,
    \Magento\Staging\Model\VersionManagerFactory $versionManagerFactory
){
    $this->updateRepository = $updateRepositoryInterface;
    $this->updateFactory = $updateFactory;
    $this->localeDate = $localeDate;
    $this->productRepository = $productRepositoryInterface;
    $this->productStaging = $productStagingInterface;
    $this->versionManager = $versionManagerFactory->create();
}


    /** @var \Magento\Staging\Api\Data\UpdateInterface $schedule */
    $schedule = $this->updateFactory->create();
    $schedule->setName("test update");
    $timestampStart = $this->localeDate->scopeTimeStamp() + 3600; 
    $date = new \DateTime('@' . $timestampStart, new \DateTimeZone('UTC')); 
    $schedule->setStartTime($date->format('Y-m-d H:i:s'));

    $timestampEnd = $timestampStart + (60 * 60 * 24);
    $date = new \DateTime('@' . $timestampEnd, new \DateTimeZone('UTC'));
    $schedule->setEndTime($date->format('Y-m-d H:i:s'));

If we omit setting the end date, scheduled update will run indefinitely. At the end, save scheduled update and set it's version.

    // @var  \Magento\Staging\Api\Data\UpdateInterface
    $stagingRepo = $this->updateRepository->save($schedule);
    $this->versionManager->setCurrentVersionId($stagingRepo->getId());

Next, create product updates

    $repository = $this->productRepository;
    $product = $repository->get('239487');
    $name = $product->getName();
    $product->setName($name . " - New");
    $price = $product->getPrice();
    $product->setSpecialPrice($price - 10);

And the last step is to schedule product updates

    $this->productStaging->schedule($product, $stagingRepo->getId());

I wrote a small example and it can be found here

2
  • It was life saving solution. Thank you so much! Commented May 5, 2021 at 9:16
  • Do you know how to check if there is an existing schedule for a product?
    – amitshree
    Commented Jul 15, 2021 at 6:37
0

You can use API for this. First, you may need to retrieve update using \Magento\Staging\Api\UpdateRepositoryInterface, then you can create a different version of the product using \Magento\CatalogStaging\Api\ProductStagingInterface.

I didn't try it myself. Let me know if it doesn't work and I will look more into it.

1
  • Hi @igor-melnykov , think same u too, but i don't know type of data send to API, i'm try but data when send to API was wrong so it fail, pls look more into it, thanks
    – Init
    Commented Dec 21, 2016 at 7:20

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.