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
but I want use code to set programmatically. If anyone know, please advice.
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
but I want use code to set programmatically. If anyone know, please advice.
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
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.