Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LinkedProductSelectBuilderBySpecialPrice.php
Go to the documentation of this file.
1 <?php
7 
13 
18 {
22  private $storeManager;
23 
27  private $resource;
28 
32  private $eavConfig;
33 
37  private $catalogHelper;
38 
42  private $dateTime;
43 
47  private $localeDate;
48 
52  private $metadataPool;
53 
57  private $baseSelectProcessor;
58 
69  public function __construct(
70  \Magento\Store\Model\StoreManagerInterface $storeManager,
72  \Magento\Eav\Model\Config $eavConfig,
73  \Magento\Catalog\Helper\Data $catalogHelper,
74  \Magento\Framework\Stdlib\DateTime $dateTime,
75  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
76  \Magento\Framework\EntityManager\MetadataPool $metadataPool,
77  BaseSelectProcessorInterface $baseSelectProcessor = null
78  ) {
79  $this->storeManager = $storeManager;
80  $this->resource = $resourceConnection;
81  $this->eavConfig = $eavConfig;
82  $this->catalogHelper = $catalogHelper;
83  $this->dateTime = $dateTime;
84  $this->localeDate = $localeDate;
85  $this->metadataPool = $metadataPool;
86  $this->baseSelectProcessor = (null !== $baseSelectProcessor)
87  ? $baseSelectProcessor : ObjectManager::getInstance()->get(BaseSelectProcessorInterface::class);
88  }
89 
93  public function build($productId)
94  {
95  $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
96  $connection = $this->resource->getConnection();
97  $specialPriceAttribute = $this->eavConfig->getAttribute(Product::ENTITY, 'special_price');
98  $specialPriceFromDate = $this->eavConfig->getAttribute(Product::ENTITY, 'special_from_date');
99  $specialPriceToDate = $this->eavConfig->getAttribute(Product::ENTITY, 'special_to_date');
100  $timestamp = $this->localeDate->scopeTimeStamp($this->storeManager->getStore());
101  $currentDate = $this->dateTime->formatDate($timestamp, false);
102  $productTable = $this->resource->getTableName('catalog_product_entity');
103 
104  $specialPrice = $this->resource->getConnection()->select()
105  ->from(['parent' => $productTable], '')
106  ->joinInner(
107  ['link' => $this->resource->getTableName('catalog_product_relation')],
108  "link.parent_id = parent.$linkField",
109  []
110  )->joinInner(
112  sprintf('%s.entity_id = link.child_id', BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS),
113  ['entity_id']
114  )->joinInner(
115  ['t' => $specialPriceAttribute->getBackendTable()],
116  sprintf('t.%s = %s.%1$s', $linkField, BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS),
117  []
118  )->joinLeft(
119  ['special_from' => $specialPriceFromDate->getBackendTable()],
120  $connection->quoteInto(
121  "t.{$linkField} = special_from.{$linkField} AND special_from.attribute_id = ?",
122  $specialPriceFromDate->getAttributeId()
123  ),
124  ''
125  )->joinLeft(
126  ['special_to' => $specialPriceToDate->getBackendTable()],
127  $connection->quoteInto(
128  "t.{$linkField} = special_to.{$linkField} AND special_to.attribute_id = ?",
129  $specialPriceToDate->getAttributeId()
130  ),
131  ''
132  )->where('parent.entity_id = ?', $productId)
133  ->where('t.attribute_id = ?', $specialPriceAttribute->getAttributeId())
134  ->where('t.value IS NOT NULL')
135  ->where(
136  'special_from.value IS NULL OR ' . $connection->getDatePartSql('special_from.value') .' <= ?',
137  $currentDate
138  )->where(
139  'special_to.value IS NULL OR ' . $connection->getDatePartSql('special_to.value') .' >= ?',
140  $currentDate
141  )->order('t.value ' . Select::SQL_ASC)
142  ->order(BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS . '.' . $linkField . ' ' . Select::SQL_ASC)
143  ->limit(1);
144  $specialPrice = $this->baseSelectProcessor->process($specialPrice);
145 
146  if (!$this->catalogHelper->isPriceGlobal()) {
147  $priceSelectStore = clone $specialPrice;
148  $priceSelectStore->where('t.store_id = ?', $this->storeManager->getStore()->getId());
149  $selects[] = $priceSelectStore;
150  }
151 
152  $specialPrice->where('t.store_id = ?', Store::DEFAULT_STORE_ID);
153  $selects[] = $specialPrice;
154 
155  return $selects;
156  }
157 }
$storeManager
$connection
Definition: bulk.php:13
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\ResourceConnection $resourceConnection, \Magento\Eav\Model\Config $eavConfig, \Magento\Catalog\Helper\Data $catalogHelper, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Framework\EntityManager\MetadataPool $metadataPool, BaseSelectProcessorInterface $baseSelectProcessor=null)
$dateTime
const SQL_ASC
Definition: Select.php:81