Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GiftMessageConfigProvider.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\GiftMessage\Helper\Message as GiftMessageHelper;
10 use Magento\Framework\App\Http\Context as HttpContext;
11 use Magento\Customer\Model\Context as CustomerContext;
16 
23 {
28 
32  protected $cartRepository;
33 
37  protected $itemRepository;
38 
42  protected $checkoutSession;
43 
47  protected $storeManager;
48 
52  protected $localeFormat;
53 
57  protected $formKey;
58 
69  public function __construct(
70  \Magento\Framework\App\Helper\Context $context,
71  \Magento\GiftMessage\Api\CartRepositoryInterface $cartRepository,
72  \Magento\GiftMessage\Api\ItemRepositoryInterface $itemRepository,
73  \Magento\Checkout\Model\Session $checkoutSession,
74  HttpContext $httpContext,
75  \Magento\Store\Model\StoreManagerInterface $storeManager,
76  LocaleFormat $localeFormat,
78  ) {
79  $this->scopeConfiguration = $context->getScopeConfig();
80  $this->cartRepository = $cartRepository;
81  $this->itemRepository = $itemRepository;
82  $this->checkoutSession = $checkoutSession;
83  $this->httpContext = $httpContext;
84  $this->storeManager = $storeManager;
85  $this->localeFormat = $localeFormat;
86  $this->formKey = $formKey;
87  }
88 
92  public function getConfig()
93  {
94  $configuration = [];
95  $configuration['giftMessage'] = [];
96  $orderLevelGiftMessageConfiguration = (bool)$this->scopeConfiguration->getValue(
97  GiftMessageHelper::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER,
98  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
99  );
100  $itemLevelGiftMessageConfiguration = (bool)$this->scopeConfiguration->getValue(
101  GiftMessageHelper::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS,
102  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
103  );
104  if ($orderLevelGiftMessageConfiguration) {
105  $orderMessages = $this->getOrderLevelGiftMessages();
106  $configuration['isOrderLevelGiftOptionsEnabled'] = (bool)$this->isQuoteVirtual() ? false : true;
107  $configuration['giftMessage']['orderLevel'] = $orderMessages === null ? true : $orderMessages->getData();
108  }
109 
110  $itemMessages = $this->getItemLevelGiftMessages();
111  $configuration['isItemLevelGiftOptionsEnabled'] = $itemLevelGiftMessageConfiguration;
112  $configuration['giftMessage']['itemLevel'] = $itemMessages === null ? true : $itemMessages;
113 
114  $configuration['priceFormat'] = $this->localeFormat->getPriceFormat(
115  null,
116  $this->checkoutSession->getQuote()->getQuoteCurrencyCode()
117  );
118  $configuration['storeCode'] = $this->getStoreCode();
119  $configuration['isCustomerLoggedIn'] = $this->isCustomerLoggedIn();
120  $configuration['formKey'] = $this->formKey->getFormKey();
121  $store = $this->storeManager->getStore();
122  $configuration['baseUrl'] = $store->getBaseUrl(UrlInterface::URL_TYPE_LINK);
123  return $configuration;
124  }
125 
131  private function isCustomerLoggedIn()
132  {
133  return (bool)$this->httpContext->getValue(CustomerContext::CONTEXT_AUTH);
134  }
135 
141  protected function getStoreCode()
142  {
143  return $this->checkoutSession->getQuote()->getStore()->getCode();
144  }
145 
151  protected function isQuoteVirtual()
152  {
153  return $this->checkoutSession->getQuote()->getIsVirtual();
154  }
155 
161  protected function getOrderLevelGiftMessages()
162  {
163  $cartId = $this->checkoutSession->getQuoteId();
164  return $this->cartRepository->get($cartId);
165  }
166 
172  protected function getItemLevelGiftMessages()
173  {
174  $itemLevelConfig = [];
175  $quote = $this->checkoutSession->getQuote();
176  foreach ($quote->getAllVisibleItems() as $item) {
177  $itemId = $item->getId();
178  $itemLevelConfig[$itemId] = [];
179  $isMessageAvailable = $item->getProduct()->getGiftMessageAvailable();
180  // use gift message product setting if it is available
181  if ($isMessageAvailable !== null && $isMessageAvailable != Boolean::VALUE_USE_CONFIG) {
182  $itemLevelConfig[$itemId]['is_available'] = (bool)$isMessageAvailable;
183  }
184  $message = $this->itemRepository->get($quote->getId(), $itemId);
185  if ($message) {
186  $itemLevelConfig[$itemId]['message'] = $message->getData();
187  }
188  }
189  return count($itemLevelConfig) === 0 ? null : $itemLevelConfig;
190  }
191 }
return false
Definition: gallery.phtml:36
$configuration
Definition: index.php:33
$quote
$message
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\GiftMessage\Api\CartRepositoryInterface $cartRepository, \Magento\GiftMessage\Api\ItemRepositoryInterface $itemRepository, \Magento\Checkout\Model\Session $checkoutSession, HttpContext $httpContext, \Magento\Store\Model\StoreManagerInterface $storeManager, LocaleFormat $localeFormat, FormKey $formKey)
$cartId
Definition: quote.php:22