Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockStatusFilter.php
Go to the documentation of this file.
1 <?php
8 
14 
23 {
27  private $resourceConnection;
28 
35  const FILTER_JUST_ENTITY = 'general_filter';
36 
43  const FILTER_ENTITY_AND_SUB_PRODUCTS = 'filter_with_sub_products';
44 
48  private $conditionManager;
49 
53  private $stockConfiguration;
54 
58  private $stockRegistry;
59 
66  public function __construct(
67  ResourceConnection $resourceConnection,
68  ConditionManager $conditionManager,
69  StockConfigurationInterface $stockConfiguration,
70  StockRegistryInterface $stockRegistry
71  ) {
72  $this->resourceConnection = $resourceConnection;
73  $this->conditionManager = $conditionManager;
74  $this->stockConfiguration = $stockConfiguration;
75  $this->stockRegistry = $stockRegistry;
76  }
77 
88  public function apply(Select $select, $stockValues, $type, $showOutOfStockFlag)
89  {
90  if ($type !== self::FILTER_JUST_ENTITY && $type !== self::FILTER_ENTITY_AND_SUB_PRODUCTS) {
91  throw new \InvalidArgumentException(sprintf('Invalid filter type: %s', $type));
92  }
93 
94  $select = clone $select;
95  $mainTableAlias = $this->extractTableAliasFromSelect($select);
96 
97  $this->addMainStockStatusJoin($select, $stockValues, $mainTableAlias, $showOutOfStockFlag);
98 
99  if ($type === self::FILTER_ENTITY_AND_SUB_PRODUCTS) {
100  $this->addSubProductsStockStatusJoin($select, $stockValues, $mainTableAlias, $showOutOfStockFlag);
101  }
102 
103  return $select;
104  }
105 
116  private function addMainStockStatusJoin(Select $select, $stockValues, $mainTableAlias, $showOutOfStockFlag)
117  {
118  $catalogInventoryTable = $this->resourceConnection->getTableName('cataloginventory_stock_status');
119  $select->joinInner(
120  ['stock_index' => $catalogInventoryTable],
121  $this->conditionManager->combineQueries(
122  [
123  sprintf('stock_index.product_id = %s.entity_id', $mainTableAlias),
124  $this->conditionManager->generateCondition(
125  'stock_index.website_id',
126  '=',
127  $this->stockConfiguration->getDefaultScopeId()
128  ),
129  $showOutOfStockFlag
130  ? ''
131  : $this->conditionManager->generateCondition(
132  'stock_index.stock_status',
133  is_array($stockValues) ? 'in' : '=',
134  $stockValues
135  ),
136  $this->conditionManager->generateCondition(
137  'stock_index.stock_id',
138  '=',
139  (int) $this->stockRegistry->getStock()->getStockId()
140  ),
141  ],
143  ),
144  []
145  );
146  }
147 
158  private function addSubProductsStockStatusJoin(Select $select, $stockValues, $mainTableAlias, $showOutOfStockFlag)
159  {
160  $catalogInventoryTable = $this->resourceConnection->getTableName('cataloginventory_stock_status');
161  $select->joinInner(
162  ['sub_products_stock_index' => $catalogInventoryTable],
163  $this->conditionManager->combineQueries(
164  [
165  sprintf('sub_products_stock_index.product_id = %s.source_id', $mainTableAlias),
166  $this->conditionManager->generateCondition(
167  'sub_products_stock_index.website_id',
168  '=',
169  $this->stockConfiguration->getDefaultScopeId()
170  ),
171  $showOutOfStockFlag
172  ? ''
173  : $this->conditionManager->generateCondition(
174  'sub_products_stock_index.stock_status',
175  is_array($stockValues) ? 'in' : '=',
176  $stockValues
177  ),
178  $this->conditionManager->generateCondition(
179  'sub_products_stock_index.stock_id',
180  '=',
181  (int) $this->stockRegistry->getStock()->getStockId()
182  ),
183  ],
185  ),
186  []
187  );
188  }
189 
196  private function extractTableAliasFromSelect(Select $select)
197  {
198  $fromArr = array_filter(
199  $select->getPart(Select::FROM),
200  function ($fromPart) {
201  return $fromPart['joinType'] === Select::FROM;
202  }
203  );
204 
205  return $fromArr ? array_keys($fromArr)[0] : null;
206  }
207 }
apply(Select $select, $stockValues, $type, $showOutOfStockFlag)
const FROM
Definition: Select.php:49
$type
Definition: item.phtml:13
__construct(ResourceConnection $resourceConnection, ConditionManager $conditionManager, StockConfigurationInterface $stockConfiguration, StockRegistryInterface $stockRegistry)
const SQL_AND
Definition: Select.php:77