|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation\DataProvider; |
| 8 | + |
| 9 | +use Magento\CatalogInventory\Model\Configuration as CatalogInventoryConfiguration; |
| 10 | +use Magento\CatalogInventory\Model\Stock; |
| 11 | +use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; |
| 12 | +use Magento\Framework\App\ResourceConnection; |
| 13 | +use Magento\Framework\App\ScopeResolverInterface; |
| 14 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 15 | +use Magento\Framework\DB\Select; |
| 16 | +use Magento\Framework\Search\Request\BucketInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * Attribute query builder |
| 20 | + */ |
| 21 | +class QueryBuilder |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var Resource |
| 25 | + */ |
| 26 | + private $resource; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var ScopeResolverInterface |
| 30 | + */ |
| 31 | + private $scopeResolver; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var CatalogInventoryConfiguration |
| 35 | + */ |
| 36 | + private $inventoryConfig; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param ResourceConnection $resource |
| 40 | + * @param ScopeResolverInterface $scopeResolver |
| 41 | + * @param CatalogInventoryConfiguration $inventoryConfig |
| 42 | + */ |
| 43 | + public function __construct( |
| 44 | + ResourceConnection $resource, |
| 45 | + ScopeResolverInterface $scopeResolver, |
| 46 | + CatalogInventoryConfiguration $inventoryConfig |
| 47 | + ) { |
| 48 | + $this->resource = $resource; |
| 49 | + $this->scopeResolver = $scopeResolver; |
| 50 | + $this->inventoryConfig = $inventoryConfig; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Build select. |
| 55 | + * |
| 56 | + * @param AbstractAttribute $attribute |
| 57 | + * @param string $tableName |
| 58 | + * @param int $currentScope |
| 59 | + * @param int $customerGroupId |
| 60 | + * |
| 61 | + * @return Select |
| 62 | + */ |
| 63 | + public function build( |
| 64 | + AbstractAttribute $attribute, |
| 65 | + string $tableName, |
| 66 | + int $currentScope, |
| 67 | + int $customerGroupId |
| 68 | + ) : Select { |
| 69 | + $select = $this->resource->getConnection()->select(); |
| 70 | + $select->joinInner( |
| 71 | + ['entities' => $tableName], |
| 72 | + 'main_table.entity_id = entities.entity_id', |
| 73 | + [] |
| 74 | + ); |
| 75 | + |
| 76 | + if ($attribute->getAttributeCode() === 'price') { |
| 77 | + return $this->buildQueryForPriceAttribute($currentScope, $customerGroupId, $select); |
| 78 | + } |
| 79 | + |
| 80 | + return $this->buildQueryForAttribute($currentScope, $attribute, $select); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Build select for price attribute. |
| 85 | + * |
| 86 | + * @param int $currentScope |
| 87 | + * @param int $customerGroupId |
| 88 | + * @param Select $select |
| 89 | + * |
| 90 | + * @return Select |
| 91 | + */ |
| 92 | + private function buildQueryForPriceAttribute( |
| 93 | + int $currentScope, |
| 94 | + int $customerGroupId, |
| 95 | + Select $select |
| 96 | + ) : Select { |
| 97 | + /** @var \Magento\Store\Model\Store $store */ |
| 98 | + $store = $this->scopeResolver->getScope($currentScope); |
| 99 | + if (!$store instanceof \Magento\Store\Model\Store) { |
| 100 | + throw new \RuntimeException('Illegal scope resolved'); |
| 101 | + } |
| 102 | + |
| 103 | + $table = $this->resource->getTableName('catalog_product_index_price'); |
| 104 | + $select->from(['main_table' => $table], null) |
| 105 | + ->columns([BucketInterface::FIELD_VALUE => 'main_table.min_price']) |
| 106 | + ->where('main_table.customer_group_id = ?', $customerGroupId) |
| 107 | + ->where('main_table.website_id = ?', $store->getWebsiteId()); |
| 108 | + |
| 109 | + return $select; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Build select for attribute. |
| 114 | + * |
| 115 | + * @param int $currentScope |
| 116 | + * @param AbstractAttribute $attribute |
| 117 | + * @param Select $select |
| 118 | + * |
| 119 | + * @return Select |
| 120 | + */ |
| 121 | + private function buildQueryForAttribute( |
| 122 | + int $currentScope, |
| 123 | + AbstractAttribute $attribute, |
| 124 | + Select $select |
| 125 | + ) : Select { |
| 126 | + $currentScopeId = $this->scopeResolver->getScope($currentScope)->getId(); |
| 127 | + $table = $this->resource->getTableName( |
| 128 | + 'catalog_product_index_eav' . ($attribute->getBackendType() === 'decimal' ? '_decimal' : '') |
| 129 | + ); |
| 130 | + $select->from(['main_table' => $table], ['main_table.entity_id', 'main_table.value']) |
| 131 | + ->distinct() |
| 132 | + ->joinLeft( |
| 133 | + ['stock_index' => $this->resource->getTableName('cataloginventory_stock_status')], |
| 134 | + 'main_table.source_id = stock_index.product_id', |
| 135 | + [] |
| 136 | + ) |
| 137 | + ->where('main_table.attribute_id = ?', $attribute->getAttributeId()) |
| 138 | + ->where('main_table.store_id = ? ', $currentScopeId); |
| 139 | + |
| 140 | + if (!$this->inventoryConfig->isShowOutOfStock($currentScopeId)) { |
| 141 | + $select->where('stock_index.stock_status = ?', Stock::STOCK_IN_STOCK); |
| 142 | + } |
| 143 | + |
| 144 | + $parentSelect = $this->resource->getConnection()->select(); |
| 145 | + $parentSelect->from(['main_table' => $select], ['main_table.value']); |
| 146 | + return $parentSelect; |
| 147 | + } |
| 148 | +} |
0 commit comments