CatalogRulePrice.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogRule\Pricing\Price;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\CatalogRule\Model\ResourceModel\Rule;
  9. use Magento\CatalogRule\Model\ResourceModel\RuleFactory;
  10. use Magento\Customer\Model\Session;
  11. use Magento\Framework\App\ObjectManager;
  12. use Magento\Framework\Pricing\Adjustment\Calculator;
  13. use Magento\Framework\Pricing\Price\AbstractPrice;
  14. use Magento\Framework\Pricing\Price\BasePriceProviderInterface;
  15. use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
  16. use Magento\Store\Model\StoreManager;
  17. /**
  18. * Class CatalogRulePrice
  19. */
  20. class CatalogRulePrice extends AbstractPrice implements BasePriceProviderInterface
  21. {
  22. /**
  23. * Price type identifier string
  24. */
  25. const PRICE_CODE = 'catalog_rule_price';
  26. /**
  27. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  28. */
  29. protected $dateTime;
  30. /**
  31. * @var \Magento\Store\Model\StoreManager
  32. */
  33. protected $storeManager;
  34. /**
  35. * @var \Magento\Customer\Model\Session
  36. */
  37. protected $customerSession;
  38. /**
  39. * @var \Magento\CatalogRule\Model\ResourceModel\RuleFactory
  40. * @deprecated 100.0.2
  41. */
  42. protected $resourceRuleFactory;
  43. /**
  44. * @var \Magento\CatalogRule\Model\ResourceModel\Rule
  45. */
  46. private $ruleResource;
  47. /**
  48. * @param Product $saleableItem
  49. * @param float $quantity
  50. * @param Calculator $calculator
  51. * @param RuleFactory $catalogRuleResourceFactory
  52. * @param TimezoneInterface $dateTime
  53. * @param StoreManager $storeManager
  54. * @param Session $customerSession
  55. * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
  56. */
  57. public function __construct(
  58. Product $saleableItem,
  59. $quantity,
  60. Calculator $calculator,
  61. \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
  62. TimezoneInterface $dateTime,
  63. StoreManager $storeManager,
  64. Session $customerSession,
  65. RuleFactory $catalogRuleResourceFactory
  66. ) {
  67. parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
  68. $this->dateTime = $dateTime;
  69. $this->storeManager = $storeManager;
  70. $this->customerSession = $customerSession;
  71. $this->resourceRuleFactory = $catalogRuleResourceFactory;
  72. }
  73. /**
  74. * Returns catalog rule value
  75. *
  76. * @return float|boolean
  77. */
  78. public function getValue()
  79. {
  80. if (null === $this->value) {
  81. if ($this->product->hasData(self::PRICE_CODE)) {
  82. $this->value = (float)$this->product->getData(self::PRICE_CODE) ?: false;
  83. } else {
  84. $this->value = $this->getRuleResource()
  85. ->getRulePrice(
  86. $this->dateTime->scopeDate($this->storeManager->getStore()->getId()),
  87. $this->storeManager->getStore()->getWebsiteId(),
  88. $this->customerSession->getCustomerGroupId(),
  89. $this->product->getId()
  90. );
  91. $this->value = $this->value ? (float)$this->value : false;
  92. }
  93. if ($this->value) {
  94. $this->value = $this->priceCurrency->convertAndRound($this->value);
  95. }
  96. }
  97. return $this->value;
  98. }
  99. /**
  100. * @return Rule
  101. * @deprecated 100.1.1
  102. */
  103. private function getRuleResource()
  104. {
  105. if (null === $this->ruleResource) {
  106. $this->ruleResource = ObjectManager::getInstance()->get(Rule::class);
  107. }
  108. return $this->ruleResource;
  109. }
  110. }