ProcessFrontFinalPriceObserver.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\Framework\Event\ObserverInterface;
  11. use Magento\Catalog\Model\Product;
  12. use Magento\CatalogRule\Model\Rule;
  13. use Magento\Store\Model\StoreManagerInterface;
  14. use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
  15. use Magento\Customer\Model\Session as CustomerModelSession;
  16. use Magento\Framework\Event\Observer as EventObserver;
  17. class ProcessFrontFinalPriceObserver implements ObserverInterface
  18. {
  19. /**
  20. * @var CustomerModelSession
  21. */
  22. protected $customerSession;
  23. /**
  24. * @var StoreManagerInterface
  25. */
  26. protected $storeManager;
  27. /**
  28. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  29. */
  30. protected $localeDate;
  31. /**
  32. * @var \Magento\CatalogRule\Model\ResourceModel\RuleFactory
  33. */
  34. protected $resourceRuleFactory;
  35. /**
  36. * @var \Magento\CatalogRule\Observer\RulePricesStorage
  37. */
  38. protected $rulePricesStorage;
  39. /**
  40. * @param RulePricesStorage $rulePricesStorage
  41. * @param \Magento\CatalogRule\Model\ResourceModel\RuleFactory $resourceRuleFactory
  42. * @param StoreManagerInterface $storeManager
  43. * @param TimezoneInterface $localeDate
  44. * @param CustomerModelSession $customerSession
  45. */
  46. public function __construct(
  47. RulePricesStorage $rulePricesStorage,
  48. \Magento\CatalogRule\Model\ResourceModel\RuleFactory $resourceRuleFactory,
  49. StoreManagerInterface $storeManager,
  50. TimezoneInterface $localeDate,
  51. CustomerModelSession $customerSession
  52. ) {
  53. $this->rulePricesStorage = $rulePricesStorage;
  54. $this->resourceRuleFactory = $resourceRuleFactory;
  55. $this->storeManager = $storeManager;
  56. $this->localeDate = $localeDate;
  57. $this->customerSession = $customerSession;
  58. }
  59. /**
  60. * Apply catalog price rules to product on frontend
  61. *
  62. * @param \Magento\Framework\Event\Observer $observer
  63. * @return $this
  64. */
  65. public function execute(\Magento\Framework\Event\Observer $observer)
  66. {
  67. $product = $observer->getEvent()->getProduct();
  68. $pId = $product->getId();
  69. $storeId = $product->getStoreId();
  70. if ($observer->hasDate()) {
  71. $date = new \DateTime($observer->getEvent()->getDate());
  72. } else {
  73. $date = $this->localeDate->scopeDate($storeId);
  74. }
  75. if ($observer->hasWebsiteId()) {
  76. $wId = $observer->getEvent()->getWebsiteId();
  77. } else {
  78. $wId = $this->storeManager->getStore($storeId)->getWebsiteId();
  79. }
  80. if ($observer->hasCustomerGroupId()) {
  81. $gId = $observer->getEvent()->getCustomerGroupId();
  82. } elseif ($product->hasCustomerGroupId()) {
  83. $gId = $product->getCustomerGroupId();
  84. } else {
  85. $gId = $this->customerSession->getCustomerGroupId();
  86. }
  87. $key = "{$date->format('Y-m-d H:i:s')}|{$wId}|{$gId}|{$pId}";
  88. if (!$this->rulePricesStorage->hasRulePrice($key)) {
  89. $rulePrice = $this->resourceRuleFactory->create()->getRulePrice($date, $wId, $gId, $pId);
  90. $this->rulePricesStorage->setRulePrice($key, $rulePrice);
  91. }
  92. if ($this->rulePricesStorage->getRulePrice($key) !== false) {
  93. $finalPrice = min($product->getData('final_price'), $this->rulePricesStorage->getRulePrice($key));
  94. $product->setFinalPrice($finalPrice);
  95. }
  96. return $this;
  97. }
  98. }