DefaultConfigProvider.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Model;
  7. use Magento\Catalog\Helper\Product\ConfigurationPool;
  8. use Magento\Checkout\Helper\Data as CheckoutHelper;
  9. use Magento\Checkout\Model\Session as CheckoutSession;
  10. use Magento\Customer\Api\AddressMetadataInterface;
  11. use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
  12. use Magento\Customer\Model\Context as CustomerContext;
  13. use Magento\Customer\Model\Session as CustomerSession;
  14. use Magento\Customer\Model\Url as CustomerUrlManager;
  15. use Magento\Eav\Api\AttributeOptionManagementInterface;
  16. use Magento\Framework\Api\CustomAttributesDataInterface;
  17. use Magento\Framework\App\Config\ScopeConfigInterface;
  18. use Magento\Framework\App\Http\Context as HttpContext;
  19. use Magento\Framework\App\ObjectManager;
  20. use Magento\Framework\Data\Form\FormKey;
  21. use Magento\Framework\Locale\FormatInterface as LocaleFormat;
  22. use Magento\Framework\UrlInterface;
  23. use Magento\Quote\Api\CartItemRepositoryInterface as QuoteItemRepository;
  24. use Magento\Quote\Api\CartTotalRepositoryInterface;
  25. use Magento\Quote\Api\Data\AddressInterface;
  26. use Magento\Quote\Api\ShippingMethodManagementInterface as ShippingMethodManager;
  27. use Magento\Quote\Model\QuoteIdMaskFactory;
  28. use Magento\Store\Model\ScopeInterface;
  29. use Magento\Ui\Component\Form\Element\Multiline;
  30. /**
  31. * Default Config Provider
  32. *
  33. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  34. * @SuppressWarnings(PHPMD.TooManyFields)
  35. */
  36. class DefaultConfigProvider implements ConfigProviderInterface
  37. {
  38. /**
  39. * @var AttributeOptionManagementInterface
  40. */
  41. private $attributeOptionManager;
  42. /**
  43. * @var CheckoutHelper
  44. */
  45. private $checkoutHelper;
  46. /**
  47. * @var CheckoutSession
  48. */
  49. private $checkoutSession;
  50. /**
  51. * @var CustomerRepository
  52. */
  53. private $customerRepository;
  54. /**
  55. * @var CustomerSession
  56. */
  57. private $customerSession;
  58. /**
  59. * @var CustomerUrlManager
  60. */
  61. private $customerUrlManager;
  62. /**
  63. * @var HttpContext
  64. */
  65. private $httpContext;
  66. /**
  67. * @var \Magento\Quote\Api\CartRepositoryInterface
  68. */
  69. private $quoteRepository;
  70. /**
  71. * @var QuoteItemRepository
  72. */
  73. private $quoteItemRepository;
  74. /**
  75. * @var ShippingMethodManager
  76. */
  77. private $shippingMethodManager;
  78. /**
  79. * @var ConfigurationPool
  80. */
  81. private $configurationPool;
  82. /**
  83. * @param QuoteIdMaskFactory
  84. */
  85. protected $quoteIdMaskFactory;
  86. /**
  87. * @var LocaleFormat
  88. */
  89. protected $localeFormat;
  90. /**
  91. * @var \Magento\Customer\Model\Address\Mapper
  92. */
  93. protected $addressMapper;
  94. /**
  95. * @var \Magento\Customer\Model\Address\Config
  96. */
  97. protected $addressConfig;
  98. /**
  99. * @var FormKey
  100. */
  101. protected $formKey;
  102. /**
  103. * @var \Magento\Catalog\Helper\Image
  104. */
  105. protected $imageHelper;
  106. /**
  107. * @var \Magento\Framework\View\ConfigInterface
  108. */
  109. protected $viewConfig;
  110. /**
  111. * @var \Magento\Directory\Model\Country\Postcode\ConfigInterface
  112. */
  113. protected $postCodesConfig;
  114. /**
  115. * @var \Magento\Directory\Helper\Data
  116. */
  117. protected $directoryHelper;
  118. /**
  119. * @var Cart\ImageProvider
  120. */
  121. protected $imageProvider;
  122. /**
  123. * @var CartTotalRepositoryInterface
  124. */
  125. protected $cartTotalRepository;
  126. /**
  127. * @var ScopeConfigInterface
  128. */
  129. protected $scopeConfig;
  130. /**
  131. * @var \Magento\Shipping\Model\Config
  132. */
  133. protected $shippingMethodConfig;
  134. /**
  135. * @var \Magento\Store\Model\StoreManagerInterface
  136. */
  137. protected $storeManager;
  138. /**
  139. * @var \Magento\Quote\Api\PaymentMethodManagementInterface
  140. */
  141. protected $paymentMethodManagement;
  142. /**
  143. * @var UrlInterface
  144. */
  145. protected $urlBuilder;
  146. /**
  147. * @var AddressMetadataInterface
  148. */
  149. private $addressMetadata;
  150. /**
  151. * @param CheckoutHelper $checkoutHelper
  152. * @param Session $checkoutSession
  153. * @param CustomerRepository $customerRepository
  154. * @param CustomerSession $customerSession
  155. * @param CustomerUrlManager $customerUrlManager
  156. * @param HttpContext $httpContext
  157. * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
  158. * @param QuoteItemRepository $quoteItemRepository
  159. * @param ShippingMethodManager $shippingMethodManager
  160. * @param ConfigurationPool $configurationPool
  161. * @param QuoteIdMaskFactory $quoteIdMaskFactory
  162. * @param LocaleFormat $localeFormat
  163. * @param \Magento\Customer\Model\Address\Mapper $addressMapper
  164. * @param \Magento\Customer\Model\Address\Config $addressConfig
  165. * @param FormKey $formKey
  166. * @param \Magento\Catalog\Helper\Image $imageHelper
  167. * @param \Magento\Framework\View\ConfigInterface $viewConfig
  168. * @param \Magento\Directory\Model\Country\Postcode\ConfigInterface $postCodesConfig
  169. * @param Cart\ImageProvider $imageProvider
  170. * @param \Magento\Directory\Helper\Data $directoryHelper
  171. * @param CartTotalRepositoryInterface $cartTotalRepository
  172. * @param ScopeConfigInterface $scopeConfig
  173. * @param \Magento\Shipping\Model\Config $shippingMethodConfig
  174. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  175. * @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
  176. * @param UrlInterface $urlBuilder
  177. * @param AddressMetadataInterface $addressMetadata
  178. * @param AttributeOptionManagementInterface $attributeOptionManager
  179. * @codeCoverageIgnore
  180. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  181. */
  182. public function __construct(
  183. CheckoutHelper $checkoutHelper,
  184. CheckoutSession $checkoutSession,
  185. CustomerRepository $customerRepository,
  186. CustomerSession $customerSession,
  187. CustomerUrlManager $customerUrlManager,
  188. HttpContext $httpContext,
  189. \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
  190. QuoteItemRepository $quoteItemRepository,
  191. ShippingMethodManager $shippingMethodManager,
  192. ConfigurationPool $configurationPool,
  193. QuoteIdMaskFactory $quoteIdMaskFactory,
  194. LocaleFormat $localeFormat,
  195. \Magento\Customer\Model\Address\Mapper $addressMapper,
  196. \Magento\Customer\Model\Address\Config $addressConfig,
  197. FormKey $formKey,
  198. \Magento\Catalog\Helper\Image $imageHelper,
  199. \Magento\Framework\View\ConfigInterface $viewConfig,
  200. \Magento\Directory\Model\Country\Postcode\ConfigInterface $postCodesConfig,
  201. Cart\ImageProvider $imageProvider,
  202. \Magento\Directory\Helper\Data $directoryHelper,
  203. CartTotalRepositoryInterface $cartTotalRepository,
  204. ScopeConfigInterface $scopeConfig,
  205. \Magento\Shipping\Model\Config $shippingMethodConfig,
  206. \Magento\Store\Model\StoreManagerInterface $storeManager,
  207. \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement,
  208. UrlInterface $urlBuilder,
  209. AddressMetadataInterface $addressMetadata = null,
  210. AttributeOptionManagementInterface $attributeOptionManager = null
  211. ) {
  212. $this->checkoutHelper = $checkoutHelper;
  213. $this->checkoutSession = $checkoutSession;
  214. $this->customerRepository = $customerRepository;
  215. $this->customerSession = $customerSession;
  216. $this->customerUrlManager = $customerUrlManager;
  217. $this->httpContext = $httpContext;
  218. $this->quoteRepository = $quoteRepository;
  219. $this->quoteItemRepository = $quoteItemRepository;
  220. $this->shippingMethodManager = $shippingMethodManager;
  221. $this->configurationPool = $configurationPool;
  222. $this->quoteIdMaskFactory = $quoteIdMaskFactory;
  223. $this->localeFormat = $localeFormat;
  224. $this->addressMapper = $addressMapper;
  225. $this->addressConfig = $addressConfig;
  226. $this->formKey = $formKey;
  227. $this->imageHelper = $imageHelper;
  228. $this->viewConfig = $viewConfig;
  229. $this->postCodesConfig = $postCodesConfig;
  230. $this->imageProvider = $imageProvider;
  231. $this->directoryHelper = $directoryHelper;
  232. $this->cartTotalRepository = $cartTotalRepository;
  233. $this->scopeConfig = $scopeConfig;
  234. $this->shippingMethodConfig = $shippingMethodConfig;
  235. $this->storeManager = $storeManager;
  236. $this->paymentMethodManagement = $paymentMethodManagement;
  237. $this->urlBuilder = $urlBuilder;
  238. $this->addressMetadata = $addressMetadata ?: ObjectManager::getInstance()->get(AddressMetadataInterface::class);
  239. $this->attributeOptionManager = $attributeOptionManager ??
  240. ObjectManager::getInstance()->get(AttributeOptionManagementInterface::class);
  241. }
  242. /**
  243. * Return configuration array
  244. *
  245. * @return array|mixed
  246. * @throws \Magento\Framework\Exception\NoSuchEntityException
  247. * @throws \Magento\Framework\Exception\LocalizedException
  248. */
  249. public function getConfig()
  250. {
  251. $quote = $this->checkoutSession->getQuote();
  252. $quoteId = $quote->getId();
  253. $email = $quote->getShippingAddress()->getEmail();
  254. $quoteItemData = $this->getQuoteItemData();
  255. $output['formKey'] = $this->formKey->getFormKey();
  256. $output['customerData'] = $this->getCustomerData();
  257. $output['quoteData'] = $this->getQuoteData();
  258. $output['quoteItemData'] = $quoteItemData;
  259. $output['quoteMessages'] = $this->getQuoteItemsMessages($quoteItemData);
  260. $output['isCustomerLoggedIn'] = $this->isCustomerLoggedIn();
  261. $output['selectedShippingMethod'] = $this->getSelectedShippingMethod();
  262. if ($email && !$this->isCustomerLoggedIn()) {
  263. $shippingAddressFromData = $this->getAddressFromData($quote->getShippingAddress());
  264. $billingAddressFromData = $this->getAddressFromData($quote->getBillingAddress());
  265. $output['shippingAddressFromData'] = $shippingAddressFromData;
  266. if ($shippingAddressFromData != $billingAddressFromData) {
  267. $output['billingAddressFromData'] = $billingAddressFromData;
  268. }
  269. $output['validatedEmailValue'] = $email;
  270. }
  271. $output['storeCode'] = $this->getStoreCode();
  272. $output['isGuestCheckoutAllowed'] = $this->isGuestCheckoutAllowed();
  273. $output['isCustomerLoginRequired'] = $this->isCustomerLoginRequired();
  274. $output['registerUrl'] = $this->getRegisterUrl();
  275. $output['checkoutUrl'] = $this->getCheckoutUrl();
  276. $output['defaultSuccessPageUrl'] = $this->getDefaultSuccessPageUrl();
  277. $output['pageNotFoundUrl'] = $this->pageNotFoundUrl();
  278. $output['forgotPasswordUrl'] = $this->getForgotPasswordUrl();
  279. $output['staticBaseUrl'] = $this->getStaticBaseUrl();
  280. $output['priceFormat'] = $this->localeFormat->getPriceFormat(
  281. null,
  282. $quote->getQuoteCurrencyCode()
  283. );
  284. $output['basePriceFormat'] = $this->localeFormat->getPriceFormat(
  285. null,
  286. $quote->getBaseCurrencyCode()
  287. );
  288. $output['postCodes'] = $this->postCodesConfig->getPostCodes();
  289. $output['imageData'] = $this->imageProvider->getImages($quoteId);
  290. $output['totalsData'] = $this->getTotalsData();
  291. $output['shippingPolicy'] = [
  292. 'isEnabled' => $this->scopeConfig->isSetFlag(
  293. 'shipping/shipping_policy/enable_shipping_policy',
  294. ScopeInterface::SCOPE_STORE
  295. ),
  296. 'shippingPolicyContent' => nl2br(
  297. $this->scopeConfig->getValue(
  298. 'shipping/shipping_policy/shipping_policy_content',
  299. ScopeInterface::SCOPE_STORE
  300. )
  301. )
  302. ];
  303. $output['activeCarriers'] = $this->getActiveCarriers();
  304. $output['originCountryCode'] = $this->getOriginCountryCode();
  305. $output['paymentMethods'] = $this->getPaymentMethods();
  306. $output['autocomplete'] = $this->isAutocompleteEnabled();
  307. $output['displayBillingOnPaymentMethod'] = $this->checkoutHelper->isDisplayBillingOnPaymentMethodAvailable();
  308. return $output;
  309. }
  310. /**
  311. * Is autocomplete enabled for storefront
  312. *
  313. * @return string
  314. * @codeCoverageIgnore
  315. */
  316. private function isAutocompleteEnabled()
  317. {
  318. return $this->scopeConfig->getValue(
  319. \Magento\Customer\Model\Form::XML_PATH_ENABLE_AUTOCOMPLETE,
  320. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  321. ) ? 'on' : 'off';
  322. }
  323. /**
  324. * Retrieve customer data
  325. *
  326. * @return array
  327. */
  328. private function getCustomerData()
  329. {
  330. $customerData = [];
  331. if ($this->isCustomerLoggedIn()) {
  332. $customer = $this->customerRepository->getById($this->customerSession->getCustomerId());
  333. $customerData = $customer->__toArray();
  334. foreach ($customer->getAddresses() as $key => $address) {
  335. $customerData['addresses'][$key]['inline'] = $this->getCustomerAddressInline($address);
  336. if ($address->getCustomAttributes()) {
  337. $customerData['addresses'][$key]['custom_attributes'] = $this->filterNotVisibleAttributes(
  338. $customerData['addresses'][$key]['custom_attributes']
  339. );
  340. }
  341. }
  342. }
  343. return $customerData;
  344. }
  345. /**
  346. * Filter not visible on storefront custom attributes.
  347. *
  348. * @param array $attributes
  349. * @return array
  350. */
  351. private function filterNotVisibleAttributes(array $attributes)
  352. {
  353. $attributesMetadata = $this->addressMetadata->getAllAttributesMetadata();
  354. foreach ($attributesMetadata as $attributeMetadata) {
  355. if (!$attributeMetadata->isVisible()) {
  356. unset($attributes[$attributeMetadata->getAttributeCode()]);
  357. }
  358. }
  359. return $this->setLabelsToAttributes($attributes);
  360. }
  361. /**
  362. * Set additional customer address data
  363. *
  364. * @param \Magento\Customer\Api\Data\AddressInterface $address
  365. * @return string
  366. */
  367. private function getCustomerAddressInline($address)
  368. {
  369. $builtOutputAddressData = $this->addressMapper->toFlatArray($address);
  370. return $this->addressConfig
  371. ->getFormatByCode(\Magento\Customer\Model\Address\Config::DEFAULT_ADDRESS_FORMAT)
  372. ->getRenderer()
  373. ->renderArray($builtOutputAddressData);
  374. }
  375. /**
  376. * Retrieve quote data
  377. *
  378. * @return array
  379. */
  380. private function getQuoteData()
  381. {
  382. $quoteData = [];
  383. if ($this->checkoutSession->getQuote()->getId()) {
  384. $quote = $this->quoteRepository->get($this->checkoutSession->getQuote()->getId());
  385. $quoteData = $quote->toArray();
  386. $quoteData['is_virtual'] = $quote->getIsVirtual();
  387. if (!$quote->getCustomer()->getId()) {
  388. /** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */
  389. $quoteIdMask = $this->quoteIdMaskFactory->create();
  390. $quoteData['entity_id'] = $quoteIdMask->load(
  391. $this->checkoutSession->getQuote()->getId(),
  392. 'quote_id'
  393. )->getMaskedId();
  394. }
  395. }
  396. return $quoteData;
  397. }
  398. /**
  399. * Retrieve quote item data
  400. *
  401. * @return array
  402. */
  403. private function getQuoteItemData()
  404. {
  405. $quoteItemData = [];
  406. $quoteId = $this->checkoutSession->getQuote()->getId();
  407. if ($quoteId) {
  408. $quoteItems = $this->quoteItemRepository->getList($quoteId);
  409. foreach ($quoteItems as $index => $quoteItem) {
  410. $quoteItemData[$index] = $quoteItem->toArray();
  411. $quoteItemData[$index]['options'] = $this->getFormattedOptionValue($quoteItem);
  412. $quoteItemData[$index]['thumbnail'] = $this->imageHelper->init(
  413. $quoteItem->getProduct(),
  414. 'product_thumbnail_image'
  415. )->getUrl();
  416. $quoteItemData[$index]['message'] = $quoteItem->getMessage();
  417. }
  418. }
  419. return $quoteItemData;
  420. }
  421. /**
  422. * Retrieve formatted item options view
  423. *
  424. * @param \Magento\Quote\Api\Data\CartItemInterface $item
  425. * @return array
  426. */
  427. protected function getFormattedOptionValue($item)
  428. {
  429. $optionsData = [];
  430. $options = $this->configurationPool->getByProductType($item->getProductType())->getOptions($item);
  431. foreach ($options as $index => $optionValue) {
  432. /* @var $helper \Magento\Catalog\Helper\Product\Configuration */
  433. $helper = $this->configurationPool->getByProductType('default');
  434. $params = [
  435. 'max_length' => 55,
  436. 'cut_replacer' => ' <a href="#" class="dots tooltip toggle" onclick="return false">...</a>'
  437. ];
  438. $option = $helper->getFormattedOptionValue($optionValue, $params);
  439. $optionsData[$index] = $option;
  440. $optionsData[$index]['label'] = $optionValue['label'];
  441. }
  442. return $optionsData;
  443. }
  444. /**
  445. * Retrieve customer registration URL
  446. *
  447. * @return string
  448. * @codeCoverageIgnore
  449. */
  450. public function getRegisterUrl()
  451. {
  452. return $this->customerUrlManager->getRegisterUrl();
  453. }
  454. /**
  455. * Retrieve checkout URL
  456. *
  457. * @return string
  458. * @codeCoverageIgnore
  459. */
  460. public function getCheckoutUrl()
  461. {
  462. return $this->urlBuilder->getUrl('checkout');
  463. }
  464. /**
  465. * Retrieve checkout URL
  466. *
  467. * @return string
  468. * @codeCoverageIgnore
  469. */
  470. public function pageNotFoundUrl()
  471. {
  472. return $this->urlBuilder->getUrl('checkout/noroute');
  473. }
  474. /**
  475. * Retrieve default success page URL
  476. *
  477. * @return string
  478. * @codeCoverageIgnore
  479. */
  480. public function getDefaultSuccessPageUrl()
  481. {
  482. return $this->urlBuilder->getUrl('checkout/onepage/success/');
  483. }
  484. /**
  485. * Retrieve selected shipping method
  486. *
  487. * @return array|null
  488. */
  489. private function getSelectedShippingMethod()
  490. {
  491. $shippingMethodData = null;
  492. try {
  493. $quoteId = $this->checkoutSession->getQuote()->getId();
  494. $shippingMethod = $this->shippingMethodManager->get($quoteId);
  495. if ($shippingMethod) {
  496. $shippingMethodData = $shippingMethod->__toArray();
  497. }
  498. } catch (\Exception $exception) {
  499. $shippingMethodData = null;
  500. }
  501. return $shippingMethodData;
  502. }
  503. /**
  504. * Create address data appropriate to fill checkout address form
  505. *
  506. * @param AddressInterface $address
  507. * @return array
  508. * @throws \Magento\Framework\Exception\LocalizedException
  509. */
  510. private function getAddressFromData(AddressInterface $address)
  511. {
  512. $addressData = [];
  513. $attributesMetadata = $this->addressMetadata->getAllAttributesMetadata();
  514. foreach ($attributesMetadata as $attributeMetadata) {
  515. if (!$attributeMetadata->isVisible()) {
  516. continue;
  517. }
  518. $attributeCode = $attributeMetadata->getAttributeCode();
  519. $attributeData = $address->getData($attributeCode);
  520. if ($attributeData) {
  521. if ($attributeMetadata->getFrontendInput() === Multiline::NAME) {
  522. $attributeData = \is_array($attributeData) ? $attributeData : explode("\n", $attributeData);
  523. $attributeData = (object)$attributeData;
  524. }
  525. if ($attributeMetadata->isUserDefined()) {
  526. $addressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES][$attributeCode] = $attributeData;
  527. continue;
  528. }
  529. $addressData[$attributeCode] = $attributeData;
  530. }
  531. }
  532. return $addressData;
  533. }
  534. /**
  535. * Retrieve store code
  536. *
  537. * @return string
  538. * @codeCoverageIgnore
  539. */
  540. private function getStoreCode()
  541. {
  542. return $this->checkoutSession->getQuote()->getStore()->getCode();
  543. }
  544. /**
  545. * Check if guest checkout is allowed
  546. *
  547. * @return bool
  548. * @codeCoverageIgnore
  549. */
  550. private function isGuestCheckoutAllowed()
  551. {
  552. return $this->checkoutHelper->isAllowedGuestCheckout($this->checkoutSession->getQuote());
  553. }
  554. /**
  555. * Check if customer is logged in
  556. *
  557. * @return bool
  558. * @codeCoverageIgnore
  559. */
  560. private function isCustomerLoggedIn()
  561. {
  562. return (bool)$this->httpContext->getValue(CustomerContext::CONTEXT_AUTH);
  563. }
  564. /**
  565. * Check if customer must be logged in to proceed with checkout
  566. *
  567. * @return bool
  568. * @codeCoverageIgnore
  569. */
  570. private function isCustomerLoginRequired()
  571. {
  572. return $this->checkoutHelper->isCustomerMustBeLogged();
  573. }
  574. /**
  575. * Return forgot password URL
  576. *
  577. * @return string
  578. * @codeCoverageIgnore
  579. */
  580. private function getForgotPasswordUrl()
  581. {
  582. return $this->customerUrlManager->getForgotPasswordUrl();
  583. }
  584. /**
  585. * Return base static url.
  586. *
  587. * @return string
  588. * @codeCoverageIgnore
  589. */
  590. protected function getStaticBaseUrl()
  591. {
  592. return $this->checkoutSession->getQuote()->getStore()->getBaseUrl(UrlInterface::URL_TYPE_STATIC);
  593. }
  594. /**
  595. * Return quote totals data
  596. *
  597. * @return array
  598. */
  599. private function getTotalsData()
  600. {
  601. /** @var \Magento\Quote\Api\Data\TotalsInterface $totals */
  602. $totals = $this->cartTotalRepository->get($this->checkoutSession->getQuote()->getId());
  603. $items = [];
  604. /** @var \Magento\Quote\Model\Cart\Totals\Item $item */
  605. foreach ($totals->getItems() as $item) {
  606. $items[] = $item->__toArray();
  607. }
  608. $totalSegmentsData = [];
  609. /** @var \Magento\Quote\Model\Cart\TotalSegment $totalSegment */
  610. foreach ($totals->getTotalSegments() as $totalSegment) {
  611. $totalSegmentArray = $totalSegment->toArray();
  612. if (is_object($totalSegment->getExtensionAttributes())) {
  613. $totalSegmentArray['extension_attributes'] = $totalSegment->getExtensionAttributes()->__toArray();
  614. }
  615. $totalSegmentsData[] = $totalSegmentArray;
  616. }
  617. $totals->setItems($items);
  618. $totals->setTotalSegments($totalSegmentsData);
  619. $totalsArray = $totals->toArray();
  620. if (is_object($totals->getExtensionAttributes())) {
  621. $totalsArray['extension_attributes'] = $totals->getExtensionAttributes()->__toArray();
  622. }
  623. return $totalsArray;
  624. }
  625. /**
  626. * Returns active carriers codes
  627. *
  628. * @return array
  629. */
  630. private function getActiveCarriers()
  631. {
  632. $activeCarriers = [];
  633. foreach ($this->shippingMethodConfig->getActiveCarriers() as $carrier) {
  634. $activeCarriers[] = $carrier->getCarrierCode();
  635. }
  636. return $activeCarriers;
  637. }
  638. /**
  639. * Returns origin country code
  640. *
  641. * @return string
  642. */
  643. private function getOriginCountryCode()
  644. {
  645. return $this->scopeConfig->getValue(
  646. \Magento\Shipping\Model\Config::XML_PATH_ORIGIN_COUNTRY_ID,
  647. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  648. $this->storeManager->getStore()
  649. );
  650. }
  651. /**
  652. * Returns array of payment methods
  653. *
  654. * @return array $paymentMethods
  655. * @throws \Magento\Framework\Exception\NoSuchEntityException
  656. */
  657. private function getPaymentMethods()
  658. {
  659. $paymentMethods = [];
  660. $quote = $this->checkoutSession->getQuote();
  661. if ($quote->getIsVirtual()) {
  662. foreach ($this->paymentMethodManagement->getList($quote->getId()) as $paymentMethod) {
  663. $paymentMethods[] = [
  664. 'code' => $paymentMethod->getCode(),
  665. 'title' => $paymentMethod->getTitle()
  666. ];
  667. }
  668. }
  669. return $paymentMethods;
  670. }
  671. /**
  672. * Set Labels to custom Attributes
  673. *
  674. * @param array $customAttributes
  675. * @return array $customAttributes
  676. * @throws \Magento\Framework\Exception\InputException
  677. * @throws \Magento\Framework\Exception\StateException
  678. */
  679. private function setLabelsToAttributes(array $customAttributes) : array
  680. {
  681. if (!empty($customAttributes)) {
  682. foreach ($customAttributes as $customAttributeCode => $customAttribute) {
  683. $attributeOptionLabels = $this->getAttributeLabels($customAttribute, $customAttributeCode);
  684. if (!empty($attributeOptionLabels)) {
  685. $customAttributes[$customAttributeCode]['label'] = implode(', ', $attributeOptionLabels);
  686. }
  687. }
  688. }
  689. return $customAttributes;
  690. }
  691. /**
  692. * Get Labels by CustomAttribute and CustomAttributeCode
  693. *
  694. * @param array $customAttribute
  695. * @param string|integer $customAttributeCode
  696. * @return array $attributeOptionLabels
  697. * @throws \Magento\Framework\Exception\InputException
  698. * @throws \Magento\Framework\Exception\StateException
  699. */
  700. private function getAttributeLabels(array $customAttribute, string $customAttributeCode) : array
  701. {
  702. $attributeOptionLabels = [];
  703. if (!empty($customAttribute['value'])) {
  704. $customAttributeValues = explode(',', $customAttribute['value']);
  705. $attributeOptions = $this->attributeOptionManager->getItems(
  706. \Magento\Customer\Model\Indexer\Address\AttributeProvider::ENTITY,
  707. $customAttributeCode
  708. );
  709. if (!empty($attributeOptions)) {
  710. foreach ($attributeOptions as $attributeOption) {
  711. $attributeOptionValue = $attributeOption->getValue();
  712. if (in_array($attributeOptionValue, $customAttributeValues)) {
  713. $attributeOptionLabels[] = $attributeOption->getLabel() ?? $attributeOptionValue;
  714. }
  715. }
  716. }
  717. }
  718. return $attributeOptionLabels;
  719. }
  720. /**
  721. * Get notification messages for the quote items
  722. *
  723. * @param array $quoteItemData
  724. * @return array
  725. */
  726. private function getQuoteItemsMessages(array $quoteItemData): array
  727. {
  728. $quoteItemsMessages = [];
  729. if ($quoteItemData) {
  730. foreach ($quoteItemData as $item) {
  731. $quoteItemsMessages[$item['item_id']] = $item['message'];
  732. }
  733. }
  734. return $quoteItemsMessages;
  735. }
  736. }