2

I have added 2 schedules to a product in Magento as shown below.

enter image description here

Below code gives list of all schedules present in our database but there is no sku/product_id relation here.

<?php

namespace Vendor\CustomImport\Model;

use Magento\Framework\Api\SearchCriteria;

class Test
{
  
    protected $searchCriteriaBuilder;
    protected $updateRepository;

    public function __construct
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
        \Magento\Staging\Api\UpdateRepositoryInterface $updateRepository
    ) {
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->updateRepository = $updateRepository;
    }

    public function getSchedule()
    {
    try {
        $searchCriteria = $this->searchCriteriaBuilder->create();

        $items = $this->updateRepository->getList($searchCriteria)->getItems();
    echo '<pre>';
    foreach ($items as $item) {
        print_r($item->getData());exit;
    }

    }   catch (\Exception $e){
    echo $e->getMessage();
    }
    }
}

Question: How to get all scheduled changes for a product by sku?

2
  • Are using any third party extension or what? Jul 17, 2021 at 12:59
  • No. Scheduling is Magento enterprise default feature.
    – amitshree
    Jul 17, 2021 at 17:16

2 Answers 2

1

You should be able to find related schedule product updates by searching in product entity table, filtering by created_in where equal to id from staging_update. (initially created product has created_in = 1)

So, for the sake of simplicity, in your items foreach loop, do something like this

$scheduledChanges = [];
foreach ($items as $item) {
    $productSearchCriteria = $this->searchCriteriaBuilder->addFieldter('created_in', $item->getId())->create();
    $products = $this->productRepository->getList($productSearchCriteria)->getItems();

    foreach($products as $product) {
        $scheduledChanges[$product->getSku()] = $scheduledChanges[$product->getSku()] ?? [];
        $scheduledChanges[$product->getSku()][] = $item->getName();
    }
}
// print_r($scheduledChanges); // the array would group scheduled updates by SKU

additionally, you may want to add a is_rollback is null filter to the staging table if you don't care about rollback updates.

1
  • It says The "created_in" attribute name is invalid. Reset the name and try again.
    – amitshree
    Jul 20, 2021 at 8:26
0

Here is how you can get all existing schedules of a product by product id.

  <?php
    
    namespace Vendor\CustomImport\Model\Import;
    
    use Magento\Framework\App\RequestInterface;
    use Magento\Staging\Model\Entity\Upcoming\SearchResultFactory;
    
    class Test
    {
    
        protected $request;
        protected $searchResultFactory;
    
        public function __construct (
            RequestInterface $request,
            SearchResultFactory $searchResultFactory
        ) {
            $this->request = $request;
            $this->searchResultFactory = $searchResultFactory;
        }
    
        public function getSchedule($productId)
        {
        $schedules = [];
        try {
            $params = [
                'entityRequestName' => 'id',
                'entityTable' => 'catalog_product_entity',
                'entityColumn' => 'entity_id'
            ];
            $this->request->setParams( ['id' => $productId]);
            $schedules = $this->searchResultFactory->create($params);
            foreach ($schedules as $schedule) {
                echo $schedule->getName();
                echo $schedule->getStartTime();
                // Get Other values
            }
        }   catch (\Exception $e){
            echo $e->getMessage();
        }
        return  $schedules;
    }
}

Your Answer

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

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