|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\CatalogInventoryDataExporter\Model\Plugin; |
| 8 | + |
| 9 | +use Magento\Framework\App\ResourceConnection; |
| 10 | +use Magento\Framework\Indexer\IndexerRegistry; |
| 11 | +use Magento\Indexer\Model\IndexerFactory; |
| 12 | +use Magento\DataExporter\Model\Logging\CommerceDataExportLoggerInterface as LoggerInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Schedule reindex for "product feed indexer" if Stock Status updated. |
| 16 | + * Out of the box we can't use standard mview.xml configuration to listen to changes on stock table |
| 17 | + * `inventory_stock_<stock_id>` since mview doesn't support subscribing on dynamic tables |
| 18 | + */ |
| 19 | +class ScheduleProductUpdate |
| 20 | +{ |
| 21 | + private const FEED_INDEXER = 'catalog_data_exporter_products'; |
| 22 | + |
| 23 | + private IndexerRegistry $indexerRegistry; |
| 24 | + private IndexerFactory $indexerFactory; |
| 25 | + private ResourceConnection $resourceConnection; |
| 26 | + private LoggerInterface $logger; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param IndexerFactory $indexerFactory |
| 30 | + * @param IndexerRegistry $indexerRegistry |
| 31 | + * @param ResourceConnection $resourceConnection |
| 32 | + * @param LoggerInterface $logger |
| 33 | + */ |
| 34 | + public function __construct( |
| 35 | + IndexerFactory $indexerFactory, |
| 36 | + IndexerRegistry $indexerRegistry, |
| 37 | + ResourceConnection $resourceConnection, |
| 38 | + LoggerInterface $logger |
| 39 | + ) { |
| 40 | + $this->indexerFactory = $indexerFactory; |
| 41 | + $this->indexerRegistry = $indexerRegistry; |
| 42 | + $this->resourceConnection = $resourceConnection; |
| 43 | + $this->logger = $logger; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Add product ids to changelog |
| 48 | + * |
| 49 | + * @param array $productSkus |
| 50 | + * @return void |
| 51 | + */ |
| 52 | + public function execute(array $productSkus): void |
| 53 | + { |
| 54 | + try { |
| 55 | + $productIndexer = $this->indexerRegistry->get(self::FEED_INDEXER); |
| 56 | + if (!empty($productSkus) && $productIndexer->isScheduled()) { |
| 57 | + $productIds = $this->getProductIdsFromSkus($productSkus); |
| 58 | + if (!$productIds) { |
| 59 | + $this->logger->warning("Cannot get product ids from SKUs: " . var_export($productSkus, true)); |
| 60 | + return ; |
| 61 | + } |
| 62 | + $this->updateChangelog($productIds); |
| 63 | + } |
| 64 | + } catch (\Throwable $e) { |
| 65 | + $this->logger->error('Cannot update indexer during inventory source item save: ' . $e->getMessage()); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Update change log |
| 71 | + * |
| 72 | + * @param array $productIds |
| 73 | + * @return void |
| 74 | + */ |
| 75 | + private function updateChangelog(array $productIds): void |
| 76 | + { |
| 77 | + $connection = $this->resourceConnection->getConnection(); |
| 78 | + $view = $this->indexerFactory->create()->load(self::FEED_INDEXER)->getView(); |
| 79 | + $tableName = $view->getChangelog()->getName(); |
| 80 | + $realTableName = $this->resourceConnection->getTableName($tableName); |
| 81 | + $connection->insertArray($realTableName, ['entity_id'], $productIds); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Get product ids from skus |
| 86 | + * |
| 87 | + * @param array $productSkus |
| 88 | + * @return array |
| 89 | + */ |
| 90 | + private function getProductIdsFromSkus(array $productSkus): array |
| 91 | + { |
| 92 | + $connection = $this->resourceConnection->getConnection(); |
| 93 | + $select = $connection->select() |
| 94 | + ->from( |
| 95 | + ['e' => $this->resourceConnection->getTableName('catalog_product_entity')], |
| 96 | + ['entity_id'] |
| 97 | + )->where('sku IN (?)', $productSkus); |
| 98 | + |
| 99 | + return $connection->fetchCol($select); |
| 100 | + } |
| 101 | +} |
0 commit comments