Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddStockDataToCollection.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
13 
18 {
22  private $stockIndexTableNameResolver;
23 
27  public function __construct(StockIndexTableNameResolverInterface $stockIndexTableNameResolver)
28  {
29  $this->stockIndexTableNameResolver = $stockIndexTableNameResolver;
30  }
31 
38  public function execute(Collection $collection, bool $isFilterInStock, int $stockId)
39  {
40  $stockIndexTableName = $this->stockIndexTableNameResolver->execute($stockId);
41 
42  $resource = $collection->getResource();
43  $collection->getSelect()->join(
44  ['product' => $resource->getTable('catalog_product_entity')],
45  sprintf('product.entity_id = %s.entity_id', Collection::MAIN_TABLE_ALIAS),
46  []
47  );
48  $collection->getSelect()
49  ->join(
50  ['stock_status_index' => $stockIndexTableName],
51  'product.sku = stock_status_index.' . IndexStructure::SKU,
53  );
54 
55  if ($isFilterInStock) {
56  $collection->getSelect()
57  ->where('stock_status_index.' . IndexStructure::IS_SALABLE . ' = ?', 1);
58  }
59  }
60 }
__construct(StockIndexTableNameResolverInterface $stockIndexTableNameResolver)
$resource
Definition: bulk.php:12
execute(Collection $collection, bool $isFilterInStock, int $stockId)