Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LinkedProductSelectBuilderByBasePrice.php
Go to the documentation of this file.
1 <?php
7 
13 
15 {
19  private $storeManager;
20 
24  private $resource;
25 
29  private $eavConfig;
30 
34  private $catalogHelper;
35 
39  private $metadataPool;
40 
44  private $baseSelectProcessor;
45 
54  public function __construct(
55  \Magento\Store\Model\StoreManagerInterface $storeManager,
57  \Magento\Eav\Model\Config $eavConfig,
58  \Magento\Catalog\Helper\Data $catalogHelper,
59  \Magento\Framework\EntityManager\MetadataPool $metadataPool,
60  BaseSelectProcessorInterface $baseSelectProcessor = null
61  ) {
62  $this->storeManager = $storeManager;
63  $this->resource = $resourceConnection;
64  $this->eavConfig = $eavConfig;
65  $this->catalogHelper = $catalogHelper;
66  $this->metadataPool = $metadataPool;
67  $this->baseSelectProcessor = (null !== $baseSelectProcessor)
68  ? $baseSelectProcessor : ObjectManager::getInstance()->get(BaseSelectProcessorInterface::class);
69  }
70 
74  public function build($productId)
75  {
76  $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
77  $priceAttribute = $this->eavConfig->getAttribute(Product::ENTITY, 'price');
78  $productTable = $this->resource->getTableName('catalog_product_entity');
79 
80  $priceSelect = $this->resource->getConnection()->select()
81  ->from(['parent' => $productTable], '')
82  ->joinInner(
83  ['link' => $this->resource->getTableName('catalog_product_relation')],
84  "link.parent_id = parent.$linkField",
85  []
86  )->joinInner(
88  sprintf('%s.entity_id = link.child_id', BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS, $linkField),
89  ['entity_id']
90  )->joinInner(
91  ['t' => $priceAttribute->getBackendTable()],
92  sprintf('t.%s = %s.%1$s', $linkField, BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS),
93  []
94  )->where('parent.entity_id = ?', $productId)
95  ->where('t.attribute_id = ?', $priceAttribute->getAttributeId())
96  ->where('t.value IS NOT NULL')
97  ->order('t.value ' . Select::SQL_ASC)
98  ->order(BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS . '.' . $linkField . ' ' . Select::SQL_ASC)
99  ->limit(1);
100  $priceSelect = $this->baseSelectProcessor->process($priceSelect);
101 
102  if (!$this->catalogHelper->isPriceGlobal()) {
103  $priceSelectStore = clone $priceSelect;
104  $priceSelectStore->where('t.store_id = ?', $this->storeManager->getStore()->getId());
105  $selects[] = $priceSelectStore;
106  }
107 
108  $priceSelect->where('t.store_id = ?', Store::DEFAULT_STORE_ID);
109  $selects[] = $priceSelect;
110 
111  return $selects;
112  }
113 }
$storeManager
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\ResourceConnection $resourceConnection, \Magento\Eav\Model\Config $eavConfig, \Magento\Catalog\Helper\Data $catalogHelper, \Magento\Framework\EntityManager\MetadataPool $metadataPool, BaseSelectProcessorInterface $baseSelectProcessor=null)
const SQL_ASC
Definition: Select.php:81