scopeConfiguration = $context->getScopeConfig(); $this->cartRepository = $cartRepository; $this->itemRepository = $itemRepository; $this->checkoutSession = $checkoutSession; $this->httpContext = $httpContext; $this->storeManager = $storeManager; $this->localeFormat = $localeFormat; $this->formKey = $formKey; } /** * @inheritdoc */ public function getConfig() { $configuration = []; $configuration['giftMessage'] = []; $orderLevelGiftMsg = $this->scopeConfiguration->isSetFlag( GiftMessageHelper::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER, ScopeInterface::SCOPE_STORE ); $itemLevelGiftMessage = $this->scopeConfiguration->isSetFlag( GiftMessageHelper::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS, ScopeInterface::SCOPE_STORE ); if ($orderLevelGiftMsg) { $orderMessages = $this->getOrderLevelGiftMessages(); $configuration['isOrderLevelGiftOptionsEnabled'] = (bool)$this->isQuoteVirtual() ? false : true; $configuration['giftMessage']['orderLevel'] = $orderMessages === null ? true : $orderMessages->getData(); } $itemMessages = $this->getItemLevelGiftMessages(); $configuration['isItemLevelGiftOptionsEnabled'] = $itemLevelGiftMessage; $configuration['giftMessage']['itemLevel'] = $itemMessages === null ? true : $itemMessages; $configuration['priceFormat'] = $this->localeFormat->getPriceFormat( null, $this->checkoutSession->getQuote()->getQuoteCurrencyCode() ); $configuration['storeCode'] = $this->getStoreCode(); $configuration['isCustomerLoggedIn'] = $this->isCustomerLoggedIn(); $configuration['formKey'] = $this->formKey->getFormKey(); $store = $this->storeManager->getStore(); $configuration['baseUrl'] = $store->getBaseUrl(UrlInterface::URL_TYPE_LINK); return $configuration; } /** * Check if customer is logged in * * @return bool */ private function isCustomerLoggedIn() { return (bool)$this->httpContext->getValue(CustomerContext::CONTEXT_AUTH); } /** * Retrieve store code * * @return string */ protected function getStoreCode() { return $this->checkoutSession->getQuote()->getStore()->getCode(); } /** * Check if quote is virtual * * @return bool */ protected function isQuoteVirtual() { return $this->checkoutSession->getQuote()->getIsVirtual(); } /** * Load already specified quote level gift message. * * @return \Magento\GiftMessage\Api\Data\MessageInterface|null */ protected function getOrderLevelGiftMessages() { $cartId = $this->checkoutSession->getQuoteId(); return $this->cartRepository->get($cartId); } /** * Load already specified item level gift messages and related configuration. * * @return \Magento\GiftMessage\Api\Data\MessageInterface[]|null * @throws \Magento\Framework\Exception\NoSuchEntityException */ protected function getItemLevelGiftMessages() { $itemLevelConfig = []; $quote = $this->checkoutSession->getQuote(); foreach ($quote->getAllVisibleItems() as $item) { $itemId = $item->getId(); $itemLevelConfig[$itemId] = []; $isMessageAvailable = $item->getProduct()->getGiftMessageAvailable(); // use gift message product setting if it is available if ($isMessageAvailable !== null && $isMessageAvailable != Boolean::VALUE_USE_CONFIG) { $itemLevelConfig[$itemId]['is_available'] = (bool)$isMessageAvailable; } $message = $this->itemRepository->get($quote->getId(), $itemId); if ($message) { $itemLevelConfig[$itemId]['message'] = $message->getData(); } } return count($itemLevelConfig) === 0 ? null : $itemLevelConfig; } }