ValidationMessages.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Block\Cart;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\View\Element\Message\InterpretationStrategyInterface;
  9. /**
  10. * Shopping cart validation messages block
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class ValidationMessages extends \Magento\Framework\View\Element\Messages
  16. {
  17. /**
  18. * @var \Magento\Checkout\Helper\Cart
  19. */
  20. protected $cartHelper;
  21. /**
  22. * @var \Magento\Framework\Locale\CurrencyInterface
  23. */
  24. protected $currency;
  25. /**
  26. * @var \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage
  27. */
  28. private $minimumAmountErrorMessage;
  29. /**
  30. * @param \Magento\Framework\View\Element\Template\Context $context
  31. * @param \Magento\Framework\Message\Factory $messageFactory
  32. * @param \Magento\Framework\Message\CollectionFactory $collectionFactory
  33. * @param \Magento\Framework\Message\ManagerInterface $messageManager
  34. * @param InterpretationStrategyInterface $interpretationStrategy
  35. * @param \Magento\Checkout\Helper\Cart $cartHelper
  36. * @param \Magento\Framework\Locale\CurrencyInterface $currency
  37. * @param array $data
  38. * @codeCoverageIgnore
  39. */
  40. public function __construct(
  41. \Magento\Framework\View\Element\Template\Context $context,
  42. \Magento\Framework\Message\Factory $messageFactory,
  43. \Magento\Framework\Message\CollectionFactory $collectionFactory,
  44. \Magento\Framework\Message\ManagerInterface $messageManager,
  45. InterpretationStrategyInterface $interpretationStrategy,
  46. \Magento\Checkout\Helper\Cart $cartHelper,
  47. \Magento\Framework\Locale\CurrencyInterface $currency,
  48. array $data = []
  49. ) {
  50. parent::__construct(
  51. $context,
  52. $messageFactory,
  53. $collectionFactory,
  54. $messageManager,
  55. $interpretationStrategy,
  56. $data
  57. );
  58. $this->cartHelper = $cartHelper;
  59. $this->currency = $currency;
  60. }
  61. /**
  62. * @return $this
  63. */
  64. protected function _prepareLayout()
  65. {
  66. if ($this->cartHelper->getItemsCount()) {
  67. $this->validateMinimumAmount();
  68. $this->addQuoteMessages();
  69. $this->addMessages($this->messageManager->getMessages(true));
  70. }
  71. return parent::_prepareLayout();
  72. }
  73. /**
  74. * Validate minimum amount and display notice in error
  75. *
  76. * @return void
  77. */
  78. protected function validateMinimumAmount()
  79. {
  80. if (!$this->cartHelper->getQuote()->validateMinimumAmount()) {
  81. $this->messageManager->addNoticeMessage($this->getMinimumAmountErrorMessage()->getMessage());
  82. }
  83. }
  84. /**
  85. * @return \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage
  86. * @deprecated 100.1.0
  87. */
  88. private function getMinimumAmountErrorMessage()
  89. {
  90. if ($this->minimumAmountErrorMessage === null) {
  91. $objectManager = ObjectManager::getInstance();
  92. $this->minimumAmountErrorMessage = $objectManager->get(
  93. \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class
  94. );
  95. }
  96. return $this->minimumAmountErrorMessage;
  97. }
  98. /**
  99. * Add quote messages
  100. *
  101. * @return void
  102. */
  103. protected function addQuoteMessages()
  104. {
  105. // Compose array of messages to add
  106. $messages = [];
  107. /** @var \Magento\Framework\Message\MessageInterface $message */
  108. foreach ($this->cartHelper->getQuote()->getMessages() as $message) {
  109. if ($message) {
  110. // Escape HTML entities in quote message to prevent XSS
  111. $message->setText($this->escapeHtml($message->getText()));
  112. $messages[] = $message;
  113. }
  114. }
  115. if ($messages) {
  116. $this->messageManager->addUniqueMessages($messages);
  117. }
  118. }
  119. }