Shipping.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Paypal Express Onepage checkout block for Shipping Address
  8. */
  9. namespace Magento\Paypal\Block\Express\Review;
  10. use Magento\Customer\Api\CustomerRepositoryInterface;
  11. use Magento\Quote\Model\Quote;
  12. class Shipping extends \Magento\Framework\View\Element\Template
  13. {
  14. /**
  15. * Sales Quote Shipping Address instance
  16. *
  17. * @var \Magento\Quote\Model\Quote\Address
  18. */
  19. protected $address = null;
  20. /**
  21. * @var \Magento\Quote\Model\Quote\AddressFactory
  22. */
  23. protected $addressFactory;
  24. /**
  25. * @var \Magento\Customer\Api\Data\CustomerInterface
  26. */
  27. protected $customer;
  28. /**
  29. * @var Quote
  30. */
  31. protected $quote;
  32. /**
  33. * @var \Magento\Checkout\Model\Session
  34. */
  35. protected $checkoutSession;
  36. /**
  37. * @var CustomerRepositoryInterface
  38. */
  39. protected $customerRepository;
  40. /**
  41. * @var \Magento\Framework\App\Http\Context
  42. */
  43. protected $httpContext;
  44. /**
  45. * @var \Magento\Customer\Model\Session
  46. */
  47. protected $customerSession;
  48. /**
  49. * @param \Magento\Framework\View\Element\Template\Context $context
  50. * @param \Magento\Customer\Model\Session $customerSession
  51. * @param \Magento\Checkout\Model\Session $resourceSession
  52. * @param CustomerRepositoryInterface $customerRepository
  53. * @param \Magento\Framework\App\Http\Context $httpContext
  54. * @param \Magento\Quote\Model\Quote\AddressFactory $addressFactory
  55. * @param array $data
  56. */
  57. public function __construct(
  58. \Magento\Framework\View\Element\Template\Context $context,
  59. \Magento\Customer\Model\Session $customerSession,
  60. \Magento\Checkout\Model\Session $resourceSession,
  61. CustomerRepositoryInterface $customerRepository,
  62. \Magento\Framework\App\Http\Context $httpContext,
  63. \Magento\Quote\Model\Quote\AddressFactory $addressFactory,
  64. array $data = []
  65. ) {
  66. $this->addressFactory = $addressFactory;
  67. $this->_isScopePrivate = true;
  68. $this->httpContext = $httpContext;
  69. $this->customerRepository = $customerRepository;
  70. $this->checkoutSession = $resourceSession;
  71. $this->customerSession = $customerSession;
  72. parent::__construct($context, $data);
  73. }
  74. /**
  75. * Initialize shipping address step
  76. *
  77. * @return void
  78. */
  79. protected function _construct()
  80. {
  81. $this->checkoutSession->setStepData(
  82. 'shipping',
  83. ['label' => __('Shipping Information'), 'is_show' => $this->isShow()]
  84. );
  85. parent::_construct();
  86. }
  87. /**
  88. * Return checkout method
  89. *
  90. * @return string
  91. */
  92. public function getMethod()
  93. {
  94. return $this->getQuote()->getCheckoutMethod();
  95. }
  96. /**
  97. * Retrieve is allow and show block
  98. *
  99. * @return bool
  100. */
  101. public function isShow()
  102. {
  103. return !$this->getQuote()->isVirtual();
  104. }
  105. /**
  106. * Return Sales Quote Address model (shipping address)
  107. *
  108. * @return \Magento\Quote\Model\Quote\Address
  109. */
  110. public function getAddress()
  111. {
  112. if ($this->address === null) {
  113. if ($this->isCustomerLoggedIn() || $this->getQuote()->getShippingAddress()) {
  114. $this->address = $this->getQuote()->getShippingAddress();
  115. } else {
  116. $this->address = $this->addressFactory->create();
  117. }
  118. }
  119. return $this->address;
  120. }
  121. /**
  122. * Get config
  123. *
  124. * @param string $path
  125. * @return string|null
  126. */
  127. public function getConfig($path)
  128. {
  129. return $this->_scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
  130. }
  131. /**
  132. * Get logged in customer
  133. *
  134. * @return \Magento\Customer\Api\Data\CustomerInterface
  135. */
  136. protected function _getCustomer()
  137. {
  138. if (empty($this->customer)) {
  139. $this->customer = $this->customerRepository->getById($this->customerSession->getCustomerId());
  140. }
  141. return $this->customer;
  142. }
  143. /**
  144. * Retrieve sales quote model
  145. *
  146. * @return Quote
  147. */
  148. public function getQuote()
  149. {
  150. if (empty($this->quote)) {
  151. $this->quote = $this->checkoutSession->getQuote();
  152. }
  153. return $this->quote;
  154. }
  155. /**
  156. * @return bool
  157. */
  158. public function isCustomerLoggedIn()
  159. {
  160. return $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
  161. }
  162. }