AddIsInStockFieldToCollection.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryCatalog\Model\ResourceModel;
  8. use Magento\Catalog\Model\ResourceModel\Product\Collection;
  9. use Magento\InventoryIndexer\Indexer\IndexStructure;
  10. use Magento\InventoryIndexer\Model\StockIndexTableNameResolverInterface;
  11. /**
  12. * Adapt adding and applying is in stock field to collection for Multi Stocks.
  13. */
  14. class AddIsInStockFieldToCollection
  15. {
  16. /**
  17. * @var StockIndexTableNameResolverInterface
  18. */
  19. private $stockIndexTableProvider;
  20. /**
  21. * @param StockIndexTableNameResolverInterface $stockIndexTableProvider
  22. */
  23. public function __construct(
  24. StockIndexTableNameResolverInterface $stockIndexTableProvider
  25. ) {
  26. $this->stockIndexTableProvider = $stockIndexTableProvider;
  27. }
  28. /**
  29. * @param Collection $collection
  30. * @param int $stockId
  31. * @return void
  32. */
  33. public function execute($collection, int $stockId)
  34. {
  35. $tableName = $this->stockIndexTableProvider->execute($stockId);
  36. $collection->getSelect()->join(
  37. ['inventory_in_stock' => $tableName],
  38. 'e.sku = inventory_in_stock.sku',
  39. []
  40. )->where('inventory_in_stock.' . IndexStructure::IS_SALABLE . ' = ?', 1);
  41. }
  42. }