AddStockDataToCollection.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Framework\App\ObjectManager;
  10. use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
  11. use Magento\InventoryIndexer\Indexer\IndexStructure;
  12. use Magento\InventoryIndexer\Model\StockIndexTableNameResolverInterface;
  13. /**
  14. * Add Stock data to collection
  15. */
  16. class AddStockDataToCollection
  17. {
  18. /**
  19. * @var StockIndexTableNameResolverInterface
  20. */
  21. private $stockIndexTableNameResolver;
  22. /**
  23. * @var DefaultStockProviderInterface
  24. */
  25. private $defaultStockProvider;
  26. /**
  27. * @param StockIndexTableNameResolverInterface $stockIndexTableNameResolver
  28. * @param DefaultStockProviderInterface $defaultStockProvider
  29. */
  30. public function __construct(
  31. StockIndexTableNameResolverInterface $stockIndexTableNameResolver,
  32. DefaultStockProviderInterface $defaultStockProvider = null
  33. ) {
  34. $this->stockIndexTableNameResolver = $stockIndexTableNameResolver;
  35. $this->defaultStockProvider = $defaultStockProvider ?: ObjectManager::getInstance()
  36. ->get(DefaultStockProviderInterface::class);
  37. }
  38. /**
  39. * @param Collection $collection
  40. * @param bool $isFilterInStock
  41. * @param int $stockId
  42. * @return void
  43. */
  44. public function execute(Collection $collection, bool $isFilterInStock, int $stockId)
  45. {
  46. if ($stockId === $this->defaultStockProvider->getId()) {
  47. $isSalableColumnName = 'stock_status';
  48. $resource = $collection->getResource();
  49. $collection->getSelect()
  50. ->join(
  51. ['stock_status_index' => $resource->getTable('cataloginventory_stock_status')],
  52. sprintf('%s.entity_id = stock_status_index.product_id', Collection::MAIN_TABLE_ALIAS),
  53. [IndexStructure::IS_SALABLE => $isSalableColumnName]
  54. );
  55. } else {
  56. $stockIndexTableName = $this->stockIndexTableNameResolver->execute($stockId);
  57. $resource = $collection->getResource();
  58. $collection->getSelect()->join(
  59. ['product' => $resource->getTable('catalog_product_entity')],
  60. sprintf('product.entity_id = %s.entity_id', Collection::MAIN_TABLE_ALIAS),
  61. []
  62. );
  63. $isSalableColumnName = IndexStructure::IS_SALABLE;
  64. $collection->getSelect()
  65. ->join(
  66. ['stock_status_index' => $stockIndexTableName],
  67. 'product.sku = stock_status_index.' . IndexStructure::SKU,
  68. [$isSalableColumnName]
  69. );
  70. }
  71. if ($isFilterInStock) {
  72. $collection->getSelect()
  73. ->where('stock_status_index.' . $isSalableColumnName . ' = ?', 1);
  74. }
  75. }
  76. }