Blog Development Magento 2

Programmatically schedule product and content changes in Magento 2 Enterprise

Programmatically schedule product and content changes in Magento 2 Enterprise

Categories
Magento 2

Magento 2 Enterprise Edition introduced a new way of staging content compared to Magento 1 EE. This new approach allows easy product scheduling and content staging. Two big features e-commerce marketing teams and site administrators rely on. With an additional software within the stack like the Product Information Management tool Pimcore in our case, there is an apparent need to […]

Magento 2 Enterprise Edition introduced a new way of staging content compared to Magento 1 EE.

This new approach allows easy product scheduling and content staging. Two big features e-commerce marketing teams and site administrators rely on.

With an additional software within the stack like the Product Information Management tool Pimcore in our case, there is an apparent need to make these changes programatically.

In this walkthrough we explain how we did it as a part of a real world project.

Here’s an overview with code examples and explanations for each part.

Approach

First, an example of how to programatically create a scheduled process.

You will need two instances:

Instance for scheduled updates:

  1. Magento\Staging\Api\Data\UpdateInterface
  2. Magento\Staging\Api\UpdateRepositoryInterface

Instance for adding products:

  1. Magento\CatalogStaging\Api\ProductStagingInterface

Bellow you will find some code examples to help you out building those three instances.

Create scheduled update instance:

/** @var \Magento\Staging\Api\Data\UpdateInterface $schedule */ $schedule = $this->updateFactory->create();

Set the name for scheduled updates and define the start date:

$schedule->setName(“Test”); $timestampStart = $this->localeDate->scopeTimeStamp() + 3600; $date = new \DateTime(‘@’ . $timestampStart, new \DateTimeZone(‘UTC’)); $schedule->setStartTime($date->format(‘Y-m-d H:i:s’));

By defining the end date, the scheduled update will be finished by the set time and all scheduled data will be reverted to the previous state.

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

If we want our scheduled update to run indefinitely we can omit setting the end date.
As a last step in scheduling updates, it needs to be saved and it’s version set:

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

Following the scheduled update product updates come next.
It’s suitable to create a product repository and then use a get method to retrieve a specific product:

$repository = $this->productRepository; $product = $repository->get(‘239487’);

Preparing new product data to be applied by the scheduled update:

$name = $product->getName(); $product->setName($name . ” – New”); $price = $product->getPrice(); $product->setSpecialPrice($price – 10);

Finally, we can schedule product updates:

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

List all scheduled updates

It’s good to know what updates are in queue. You can list all of them with the following:

//@var Magento\Staging\Api\Data\UpdateSearchResultInterface $list */$list = $this->updateRepository->getList($this->searchCriteria); foreach ($list->getItems() as $item) { var_dump($item->getData()); }

Delete scheduled updates

Once you no longer need a specific scheduled update, delete it and update the repository:

$delete = $this->updateRepository->get(‘1494605241’); $this->updateRepository->delete($delete);

For your convenience, here’s the full code you can try out and see if it works for you.

This is an example of the approach we took while integrating Magento 2 EE with Pimcore.Hope you will find this useful in your case also.

Happy coding!