Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GetProductIdsByStockIds.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 
19 {
23  private $resource;
24 
28  private $stockIndexTableNameResolver;
29 
33  private $defaultStockProvider;
34 
38  private $productTableName;
39 
46  public function __construct(
47  ResourceConnection $resource,
48  StockIndexTableNameResolverInterface $stockIndexTableNameResolver,
49  DefaultStockProviderInterface $defaultStockProvider,
50  string $productTableName
51  ) {
52  $this->resource = $resource;
53  $this->defaultStockProvider = $defaultStockProvider;
54  $this->stockIndexTableNameResolver = $stockIndexTableNameResolver;
55  $this->productTableName = $productTableName;
56  }
57 
62  public function execute(array $stockIds): array
63  {
64  $productIds = [[]];
65  foreach ($stockIds as $stockId) {
66  if ($this->defaultStockProvider->getId() === (int)$stockId) {
67  continue;
68  }
69  $stockIndexTableName = $this->stockIndexTableNameResolver->execute($stockId);
70  $connection = $this->resource->getConnection();
71 
72  if ($connection->isTableExists($stockIndexTableName)) {
73  $sql = $connection->select()
74  ->from(['stock_index' => $stockIndexTableName], [])
75  ->join(
76  ['product' => $this->resource->getTableName($this->productTableName)],
77  'product.sku = stock_index.' . IndexStructure::SKU,
78  ['product.entity_id']
79  );
80  $productIds[] = $connection->fetchCol($sql);
81  }
82  }
83  $productIds = array_merge(...$productIds);
84 
85  return array_unique($productIds);
86  }
87 }
__construct(ResourceConnection $resource, StockIndexTableNameResolverInterface $stockIndexTableNameResolver, DefaultStockProviderInterface $defaultStockProvider, string $productTableName)
$resource
Definition: bulk.php:12
$connection
Definition: bulk.php:13