ProcessAdminFinalPriceObserver.php 3.2 KB

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