GiftMessageConfigProvider.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GiftMessage\Model;
  7. use Magento\Checkout\Model\ConfigProviderInterface;
  8. use Magento\GiftMessage\Helper\Message as GiftMessageHelper;
  9. use Magento\Framework\App\Http\Context as HttpContext;
  10. use Magento\Customer\Model\Context as CustomerContext;
  11. use Magento\Framework\UrlInterface;
  12. use Magento\Framework\Locale\FormatInterface as LocaleFormat;
  13. use Magento\Framework\Data\Form\FormKey;
  14. use Magento\Catalog\Model\Product\Attribute\Source\Boolean;
  15. use Magento\Store\Model\ScopeInterface;
  16. /**
  17. * Configuration provider for GiftMessage rendering on "Checkout cart" page.
  18. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  19. * @SuppressWarnings(PHPMD.NPathComplexity)
  20. */
  21. class GiftMessageConfigProvider implements ConfigProviderInterface
  22. {
  23. /**
  24. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  25. */
  26. protected $scopeConfiguration;
  27. /**
  28. * @var \Magento\GiftMessage\Api\CartRepositoryInterface
  29. */
  30. protected $cartRepository;
  31. /**
  32. * @var \Magento\GiftMessage\Api\ItemRepositoryInterface
  33. */
  34. protected $itemRepository;
  35. /**
  36. * @var \Magento\Checkout\Model\Session
  37. */
  38. protected $checkoutSession;
  39. /**
  40. * @var HttpContext
  41. */
  42. protected $httpContext;
  43. /**
  44. * @var \Magento\Store\Model\StoreManagerInterface
  45. */
  46. protected $storeManager;
  47. /**
  48. * @var LocaleFormat
  49. */
  50. protected $localeFormat;
  51. /**
  52. * @var FormKey
  53. */
  54. protected $formKey;
  55. /**
  56. * GiftMessageConfigProvider constructor.
  57. *
  58. * @param \Magento\Framework\App\Helper\Context $context
  59. * @param \Magento\GiftMessage\Api\CartRepositoryInterface $cartRepository
  60. * @param \Magento\GiftMessage\Api\ItemRepositoryInterface $itemRepository
  61. * @param \Magento\Checkout\Model\Session $checkoutSession
  62. * @param HttpContext $httpContext
  63. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  64. * @param LocaleFormat $localeFormat
  65. * @param FormKey $formKey
  66. */
  67. public function __construct(
  68. \Magento\Framework\App\Helper\Context $context,
  69. \Magento\GiftMessage\Api\CartRepositoryInterface $cartRepository,
  70. \Magento\GiftMessage\Api\ItemRepositoryInterface $itemRepository,
  71. \Magento\Checkout\Model\Session $checkoutSession,
  72. HttpContext $httpContext,
  73. \Magento\Store\Model\StoreManagerInterface $storeManager,
  74. LocaleFormat $localeFormat,
  75. FormKey $formKey
  76. ) {
  77. $this->scopeConfiguration = $context->getScopeConfig();
  78. $this->cartRepository = $cartRepository;
  79. $this->itemRepository = $itemRepository;
  80. $this->checkoutSession = $checkoutSession;
  81. $this->httpContext = $httpContext;
  82. $this->storeManager = $storeManager;
  83. $this->localeFormat = $localeFormat;
  84. $this->formKey = $formKey;
  85. }
  86. /**
  87. * @inheritdoc
  88. */
  89. public function getConfig()
  90. {
  91. $configuration = [];
  92. $configuration['giftMessage'] = [];
  93. $orderLevelGiftMsg = $this->scopeConfiguration->isSetFlag(
  94. GiftMessageHelper::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER,
  95. ScopeInterface::SCOPE_STORE
  96. );
  97. $itemLevelGiftMessage = $this->scopeConfiguration->isSetFlag(
  98. GiftMessageHelper::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS,
  99. ScopeInterface::SCOPE_STORE
  100. );
  101. if ($orderLevelGiftMsg) {
  102. $orderMessages = $this->getOrderLevelGiftMessages();
  103. $configuration['isOrderLevelGiftOptionsEnabled'] = (bool)$this->isQuoteVirtual() ? false : true;
  104. $configuration['giftMessage']['orderLevel'] = $orderMessages === null ? true : $orderMessages->getData();
  105. }
  106. $itemMessages = $this->getItemLevelGiftMessages();
  107. $configuration['isItemLevelGiftOptionsEnabled'] = $itemLevelGiftMessage;
  108. $configuration['giftMessage']['itemLevel'] = $itemMessages === null ? true : $itemMessages;
  109. $configuration['priceFormat'] = $this->localeFormat->getPriceFormat(
  110. null,
  111. $this->checkoutSession->getQuote()->getQuoteCurrencyCode()
  112. );
  113. $configuration['storeCode'] = $this->getStoreCode();
  114. $configuration['isCustomerLoggedIn'] = $this->isCustomerLoggedIn();
  115. $configuration['formKey'] = $this->formKey->getFormKey();
  116. $store = $this->storeManager->getStore();
  117. $configuration['baseUrl'] = $store->getBaseUrl(UrlInterface::URL_TYPE_LINK);
  118. return $configuration;
  119. }
  120. /**
  121. * Check if customer is logged in
  122. *
  123. * @return bool
  124. */
  125. private function isCustomerLoggedIn()
  126. {
  127. return (bool)$this->httpContext->getValue(CustomerContext::CONTEXT_AUTH);
  128. }
  129. /**
  130. * Retrieve store code
  131. *
  132. * @return string
  133. */
  134. protected function getStoreCode()
  135. {
  136. return $this->checkoutSession->getQuote()->getStore()->getCode();
  137. }
  138. /**
  139. * Check if quote is virtual
  140. *
  141. * @return bool
  142. */
  143. protected function isQuoteVirtual()
  144. {
  145. return $this->checkoutSession->getQuote()->getIsVirtual();
  146. }
  147. /**
  148. * Load already specified quote level gift message.
  149. *
  150. * @return \Magento\GiftMessage\Api\Data\MessageInterface|null
  151. */
  152. protected function getOrderLevelGiftMessages()
  153. {
  154. $cartId = $this->checkoutSession->getQuoteId();
  155. return $this->cartRepository->get($cartId);
  156. }
  157. /**
  158. * Load already specified item level gift messages and related configuration.
  159. *
  160. * @return \Magento\GiftMessage\Api\Data\MessageInterface[]|null
  161. * @throws \Magento\Framework\Exception\NoSuchEntityException
  162. */
  163. protected function getItemLevelGiftMessages()
  164. {
  165. $itemLevelConfig = [];
  166. $quote = $this->checkoutSession->getQuote();
  167. foreach ($quote->getAllVisibleItems() as $item) {
  168. $itemId = $item->getId();
  169. $itemLevelConfig[$itemId] = [];
  170. $isMessageAvailable = $item->getProduct()->getGiftMessageAvailable();
  171. // use gift message product setting if it is available
  172. if ($isMessageAvailable !== null && $isMessageAvailable != Boolean::VALUE_USE_CONFIG) {
  173. $itemLevelConfig[$itemId]['is_available'] = (bool)$isMessageAvailable;
  174. }
  175. $message = $this->itemRepository->get($quote->getId(), $itemId);
  176. if ($message) {
  177. $itemLevelConfig[$itemId]['message'] = $message->getData();
  178. }
  179. }
  180. return count($itemLevelConfig) === 0 ? null : $itemLevelConfig;
  181. }
  182. }