Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SelectBuilderForAttribute.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation\DataProvider\SelectBuilderForAttribute\
11 ApplyStockConditionToSelect;
21 
29 {
33  private $scopeResolver;
34 
38  private $resource;
39 
43  private $applyStockConditionToSelect;
44 
48  private $customerSession;
49 
53  private $scopeConfig;
54 
62  public function __construct(
63  ResourceConnection $resource,
64  ScopeResolverInterface $scopeResolver,
65  ApplyStockConditionToSelect $applyStockConditionToSelect,
66  Session $customerSession,
67  ScopeConfigInterface $scopeConfig
68  ) {
69  $this->resource = $resource;
70  $this->scopeResolver = $scopeResolver;
71  $this->applyStockConditionToSelect = $applyStockConditionToSelect;
72  $this->customerSession = $customerSession;
73  $this->scopeConfig = $scopeConfig;
74  }
75 
83  public function build(Select $select, AbstractAttribute $attribute, int $currentScope): Select
84  {
85  if ($attribute->getAttributeCode() === 'price') {
87  $store = $this->scopeResolver->getScope($currentScope);
88  if (!$store instanceof Store) {
89  throw new \RuntimeException('Illegal scope resolved');
90  }
91  $table = $this->resource->getTableName('catalog_product_index_price');
92  $select->from(['main_table' => $table], null)
93  ->columns([BucketInterface::FIELD_VALUE => 'main_table.min_price'])
94  ->where('main_table.customer_group_id = ?', $this->customerSession->getCustomerGroupId())
95  ->where('main_table.website_id = ?', $store->getWebsiteId());
96  } else {
97  $currentScopeId = $this->scopeResolver->getScope($currentScope)->getId();
98  $table = $this->resource->getTableName(
99  'catalog_product_index_eav' . ($attribute->getBackendType() === 'decimal' ? '_decimal' : '')
100  );
101  $subSelect = $select;
102  $subSelect->from(['main_table' => $table], ['main_table.entity_id', 'main_table.value'])
103  ->distinct()
104  ->where('main_table.attribute_id = ?', $attribute->getAttributeId())
105  ->where('main_table.store_id = ? ', $currentScopeId);
106  if ($this->isAddStockFilter()) {
107  $subSelect = $this->applyStockConditionToSelect->execute($subSelect);
108  }
109 
110  $parentSelect = $this->resource->getConnection()->select();
111  $parentSelect->from(['main_table' => $subSelect], ['main_table.value']);
112  $select = $parentSelect;
113  }
114 
115  return $select;
116  }
117 
121  private function isAddStockFilter()
122  {
123  $isShowOutOfStock = $this->scopeConfig->isSetFlag(
124  'cataloginventory/options/show_out_of_stock',
126  );
127 
128  return false === $isShowOutOfStock;
129  }
130 }
$resource
Definition: bulk.php:12
$table
Definition: trigger.php:14
__construct(ResourceConnection $resource, ScopeResolverInterface $scopeResolver, ApplyStockConditionToSelect $applyStockConditionToSelect, Session $customerSession, ScopeConfigInterface $scopeConfig)