RefreshSpecialPrices.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Cron;
  7. use Magento\Catalog\Api\Data\CategoryInterface;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\Framework\EntityManager\MetadataPool;
  11. class RefreshSpecialPrices
  12. {
  13. /**
  14. * @var \Magento\Store\Model\StoreManagerInterface
  15. */
  16. protected $_storeManager;
  17. /**
  18. * @var Resource
  19. */
  20. protected $_resource;
  21. /**
  22. * @var \Magento\Framework\Stdlib\DateTime
  23. */
  24. protected $_dateTime;
  25. /**
  26. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  27. */
  28. protected $_localeDate;
  29. /**
  30. * @var \Magento\Eav\Model\Config
  31. */
  32. protected $_eavConfig;
  33. /**
  34. * @var \Magento\Catalog\Model\Indexer\Product\Price\Processor
  35. */
  36. protected $_processor;
  37. /**
  38. * @var \Magento\Framework\DB\Adapter\AdapterInterface
  39. */
  40. protected $_connection;
  41. /**
  42. * @var MetadataPool
  43. */
  44. private $metadataPool;
  45. /**
  46. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  47. * @param ResourceConnection $resource
  48. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  49. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  50. * @param \Magento\Eav\Model\Config $eavConfig
  51. * @param \Magento\Catalog\Model\Indexer\Product\Price\Processor $processor
  52. */
  53. public function __construct(
  54. \Magento\Store\Model\StoreManagerInterface $storeManager,
  55. ResourceConnection $resource,
  56. \Magento\Framework\Stdlib\DateTime $dateTime,
  57. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  58. \Magento\Eav\Model\Config $eavConfig,
  59. \Magento\Catalog\Model\Indexer\Product\Price\Processor $processor
  60. ) {
  61. $this->_storeManager = $storeManager;
  62. $this->_resource = $resource;
  63. $this->_dateTime = $dateTime;
  64. $this->_localeDate = $localeDate;
  65. $this->_eavConfig = $eavConfig;
  66. $this->_processor = $processor;
  67. }
  68. /**
  69. * Retrieve write connection instance
  70. *
  71. * @return bool|\Magento\Framework\DB\Adapter\AdapterInterface
  72. */
  73. protected function _getConnection()
  74. {
  75. if (null === $this->_connection) {
  76. $this->_connection = $this->_resource->getConnection();
  77. }
  78. return $this->_connection;
  79. }
  80. /**
  81. * Add products to changes list with price which depends on date
  82. *
  83. * @return void
  84. */
  85. public function execute()
  86. {
  87. $connection = $this->_getConnection();
  88. foreach ($this->_storeManager->getStores(true) as $store) {
  89. $timestamp = $this->_localeDate->scopeTimeStamp($store);
  90. $currDate = $this->_dateTime->formatDate($timestamp, false);
  91. $currDateExpr = $connection->quote($currDate);
  92. // timestamp is locale based
  93. if (date('H', $timestamp) == '00') {
  94. $format = '%Y-%m-%d %H:%i:%s';
  95. $this->_refreshSpecialPriceByStore(
  96. $store->getId(),
  97. 'special_from_date',
  98. $connection->getDateFormatSql($currDateExpr, $format)
  99. );
  100. $dateTo = $connection->getDateAddSql(
  101. $currDateExpr,
  102. -1,
  103. \Magento\Framework\DB\Adapter\AdapterInterface::INTERVAL_DAY
  104. );
  105. $this->_refreshSpecialPriceByStore(
  106. $store->getId(),
  107. 'special_to_date',
  108. $connection->getDateFormatSql($dateTo, $format)
  109. );
  110. }
  111. }
  112. }
  113. /**
  114. * Reindex affected products
  115. *
  116. * @param int $storeId
  117. * @param string $attrCode
  118. * @param \Zend_Db_Expr $attrConditionValue
  119. * @return void
  120. */
  121. protected function _refreshSpecialPriceByStore($storeId, $attrCode, $attrConditionValue)
  122. {
  123. $attribute = $this->_eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attrCode);
  124. $attributeId = $attribute->getAttributeId();
  125. $linkField = $this->getMetadataPool()->getMetadata(CategoryInterface::class)->getLinkField();
  126. $identifierField = $this->getMetadataPool()->getMetadata(CategoryInterface::class)->getIdentifierField();
  127. $connection = $this->_getConnection();
  128. $select = $connection->select()->from(
  129. ['attr' => $this->_resource->getTableName(['catalog_product_entity', 'datetime'])],
  130. [
  131. $identifierField => 'cat.' . $identifierField,
  132. ]
  133. )->joinLeft(
  134. ['cat' => $this->_resource->getTableName('catalog_product_entity')],
  135. 'cat.' . $linkField . '= attr.' . $linkField,
  136. ''
  137. )->where(
  138. 'attr.attribute_id = ?',
  139. $attributeId
  140. )->where(
  141. 'attr.store_id = ?',
  142. $storeId
  143. )->where(
  144. 'attr.value = ?',
  145. $attrConditionValue
  146. );
  147. $selectData = $connection->fetchCol($select, $identifierField);
  148. if (!empty($selectData)) {
  149. $this->_processor->getIndexer()->reindexList($selectData);
  150. }
  151. }
  152. /**
  153. * Get MetadataPool instance
  154. * @return MetadataPool
  155. *
  156. * @deprecated 101.0.0
  157. */
  158. private function getMetadataPool()
  159. {
  160. if (null === $this->metadataPool) {
  161. $this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
  162. }
  163. return $this->metadataPool;
  164. }
  165. }