Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LinkedProductSelectBuilderByIndexPrice.php
Go to the documentation of this file.
1 <?php
7 
17 
19 {
23  private $storeManager;
24 
28  private $resource;
29 
33  private $customerSession;
34 
38  private $metadataPool;
39 
43  private $baseSelectProcessor;
44 
48  private $priceTableResolver;
49 
53  private $dimensionFactory;
54 
65  public function __construct(
66  \Magento\Store\Model\StoreManagerInterface $storeManager,
68  \Magento\Customer\Model\Session $customerSession,
69  \Magento\Framework\EntityManager\MetadataPool $metadataPool,
70  BaseSelectProcessorInterface $baseSelectProcessor = null,
71  IndexScopeResolverInterface $priceTableResolver = null,
72  DimensionFactory $dimensionFactory = null
73  ) {
74  $this->storeManager = $storeManager;
75  $this->resource = $resourceConnection;
76  $this->customerSession = $customerSession;
77  $this->metadataPool = $metadataPool;
78  $this->baseSelectProcessor = (null !== $baseSelectProcessor)
79  ? $baseSelectProcessor : ObjectManager::getInstance()->get(BaseSelectProcessorInterface::class);
80  $this->priceTableResolver = $priceTableResolver
81  ?? ObjectManager::getInstance()->get(IndexScopeResolverInterface::class);
82  $this->dimensionFactory = $dimensionFactory ?? ObjectManager::getInstance()->get(DimensionFactory::class);
83  }
84 
88  public function build($productId)
89  {
90  $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
91  $productTable = $this->resource->getTableName('catalog_product_entity');
92  $websiteId = $this->storeManager->getStore()->getWebsiteId();
93  $customerGroupId = $this->customerSession->getCustomerGroupId();
94 
95  $priceSelect = $this->resource->getConnection()->select()
96  ->from(['parent' => $productTable], '')
97  ->joinInner(
98  ['link' => $this->resource->getTableName('catalog_product_relation')],
99  "link.parent_id = parent.$linkField",
100  []
101  )->joinInner(
103  sprintf('%s.entity_id = link.child_id', BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS),
104  ['entity_id']
105  )->joinInner(
106  [
107  't' => $this->priceTableResolver->resolve('catalog_product_index_price', [
108  $this->dimensionFactory->create(WebsiteDimensionProvider::DIMENSION_NAME, (string)$websiteId),
109  $this->dimensionFactory->create(
110  CustomerGroupDimensionProvider::DIMENSION_NAME,
111  (string)$customerGroupId
112  ),
113  ])
114  ],
115  sprintf('t.entity_id = %s.entity_id', BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS),
116  []
117  )->where('parent.entity_id = ?', $productId)
118  ->where('t.website_id = ?', $websiteId)
119  ->where('t.customer_group_id = ?', $customerGroupId)
120  ->order('t.min_price ' . Select::SQL_ASC)
121  ->order(BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS . '.' . $linkField . ' ' . Select::SQL_ASC)
122  ->limit(1);
123  $priceSelect = $this->baseSelectProcessor->process($priceSelect);
124 
125  return [$priceSelect];
126  }
127 }
$storeManager
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\ResourceConnection $resourceConnection, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\EntityManager\MetadataPool $metadataPool, BaseSelectProcessorInterface $baseSelectProcessor=null, IndexScopeResolverInterface $priceTableResolver=null, DimensionFactory $dimensionFactory=null)
const SQL_ASC
Definition: Select.php:81