PrepareCatalogProductCollectionPricesObserver.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Catalog Price rules observer model
  8. */
  9. namespace Magento\CatalogRule\Observer;
  10. use Magento\Catalog\Model\Product;
  11. use Magento\CatalogRule\Model\Rule;
  12. use Magento\Store\Model\StoreManagerInterface;
  13. use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
  14. use Magento\Customer\Model\Session as CustomerModelSession;
  15. use Magento\Framework\Event\Observer as EventObserver;
  16. use Magento\Customer\Api\GroupManagementInterface;
  17. use Magento\Framework\Event\ObserverInterface;
  18. /**
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class PrepareCatalogProductCollectionPricesObserver implements ObserverInterface
  22. {
  23. /**
  24. * @var CustomerModelSession
  25. */
  26. protected $customerSession;
  27. /**
  28. * @var StoreManagerInterface
  29. */
  30. protected $storeManager;
  31. /**
  32. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  33. */
  34. protected $localeDate;
  35. /**
  36. * @var \Magento\CatalogRule\Model\ResourceModel\RuleFactory
  37. */
  38. protected $resourceRuleFactory;
  39. /**
  40. * @var \Magento\CatalogRule\Observer\RulePricesStorage
  41. */
  42. protected $rulePricesStorage;
  43. /**
  44. * @var GroupManagementInterface
  45. */
  46. protected $groupManagement;
  47. /**
  48. * @param RulePricesStorage $rulePricesStorage
  49. * @param \Magento\CatalogRule\Model\ResourceModel\RuleFactory $resourceRuleFactory
  50. * @param StoreManagerInterface $storeManager
  51. * @param TimezoneInterface $localeDate
  52. * @param CustomerModelSession $customerSession
  53. * @param GroupManagementInterface $groupManagement
  54. */
  55. public function __construct(
  56. RulePricesStorage $rulePricesStorage,
  57. \Magento\CatalogRule\Model\ResourceModel\RuleFactory $resourceRuleFactory,
  58. StoreManagerInterface $storeManager,
  59. TimezoneInterface $localeDate,
  60. CustomerModelSession $customerSession,
  61. GroupManagementInterface $groupManagement
  62. ) {
  63. $this->rulePricesStorage = $rulePricesStorage;
  64. $this->resourceRuleFactory = $resourceRuleFactory;
  65. $this->storeManager = $storeManager;
  66. $this->localeDate = $localeDate;
  67. $this->customerSession = $customerSession;
  68. $this->groupManagement = $groupManagement;
  69. }
  70. /**
  71. * Apply catalog price rules to product on frontend
  72. *
  73. * @param \Magento\Framework\Event\Observer $observer
  74. * @return $this
  75. */
  76. public function execute(\Magento\Framework\Event\Observer $observer)
  77. {
  78. /* @var $collection ProductCollection */
  79. $collection = $observer->getEvent()->getCollection();
  80. $store = $this->storeManager->getStore($observer->getEvent()->getStoreId());
  81. $websiteId = $store->getWebsiteId();
  82. if ($observer->getEvent()->hasCustomerGroupId()) {
  83. $groupId = $observer->getEvent()->getCustomerGroupId();
  84. } else {
  85. if ($this->customerSession->isLoggedIn()) {
  86. $groupId = $this->customerSession->getCustomerGroupId();
  87. } else {
  88. $groupId = $this->groupManagement->getNotLoggedInGroup()->getId();
  89. }
  90. }
  91. if ($observer->getEvent()->hasDate()) {
  92. $date = new \DateTime($observer->getEvent()->getDate());
  93. } else {
  94. $date = (new \DateTime())->setTimestamp($this->localeDate->scopeTimeStamp($store));
  95. }
  96. $productIds = [];
  97. /* @var $product Product */
  98. foreach ($collection as $product) {
  99. $key = implode('|', [$date->format('Y-m-d H:i:s'), $websiteId, $groupId, $product->getId()]);
  100. if (!$this->rulePricesStorage->hasRulePrice($key)) {
  101. $productIds[] = $product->getId();
  102. }
  103. }
  104. if ($productIds) {
  105. $rulePrices = $this->resourceRuleFactory->create()->getRulePrices(
  106. $date,
  107. $websiteId,
  108. $groupId,
  109. $productIds
  110. );
  111. foreach ($productIds as $productId) {
  112. $key = implode('|', [$date->format('Y-m-d H:i:s'), $websiteId, $groupId, $productId]);
  113. $this->rulePricesStorage->setRulePrice(
  114. $key,
  115. isset($rulePrices[$productId]) ? $rulePrices[$productId] : false
  116. );
  117. }
  118. }
  119. return $this;
  120. }
  121. }