resourceConnection = $resourceConnection; $this->indexNameBuilder = $indexNameBuilder; $this->indexNameResolver = $indexNameResolver; $this->metadataPool = $metadataPool; } /** * Prepare select. * * @param int $stockId * @return Select * @throws Exception */ public function execute(int $stockId): Select { $connection = $this->resourceConnection->getConnection(); $indexName = $this->indexNameBuilder ->setIndexId(InventoryIndexer::INDEXER_ID) ->addDimension('stock_', (string)$stockId) ->setAlias(Alias::ALIAS_MAIN) ->build(); $indexTableName = $this->indexNameResolver->resolveName($indexName); $metadata = $this->metadataPool->getMetadata(ProductInterface::class); $linkField = $metadata->getLinkField(); $select = $connection->select() ->from( ['stock' => $indexTableName], [ IndexStructure::SKU => 'parent_product_entity.sku', IndexStructure::QUANTITY => 'SUM(stock.quantity)', IndexStructure::IS_SALABLE => 'MAX(stock.is_salable)', ] )->joinInner( ['product_entity' => $this->resourceConnection->getTableName('catalog_product_entity')], 'product_entity.sku = stock.sku', [] )->joinInner( ['parent_link' => $this->resourceConnection->getTableName('catalog_product_super_link')], 'parent_link.product_id = product_entity.entity_id', [] )->joinInner( ['parent_product_entity' => $this->resourceConnection->getTableName('catalog_product_entity')], 'parent_product_entity.' . $linkField . ' = parent_link.parent_id', [] ) ->group(['parent_product_entity.sku']); return $select; } }