Data.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Msrp\Helper;
  7. use Magento\Framework\App\Helper\AbstractHelper;
  8. use Magento\Framework\App\Helper\Context;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Msrp\Model\Product\Attribute\Source\Type;
  11. use Magento\Msrp\Pricing\MsrpPriceCalculatorInterface;
  12. use Magento\Store\Model\StoreManagerInterface;
  13. use Magento\Catalog\Model\Product;
  14. use Magento\Catalog\Api\ProductRepositoryInterface;
  15. /**
  16. * Msrp data helper
  17. *
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. */
  20. class Data extends AbstractHelper
  21. {
  22. /**
  23. * @var StoreManagerInterface
  24. */
  25. protected $storeManager;
  26. /**
  27. * @var \Magento\Msrp\Model\Product\Options
  28. */
  29. protected $productOptions;
  30. /**
  31. * @var \Magento\Msrp\Model\Config
  32. */
  33. protected $config;
  34. /**
  35. * @var \Magento\Framework\Pricing\PriceCurrencyInterface
  36. */
  37. protected $priceCurrency;
  38. /**
  39. * @var ProductRepositoryInterface
  40. */
  41. protected $productRepository;
  42. /**
  43. * @var MsrpPriceCalculatorInterface
  44. */
  45. private $msrpPriceCalculator;
  46. /**
  47. * @param Context $context
  48. * @param StoreManagerInterface $storeManager
  49. * @param \Magento\Msrp\Model\Product\Options $productOptions
  50. * @param \Magento\Msrp\Model\Msrp $msrp
  51. * @param \Magento\Msrp\Model\Config $config
  52. * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
  53. * @param ProductRepositoryInterface $productRepository
  54. * @param MsrpPriceCalculatorInterface|null $msrpPriceCalculator
  55. */
  56. public function __construct(
  57. Context $context,
  58. StoreManagerInterface $storeManager,
  59. \Magento\Msrp\Model\Product\Options $productOptions,
  60. \Magento\Msrp\Model\Msrp $msrp,
  61. \Magento\Msrp\Model\Config $config,
  62. \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
  63. ProductRepositoryInterface $productRepository,
  64. MsrpPriceCalculatorInterface $msrpPriceCalculator = null
  65. ) {
  66. parent::__construct($context);
  67. $this->storeManager = $storeManager;
  68. $this->productOptions = $productOptions;
  69. $this->msrp = $msrp;
  70. $this->config = $config;
  71. $this->priceCurrency = $priceCurrency;
  72. $this->productRepository = $productRepository;
  73. $this->msrpPriceCalculator = $msrpPriceCalculator
  74. ?: ObjectManager::getInstance()->get(MsrpPriceCalculatorInterface::class);
  75. }
  76. /**
  77. * Check if can apply Minimum Advertise price to product in specific visibility
  78. *
  79. * @param int|Product $product
  80. * @param int|null $visibility Check displaying price in concrete place (by default generally)
  81. * @return bool
  82. *
  83. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  84. * @throws \Magento\Framework\Exception\NoSuchEntityException
  85. */
  86. public function canApplyMsrp($product, $visibility = null)
  87. {
  88. if (!$this->config->isEnabled()) {
  89. return false;
  90. }
  91. if (is_numeric($product)) {
  92. $product = $this->productRepository->getById($product, false, $this->storeManager->getStore()->getId());
  93. }
  94. $result = $this->msrp->canApplyToProduct($product);
  95. if ($result && $visibility !== null) {
  96. $productPriceVisibility = $product->getMsrpDisplayActualPriceType();
  97. if ($productPriceVisibility == Type\Price::TYPE_USE_CONFIG) {
  98. $productPriceVisibility = $this->config->getDisplayActualPriceType();
  99. }
  100. $result = $productPriceVisibility == $visibility;
  101. }
  102. if ($product->getTypeInstance()->isComposite($product) && (!$result || $visibility !== null)) {
  103. $isEnabledInOptions = $this->productOptions->isEnabled($product, $visibility);
  104. if ($isEnabledInOptions !== null) {
  105. $result = $isEnabledInOptions;
  106. }
  107. }
  108. return $result;
  109. }
  110. /**
  111. * Get Msrp message for price
  112. *
  113. * @param Product $product
  114. * @return string
  115. * @throws \Magento\Framework\Exception\NoSuchEntityException
  116. */
  117. public function getMsrpPriceMessage($product)
  118. {
  119. $message = "";
  120. if ($this->canApplyMsrp($product, Type::TYPE_IN_CART)) {
  121. $message = __('To see product price, add this item to your cart. You can always remove it later.');
  122. } elseif ($this->canApplyMsrp($product, Type::TYPE_BEFORE_ORDER_CONFIRM)) {
  123. $message = __('See price before order confirmation.');
  124. }
  125. return $message;
  126. }
  127. /**
  128. * Check is product need gesture to show price
  129. *
  130. * @param int|Product $product
  131. * @return bool
  132. * @throws \Magento\Framework\Exception\NoSuchEntityException
  133. */
  134. public function isShowPriceOnGesture($product)
  135. {
  136. return $this->canApplyMsrp($product, Type::TYPE_ON_GESTURE);
  137. }
  138. /**
  139. * Check if we should show MAP proce before order confirmation
  140. *
  141. * @param int|Product $product
  142. * @return bool
  143. * @throws \Magento\Framework\Exception\NoSuchEntityException
  144. */
  145. public function isShowBeforeOrderConfirm($product)
  146. {
  147. return $this->canApplyMsrp($product, Type::TYPE_BEFORE_ORDER_CONFIRM);
  148. }
  149. /**
  150. * Check if any MAP price is larger than as low as value.
  151. *
  152. * @param int|Product $product
  153. * @return bool
  154. * @throws \Magento\Framework\Exception\NoSuchEntityException
  155. */
  156. public function isMinimalPriceLessMsrp($product)
  157. {
  158. if (is_numeric($product)) {
  159. $product = $this->productRepository->getById($product, false, $this->storeManager->getStore()->getId());
  160. }
  161. $msrp = $this->msrpPriceCalculator->getMsrpPriceValue($product);
  162. $price = $product->getPriceInfo()->getPrice(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE);
  163. if ($msrp) {
  164. $msrp = $this->priceCurrency->convertAndRound($msrp);
  165. }
  166. return $msrp > $price->getValue();
  167. }
  168. }