Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RefreshSpecialPrices.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Catalog\Cron;
7 
12 
14 {
18  protected $_storeManager;
19 
23  protected $_resource;
24 
28  protected $_dateTime;
29 
33  protected $_localeDate;
34 
38  protected $_eavConfig;
39 
43  protected $_processor;
44 
48  protected $_connection;
49 
53  private $metadataPool;
54 
63  public function __construct(
64  \Magento\Store\Model\StoreManagerInterface $storeManager,
66  \Magento\Framework\Stdlib\DateTime $dateTime,
67  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
68  \Magento\Eav\Model\Config $eavConfig,
69  \Magento\Catalog\Model\Indexer\Product\Price\Processor $processor
70  ) {
71  $this->_storeManager = $storeManager;
72  $this->_resource = $resource;
73  $this->_dateTime = $dateTime;
74  $this->_localeDate = $localeDate;
75  $this->_eavConfig = $eavConfig;
76  $this->_processor = $processor;
77  }
78 
84  protected function _getConnection()
85  {
86  if (null === $this->_connection) {
87  $this->_connection = $this->_resource->getConnection();
88  }
89  return $this->_connection;
90  }
91 
97  public function execute()
98  {
99  $connection = $this->_getConnection();
100 
101  foreach ($this->_storeManager->getStores(true) as $store) {
102  $timestamp = $this->_localeDate->scopeTimeStamp($store);
103  $currDate = $this->_dateTime->formatDate($timestamp, false);
104  $currDateExpr = $connection->quote($currDate);
105 
106  // timestamp is locale based
107  if (date('H', $timestamp) == '00') {
108  $format = '%Y-%m-%d %H:%i:%s';
110  $store->getId(),
111  'special_from_date',
112  $connection->getDateFormatSql($currDateExpr, $format)
113  );
114 
115  $dateTo = $connection->getDateAddSql(
116  $currDateExpr,
117  -1,
118  \Magento\Framework\DB\Adapter\AdapterInterface::INTERVAL_DAY
119  );
121  $store->getId(),
122  'special_to_date',
123  $connection->getDateFormatSql($dateTo, $format)
124  );
125  }
126  }
127  }
128 
137  protected function _refreshSpecialPriceByStore($storeId, $attrCode, $attrConditionValue)
138  {
139  $attribute = $this->_eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attrCode);
140  $attributeId = $attribute->getAttributeId();
141 
142  $linkField = $this->getMetadataPool()->getMetadata(CategoryInterface::class)->getLinkField();
143  $identifierField = $this->getMetadataPool()->getMetadata(CategoryInterface::class)->getIdentifierField();
144 
145  $connection = $this->_getConnection();
146 
147  $select = $connection->select()->from(
148  ['attr' => $this->_resource->getTableName(['catalog_product_entity', 'datetime'])],
149  [
150  $identifierField => 'cat.' . $identifierField,
151  ]
152  )->joinLeft(
153  ['cat' => $this->_resource->getTableName('catalog_product_entity')],
154  'cat.' . $linkField . '= attr.' . $linkField,
155  ''
156  )->where(
157  'attr.attribute_id = ?',
158  $attributeId
159  )->where(
160  'attr.store_id = ?',
161  $storeId
162  )->where(
163  'attr.value = ?',
164  $attrConditionValue
165  );
166 
167  $selectData = $connection->fetchCol($select, $identifierField);
168 
169  if (!empty($selectData)) {
170  $this->_processor->getIndexer()->reindexList($selectData);
171  }
172  }
173 
180  private function getMetadataPool()
181  {
182  if (null === $this->metadataPool) {
183  $this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
184  }
185  return $this->metadataPool;
186  }
187 }
$processor
Definition: 404.php:10
$storeManager
$resource
Definition: bulk.php:12
$format
Definition: list.phtml:12
_refreshSpecialPriceByStore($storeId, $attrCode, $attrConditionValue)
$connection
Definition: bulk.php:13
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager, ResourceConnection $resource, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Eav\Model\Config $eavConfig, \Magento\Catalog\Model\Indexer\Product\Price\Processor $processor)
$dateTime