_storeManager = $storeManager; $this->_resource = $resource; $this->_dateTime = $dateTime; $this->_localeDate = $localeDate; $this->_eavConfig = $eavConfig; $this->_processor = $processor; } /** * Retrieve write connection instance * * @return bool|\Magento\Framework\DB\Adapter\AdapterInterface */ protected function _getConnection() { if (null === $this->_connection) { $this->_connection = $this->_resource->getConnection(); } return $this->_connection; } /** * Add products to changes list with price which depends on date * * @return void */ public function execute() { $connection = $this->_getConnection(); foreach ($this->_storeManager->getStores(true) as $store) { $timestamp = $this->_localeDate->scopeTimeStamp($store); $currDate = $this->_dateTime->formatDate($timestamp, false); $currDateExpr = $connection->quote($currDate); // timestamp is locale based if (date('H', $timestamp) == '00') { $format = '%Y-%m-%d %H:%i:%s'; $this->_refreshSpecialPriceByStore( $store->getId(), 'special_from_date', $connection->getDateFormatSql($currDateExpr, $format) ); $dateTo = $connection->getDateAddSql( $currDateExpr, -1, \Magento\Framework\DB\Adapter\AdapterInterface::INTERVAL_DAY ); $this->_refreshSpecialPriceByStore( $store->getId(), 'special_to_date', $connection->getDateFormatSql($dateTo, $format) ); } } } /** * Reindex affected products * * @param int $storeId * @param string $attrCode * @param \Zend_Db_Expr $attrConditionValue * @return void */ protected function _refreshSpecialPriceByStore($storeId, $attrCode, $attrConditionValue) { $attribute = $this->_eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attrCode); $attributeId = $attribute->getAttributeId(); $linkField = $this->getMetadataPool()->getMetadata(CategoryInterface::class)->getLinkField(); $identifierField = $this->getMetadataPool()->getMetadata(CategoryInterface::class)->getIdentifierField(); $connection = $this->_getConnection(); $select = $connection->select()->from( ['attr' => $this->_resource->getTableName(['catalog_product_entity', 'datetime'])], [ $identifierField => 'cat.' . $identifierField, ] )->joinLeft( ['cat' => $this->_resource->getTableName('catalog_product_entity')], 'cat.' . $linkField . '= attr.' . $linkField, '' )->where( 'attr.attribute_id = ?', $attributeId )->where( 'attr.store_id = ?', $storeId )->where( 'attr.value = ?', $attrConditionValue ); $selectData = $connection->fetchCol($select, $identifierField); if (!empty($selectData)) { $this->_processor->getIndexer()->reindexList($selectData); } } /** * Get MetadataPool instance * @return MetadataPool * * @deprecated 101.0.0 */ private function getMetadataPool() { if (null === $this->metadataPool) { $this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class); } return $this->metadataPool; } }