Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DefaultConfigProvider.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Checkout\Model;
7 
9 use Magento\Checkout\Helper\Data as CheckoutHelper;
12 use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
13 use Magento\Customer\Model\Context as CustomerContext;
15 use Magento\Customer\Model\Url as CustomerUrlManager;
18 use Magento\Framework\App\Http\Context as HttpContext;
23 use Magento\Quote\Api\CartItemRepositoryInterface as QuoteItemRepository;
25 use Magento\Quote\Api\ShippingMethodManagementInterface as ShippingMethodManager;
26 use Magento\Quote\Model\QuoteIdMaskFactory;
28 
36 {
40  private $attributeOptionManager;
41 
45  private $checkoutHelper;
46 
50  private $checkoutSession;
51 
55  private $customerRepository;
56 
60  private $customerSession;
61 
65  private $customerUrlManager;
66 
70  private $httpContext;
71 
75  private $quoteRepository;
76 
80  private $quoteItemRepository;
81 
85  private $shippingMethodManager;
86 
90  private $configurationPool;
91 
96 
100  protected $localeFormat;
101 
105  protected $addressMapper;
106 
110  protected $addressConfig;
111 
115  protected $formKey;
116 
120  protected $imageHelper;
121 
125  protected $viewConfig;
126 
130  protected $postCodesConfig;
131 
135  protected $directoryHelper;
136 
140  protected $imageProvider;
141 
146 
150  protected $scopeConfig;
151 
156 
160  protected $storeManager;
161 
166 
170  protected $urlBuilder;
171 
175  private $addressMetadata;
176 
209  public function __construct(
210  CheckoutHelper $checkoutHelper,
211  CheckoutSession $checkoutSession,
212  CustomerRepository $customerRepository,
213  CustomerSession $customerSession,
214  CustomerUrlManager $customerUrlManager,
215  HttpContext $httpContext,
216  \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
217  QuoteItemRepository $quoteItemRepository,
218  ShippingMethodManager $shippingMethodManager,
219  ConfigurationPool $configurationPool,
220  QuoteIdMaskFactory $quoteIdMaskFactory,
221  LocaleFormat $localeFormat,
222  \Magento\Customer\Model\Address\Mapper $addressMapper,
223  \Magento\Customer\Model\Address\Config $addressConfig,
225  \Magento\Catalog\Helper\Image $imageHelper,
226  \Magento\Framework\View\ConfigInterface $viewConfig,
227  \Magento\Directory\Model\Country\Postcode\ConfigInterface $postCodesConfig,
229  \Magento\Directory\Helper\Data $directoryHelper,
234  \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement,
236  AddressMetadataInterface $addressMetadata = null,
237  AttributeOptionManagementInterface $attributeOptionManager = null
238  ) {
239  $this->checkoutHelper = $checkoutHelper;
240  $this->checkoutSession = $checkoutSession;
241  $this->customerRepository = $customerRepository;
242  $this->customerSession = $customerSession;
243  $this->customerUrlManager = $customerUrlManager;
244  $this->httpContext = $httpContext;
245  $this->quoteRepository = $quoteRepository;
246  $this->quoteItemRepository = $quoteItemRepository;
247  $this->shippingMethodManager = $shippingMethodManager;
248  $this->configurationPool = $configurationPool;
249  $this->quoteIdMaskFactory = $quoteIdMaskFactory;
250  $this->localeFormat = $localeFormat;
251  $this->addressMapper = $addressMapper;
252  $this->addressConfig = $addressConfig;
253  $this->formKey = $formKey;
254  $this->imageHelper = $imageHelper;
255  $this->viewConfig = $viewConfig;
256  $this->postCodesConfig = $postCodesConfig;
257  $this->imageProvider = $imageProvider;
258  $this->directoryHelper = $directoryHelper;
259  $this->cartTotalRepository = $cartTotalRepository;
260  $this->scopeConfig = $scopeConfig;
261  $this->shippingMethodConfig = $shippingMethodConfig;
262  $this->storeManager = $storeManager;
263  $this->paymentMethodManagement = $paymentMethodManagement;
264  $this->urlBuilder = $urlBuilder;
265  $this->addressMetadata = $addressMetadata ?: ObjectManager::getInstance()->get(AddressMetadataInterface::class);
266  $this->attributeOptionManager = $attributeOptionManager ??
267  ObjectManager::getInstance()->get(AttributeOptionManagementInterface::class);
268  }
269 
276  public function getConfig()
277  {
278  $quoteId = $this->checkoutSession->getQuote()->getId();
279  $output['formKey'] = $this->formKey->getFormKey();
280  $output['customerData'] = $this->getCustomerData();
281  $output['quoteData'] = $this->getQuoteData();
282  $output['quoteItemData'] = $this->getQuoteItemData();
283  $output['isCustomerLoggedIn'] = $this->isCustomerLoggedIn();
284  $output['selectedShippingMethod'] = $this->getSelectedShippingMethod();
285  $output['storeCode'] = $this->getStoreCode();
286  $output['isGuestCheckoutAllowed'] = $this->isGuestCheckoutAllowed();
287  $output['isCustomerLoginRequired'] = $this->isCustomerLoginRequired();
288  $output['registerUrl'] = $this->getRegisterUrl();
289  $output['checkoutUrl'] = $this->getCheckoutUrl();
290  $output['defaultSuccessPageUrl'] = $this->getDefaultSuccessPageUrl();
291  $output['pageNotFoundUrl'] = $this->pageNotFoundUrl();
292  $output['forgotPasswordUrl'] = $this->getForgotPasswordUrl();
293  $output['staticBaseUrl'] = $this->getStaticBaseUrl();
294  $output['priceFormat'] = $this->localeFormat->getPriceFormat(
295  null,
296  $this->checkoutSession->getQuote()->getQuoteCurrencyCode()
297  );
298  $output['basePriceFormat'] = $this->localeFormat->getPriceFormat(
299  null,
300  $this->checkoutSession->getQuote()->getBaseCurrencyCode()
301  );
302  $output['postCodes'] = $this->postCodesConfig->getPostCodes();
303  $output['imageData'] = $this->imageProvider->getImages($quoteId);
304  $output['totalsData'] = $this->getTotalsData();
305  $output['shippingPolicy'] = [
306  'isEnabled' => $this->scopeConfig->isSetFlag(
307  'shipping/shipping_policy/enable_shipping_policy',
309  ),
310  'shippingPolicyContent' => nl2br(
311  $this->scopeConfig->getValue(
312  'shipping/shipping_policy/shipping_policy_content',
314  )
315  )
316  ];
317  $output['activeCarriers'] = $this->getActiveCarriers();
318  $output['originCountryCode'] = $this->getOriginCountryCode();
319  $output['paymentMethods'] = $this->getPaymentMethods();
320  $output['autocomplete'] = $this->isAutocompleteEnabled();
321  $output['displayBillingOnPaymentMethod'] = $this->checkoutHelper->isDisplayBillingOnPaymentMethodAvailable();
322  return $output;
323  }
324 
331  private function isAutocompleteEnabled()
332  {
333  return $this->scopeConfig->getValue(
334  \Magento\Customer\Model\Form::XML_PATH_ENABLE_AUTOCOMPLETE,
336  ) ? 'on' : 'off';
337  }
338 
344  private function getCustomerData()
345  {
346  $customerData = [];
347  if ($this->isCustomerLoggedIn()) {
348  $customer = $this->customerRepository->getById($this->customerSession->getCustomerId());
349  $customerData = $customer->__toArray();
350  foreach ($customer->getAddresses() as $key => $address) {
351  $customerData['addresses'][$key]['inline'] = $this->getCustomerAddressInline($address);
352  if ($address->getCustomAttributes()) {
353  $customerData['addresses'][$key]['custom_attributes'] = $this->filterNotVisibleAttributes(
354  $customerData['addresses'][$key]['custom_attributes']
355  );
356  }
357  }
358  }
359  return $customerData;
360  }
361 
368  private function filterNotVisibleAttributes(array $attributes)
369  {
370  $attributesMetadata = $this->addressMetadata->getAllAttributesMetadata();
371  foreach ($attributesMetadata as $attributeMetadata) {
372  if (!$attributeMetadata->isVisible()) {
373  unset($attributes[$attributeMetadata->getAttributeCode()]);
374  }
375  }
376 
377  return $this->setLabelsToAttributes($attributes);
378  }
379 
386  private function getCustomerAddressInline($address)
387  {
388  $builtOutputAddressData = $this->addressMapper->toFlatArray($address);
389  return $this->addressConfig
390  ->getFormatByCode(\Magento\Customer\Model\Address\Config::DEFAULT_ADDRESS_FORMAT)
391  ->getRenderer()
392  ->renderArray($builtOutputAddressData);
393  }
394 
400  private function getQuoteData()
401  {
402  $quoteData = [];
403  if ($this->checkoutSession->getQuote()->getId()) {
404  $quote = $this->quoteRepository->get($this->checkoutSession->getQuote()->getId());
405  $quoteData = $quote->toArray();
406  $quoteData['is_virtual'] = $quote->getIsVirtual();
407 
408  if (!$quote->getCustomer()->getId()) {
410  $quoteIdMask = $this->quoteIdMaskFactory->create();
411  $quoteData['entity_id'] = $quoteIdMask->load(
412  $this->checkoutSession->getQuote()->getId(),
413  'quote_id'
414  )->getMaskedId();
415  }
416  }
417  return $quoteData;
418  }
419 
425  private function getQuoteItemData()
426  {
427  $quoteItemData = [];
428  $quoteId = $this->checkoutSession->getQuote()->getId();
429  if ($quoteId) {
430  $quoteItems = $this->quoteItemRepository->getList($quoteId);
431  foreach ($quoteItems as $index => $quoteItem) {
432  $quoteItemData[$index] = $quoteItem->toArray();
433  $quoteItemData[$index]['options'] = $this->getFormattedOptionValue($quoteItem);
434  $quoteItemData[$index]['thumbnail'] = $this->imageHelper->init(
435  $quoteItem->getProduct(),
436  'product_thumbnail_image'
437  )->getUrl();
438  }
439  }
440  return $quoteItemData;
441  }
442 
449  protected function getFormattedOptionValue($item)
450  {
451  $optionsData = [];
452  $options = $this->configurationPool->getByProductType($item->getProductType())->getOptions($item);
453  foreach ($options as $index => $optionValue) {
454  /* @var $helper \Magento\Catalog\Helper\Product\Configuration */
455  $helper = $this->configurationPool->getByProductType('default');
456  $params = [
457  'max_length' => 55,
458  'cut_replacer' => ' <a href="#" class="dots tooltip toggle" onclick="return false">...</a>'
459  ];
460  $option = $helper->getFormattedOptionValue($optionValue, $params);
462  $optionsData[$index]['label'] = $optionValue['label'];
463  }
464  return $optionsData;
465  }
466 
473  public function getRegisterUrl()
474  {
475  return $this->customerUrlManager->getRegisterUrl();
476  }
477 
484  public function getCheckoutUrl()
485  {
486  return $this->urlBuilder->getUrl('checkout');
487  }
488 
495  public function pageNotFoundUrl()
496  {
497  return $this->urlBuilder->getUrl('checkout/noroute');
498  }
499 
506  public function getDefaultSuccessPageUrl()
507  {
508  return $this->urlBuilder->getUrl('checkout/onepage/success/');
509  }
510 
516  private function getSelectedShippingMethod()
517  {
518  $shippingMethodData = null;
519  try {
520  $quoteId = $this->checkoutSession->getQuote()->getId();
521  $shippingMethod = $this->shippingMethodManager->get($quoteId);
522  if ($shippingMethod) {
523  $shippingMethodData = $shippingMethod->__toArray();
524  }
525  } catch (\Exception $exception) {
526  $shippingMethodData = null;
527  }
528  return $shippingMethodData;
529  }
530 
537  private function getStoreCode()
538  {
539  return $this->checkoutSession->getQuote()->getStore()->getCode();
540  }
541 
548  private function isGuestCheckoutAllowed()
549  {
550  return $this->checkoutHelper->isAllowedGuestCheckout($this->checkoutSession->getQuote());
551  }
552 
559  private function isCustomerLoggedIn()
560  {
561  return (bool)$this->httpContext->getValue(CustomerContext::CONTEXT_AUTH);
562  }
563 
570  private function isCustomerLoginRequired()
571  {
572  return $this->checkoutHelper->isCustomerMustBeLogged();
573  }
574 
581  private function getForgotPasswordUrl()
582  {
583  return $this->customerUrlManager->getForgotPasswordUrl();
584  }
585 
592  protected function getStaticBaseUrl()
593  {
594  return $this->checkoutSession->getQuote()->getStore()->getBaseUrl(UrlInterface::URL_TYPE_STATIC);
595  }
596 
602  private function getTotalsData()
603  {
605  $totals = $this->cartTotalRepository->get($this->checkoutSession->getQuote()->getId());
606  $items = [];
608  foreach ($totals->getItems() as $item) {
609  $items[] = $item->__toArray();
610  }
611  $totalSegmentsData = [];
613  foreach ($totals->getTotalSegments() as $totalSegment) {
614  $totalSegmentArray = $totalSegment->toArray();
615  if (is_object($totalSegment->getExtensionAttributes())) {
616  $totalSegmentArray['extension_attributes'] = $totalSegment->getExtensionAttributes()->__toArray();
617  }
618  $totalSegmentsData[] = $totalSegmentArray;
619  }
620  $totals->setItems($items);
621  $totals->setTotalSegments($totalSegmentsData);
622  $totalsArray = $totals->toArray();
623  if (is_object($totals->getExtensionAttributes())) {
624  $totalsArray['extension_attributes'] = $totals->getExtensionAttributes()->__toArray();
625  }
626  return $totalsArray;
627  }
628 
634  private function getActiveCarriers()
635  {
636  $activeCarriers = [];
637  foreach ($this->shippingMethodConfig->getActiveCarriers() as $carrier) {
638  $activeCarriers[] = $carrier->getCarrierCode();
639  }
640  return $activeCarriers;
641  }
642 
648  private function getOriginCountryCode()
649  {
650  return $this->scopeConfig->getValue(
651  \Magento\Shipping\Model\Config::XML_PATH_ORIGIN_COUNTRY_ID,
653  $this->storeManager->getStore()
654  );
655  }
656 
663  private function getPaymentMethods()
664  {
665  $paymentMethods = [];
666  $quote = $this->checkoutSession->getQuote();
667  if ($quote->getIsVirtual()) {
668  foreach ($this->paymentMethodManagement->getList($quote->getId()) as $paymentMethod) {
669  $paymentMethods[] = [
670  'code' => $paymentMethod->getCode(),
671  'title' => $paymentMethod->getTitle()
672  ];
673  }
674  }
675  return $paymentMethods;
676  }
677 
686  private function setLabelsToAttributes(array $customAttributes) : array
687  {
688  if (!empty($customAttributes)) {
689  foreach ($customAttributes as $customAttributeCode => $customAttribute) {
690  $attributeOptionLabels = $this->getAttributeLabels($customAttribute, $customAttributeCode);
691  if (!empty($attributeOptionLabels)) {
692  $customAttributes[$customAttributeCode]['label'] = implode(', ', $attributeOptionLabels);
693  }
694  }
695  }
696 
697  return $customAttributes;
698  }
699 
709  private function getAttributeLabels(array $customAttribute, string $customAttributeCode) : array
710  {
711  $attributeOptionLabels = [];
712 
713  if (!empty($customAttribute['value'])) {
714  $customAttributeValues = explode(',', $customAttribute['value']);
715  $attributeOptions = $this->attributeOptionManager->getItems(
716  \Magento\Customer\Model\Indexer\Address\AttributeProvider::ENTITY,
717  $customAttributeCode
718  );
719 
720  if (!empty($attributeOptions)) {
721  foreach ($attributeOptions as $attributeOption) {
722  $attributeOptionValue = $attributeOption->getValue();
723  if (in_array($attributeOptionValue, $customAttributeValues)) {
724  $attributeOptionLabels[] = $attributeOption->getLabel() ?? $attributeOptionValue;
725  }
726  }
727  }
728  }
729 
730  return $attributeOptionLabels;
731  }
732 }
__construct(CheckoutHelper $checkoutHelper, CheckoutSession $checkoutSession, CustomerRepository $customerRepository, CustomerSession $customerSession, CustomerUrlManager $customerUrlManager, HttpContext $httpContext, \Magento\Quote\Api\CartRepositoryInterface $quoteRepository, QuoteItemRepository $quoteItemRepository, ShippingMethodManager $shippingMethodManager, ConfigurationPool $configurationPool, QuoteIdMaskFactory $quoteIdMaskFactory, LocaleFormat $localeFormat, \Magento\Customer\Model\Address\Mapper $addressMapper, \Magento\Customer\Model\Address\Config $addressConfig, FormKey $formKey, \Magento\Catalog\Helper\Image $imageHelper, \Magento\Framework\View\ConfigInterface $viewConfig, \Magento\Directory\Model\Country\Postcode\ConfigInterface $postCodesConfig, Cart\ImageProvider $imageProvider, \Magento\Directory\Helper\Data $directoryHelper, CartTotalRepositoryInterface $cartTotalRepository, ScopeConfigInterface $scopeConfig, \Magento\Shipping\Model\Config $shippingMethodConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement, UrlInterface $urlBuilder, AddressMetadataInterface $addressMetadata=null, AttributeOptionManagementInterface $attributeOptionManager=null)
$helper
Definition: iframe.phtml:13
$customerData
$customer
Definition: customers.php:11
$quote
$customerRepository
$address
Definition: customer.php:38
$totals
Definition: totalbar.phtml:10
$quoteItem
Definition: quote.php:38
$attributes
Definition: matrix.phtml:13
$quoteRepository
$paymentMethods
$shippingMethod
Definition: popup.phtml:12
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$index
Definition: list.phtml:44
$items