27 private $resourceConnection;
48 private $conditionManager;
53 private $stockConfiguration;
58 private $stockRegistry;
73 $this->conditionManager = $conditionManager;
74 $this->stockConfiguration = $stockConfiguration;
90 if (
$type !== self::FILTER_JUST_ENTITY &&
$type !== self::FILTER_ENTITY_AND_SUB_PRODUCTS) {
91 throw new \InvalidArgumentException(sprintf(
'Invalid filter type: %s',
$type));
95 $mainTableAlias = $this->extractTableAliasFromSelect(
$select);
97 $this->addMainStockStatusJoin(
$select, $stockValues, $mainTableAlias, $showOutOfStockFlag);
99 if (
$type === self::FILTER_ENTITY_AND_SUB_PRODUCTS) {
100 $this->addSubProductsStockStatusJoin(
$select, $stockValues, $mainTableAlias, $showOutOfStockFlag);
116 private function addMainStockStatusJoin(
Select $select, $stockValues, $mainTableAlias, $showOutOfStockFlag)
118 $catalogInventoryTable = $this->resourceConnection->getTableName(
'cataloginventory_stock_status');
120 [
'stock_index' => $catalogInventoryTable],
121 $this->conditionManager->combineQueries(
123 sprintf(
'stock_index.product_id = %s.entity_id', $mainTableAlias),
124 $this->conditionManager->generateCondition(
125 'stock_index.website_id',
127 $this->stockConfiguration->getDefaultScopeId()
131 : $this->conditionManager->generateCondition(
132 'stock_index.stock_status',
133 is_array($stockValues) ?
'in' :
'=',
136 $this->conditionManager->generateCondition(
137 'stock_index.stock_id',
139 (
int) $this->stockRegistry->getStock()->getStockId()
158 private function addSubProductsStockStatusJoin(
Select $select, $stockValues, $mainTableAlias, $showOutOfStockFlag)
160 $catalogInventoryTable = $this->resourceConnection->getTableName(
'cataloginventory_stock_status');
162 [
'sub_products_stock_index' => $catalogInventoryTable],
163 $this->conditionManager->combineQueries(
165 sprintf(
'sub_products_stock_index.product_id = %s.source_id', $mainTableAlias),
166 $this->conditionManager->generateCondition(
167 'sub_products_stock_index.website_id',
169 $this->stockConfiguration->getDefaultScopeId()
173 : $this->conditionManager->generateCondition(
174 'sub_products_stock_index.stock_status',
175 is_array($stockValues) ?
'in' :
'=',
178 $this->conditionManager->generateCondition(
179 'sub_products_stock_index.stock_id',
181 (
int) $this->stockRegistry->getStock()->getStockId()
196 private function extractTableAliasFromSelect(Select
$select)
198 $fromArr = array_filter(
200 function ($fromPart) {
205 return $fromArr ? array_keys($fromArr)[0] :
null;
apply(Select $select, $stockValues, $type, $showOutOfStockFlag)
const FILTER_ENTITY_AND_SUB_PRODUCTS
__construct(ResourceConnection $resourceConnection, ConditionManager $conditionManager, StockConfigurationInterface $stockConfiguration, StockRegistryInterface $stockRegistry)