Billing.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 Billing Address
  8. */
  9. namespace Magento\Paypal\Block\Express\Review;
  10. use Magento\Customer\Api\CustomerRepositoryInterface;
  11. use Magento\Quote\Model\Quote;
  12. /**
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class Billing extends \Magento\Framework\View\Element\Template
  16. {
  17. /**
  18. * Sales Quote Billing Address instance
  19. *
  20. * @var \Magento\Quote\Model\Quote\Address
  21. */
  22. protected $address;
  23. /**
  24. * Customer Taxvat Widget block
  25. *
  26. * @var \Magento\Customer\Block\Widget\Taxvat
  27. */
  28. protected $taxvat;
  29. /**
  30. * @var \Magento\Quote\Model\Quote\AddressFactory
  31. */
  32. protected $addressFactory;
  33. /**
  34. * @var \Magento\Customer\Api\Data\CustomerInterface
  35. */
  36. protected $customer;
  37. /**
  38. * @var Quote
  39. */
  40. protected $quote;
  41. /**
  42. * @var \Magento\Checkout\Model\Session
  43. */
  44. protected $checkoutSession;
  45. /**
  46. * @var \Magento\Customer\Model\Session
  47. */
  48. protected $customerSession;
  49. /**
  50. * @var CustomerRepositoryInterface
  51. */
  52. protected $customerRepository;
  53. /**
  54. * @var \Magento\Framework\App\Http\Context
  55. */
  56. protected $httpContext;
  57. /**
  58. * @var \Magento\Directory\Model\ResourceModel\Country\CollectionFactory
  59. */
  60. protected $countryCollectionFactory;
  61. /**
  62. * @param \Magento\Framework\View\Element\Template\Context $context
  63. * @param \Magento\Customer\Model\Session $customerSession
  64. * @param \Magento\Checkout\Model\Session $resourceSession
  65. * @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory
  66. * @param CustomerRepositoryInterface $customerRepository
  67. * @param \Magento\Framework\App\Http\Context $httpContext
  68. * @param Quote\AddressFactory $addressFactory
  69. * @param array $data
  70. */
  71. public function __construct(
  72. \Magento\Framework\View\Element\Template\Context $context,
  73. \Magento\Customer\Model\Session $customerSession,
  74. \Magento\Checkout\Model\Session $resourceSession,
  75. \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory,
  76. CustomerRepositoryInterface $customerRepository,
  77. \Magento\Framework\App\Http\Context $httpContext,
  78. \Magento\Quote\Model\Quote\AddressFactory $addressFactory,
  79. array $data = []
  80. ) {
  81. $this->addressFactory = $addressFactory;
  82. $this->_isScopePrivate = true;
  83. $this->httpContext = $httpContext;
  84. $this->customerRepository = $customerRepository;
  85. $this->checkoutSession = $resourceSession;
  86. $this->customerSession = $customerSession;
  87. $this->countryCollectionFactory = $countryCollectionFactory;
  88. parent::__construct($context, $data);
  89. }
  90. /**
  91. * Initialize billing address step
  92. *
  93. * @return void
  94. */
  95. protected function _construct()
  96. {
  97. $this->getCheckout()->setStepData(
  98. 'billing',
  99. ['label' => __('Billing Information'), 'is_show' => true]
  100. );
  101. if ($this->isCustomerLoggedIn()) {
  102. $this->getCheckout()->setStepData('billing', 'allow', true);
  103. }
  104. parent::_construct();
  105. }
  106. /**
  107. * @return bool
  108. */
  109. public function isUseBillingAddressForShipping()
  110. {
  111. if ($this->getQuote()->getIsVirtual() || !$this->getQuote()->getShippingAddress()->getSameAsBilling()) {
  112. return false;
  113. }
  114. return true;
  115. }
  116. /**
  117. * Return country collection
  118. *
  119. * @return \Magento\Directory\Model\ResourceModel\Country\Collection
  120. */
  121. public function getCountries()
  122. {
  123. return $this->countryCollectionFactory->create()->loadByStore();
  124. }
  125. /**
  126. * Return checkout method
  127. *
  128. * @return string
  129. */
  130. public function getMethod()
  131. {
  132. return $this->getQuote()->getCheckoutMethod();
  133. }
  134. /**
  135. * Return Customer Address First Name
  136. * If Sales Quote Address First Name is not defined - return Customer First Name
  137. *
  138. * @return string
  139. */
  140. public function getFirstname()
  141. {
  142. return $this->getAddress()->getFirstname();
  143. }
  144. /**
  145. * Return Customer Address Last Name
  146. * If Sales Quote Address Last Name is not defined - return Customer Last Name
  147. *
  148. * @return string
  149. */
  150. public function getLastname()
  151. {
  152. return $this->getAddress()->getLastname();
  153. }
  154. /**
  155. * Check is Quote items can ship to
  156. *
  157. * @return bool
  158. */
  159. public function canShip()
  160. {
  161. return !$this->getQuote()->isVirtual();
  162. }
  163. /**
  164. * @return void
  165. */
  166. public function getSaveUrl()
  167. {
  168. }
  169. /**
  170. * Get Customer Taxvat Widget block
  171. *
  172. * @return \Magento\Customer\Block\Widget\Taxvat
  173. */
  174. protected function _getTaxvat()
  175. {
  176. if (!$this->taxvat) {
  177. $this->taxvat = $this->getLayout()->createBlock(\Magento\Customer\Block\Widget\Taxvat::class);
  178. }
  179. return $this->taxvat;
  180. }
  181. /**
  182. * Check whether taxvat is enabled
  183. *
  184. * @return bool
  185. */
  186. public function isTaxvatEnabled()
  187. {
  188. return $this->_getTaxvat()->isEnabled();
  189. }
  190. /**
  191. * @return string
  192. */
  193. public function getTaxvatHtml()
  194. {
  195. return $this->_getTaxvat()
  196. ->setTaxvat($this->getQuote()->getCustomerTaxvat())
  197. ->setFieldIdFormat('billing:%s')
  198. ->setFieldNameFormat('billing[%s]')
  199. ->toHtml();
  200. }
  201. /**
  202. * Return Sales Quote Address model
  203. *
  204. * @return \Magento\Quote\Model\Quote\Address
  205. */
  206. public function getAddress()
  207. {
  208. if ($this->address === null) {
  209. if ($this->isCustomerLoggedIn() || $this->getQuote()->getBillingAddress()) {
  210. $this->address = $this->getQuote()->getBillingAddress();
  211. if (!$this->address->getFirstname()) {
  212. $this->address->setFirstname($this->getQuote()->getCustomer()->getFirstname());
  213. }
  214. if (!$this->address->getLastname()) {
  215. $this->address->setLastname($this->getQuote()->getCustomer()->getLastname());
  216. }
  217. } else {
  218. $this->address = $this->addressFactory->create();
  219. }
  220. }
  221. return $this->address;
  222. }
  223. /**
  224. * Get config
  225. *
  226. * @param string $path
  227. * @return string|null
  228. */
  229. public function getConfig($path)
  230. {
  231. return $this->_scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
  232. }
  233. /**
  234. * Get logged in customer
  235. *
  236. * @return \Magento\Customer\Api\Data\CustomerInterface
  237. */
  238. protected function _getCustomer()
  239. {
  240. if (empty($this->customer)) {
  241. $this->customer = $this->customerRepository->getById($this->customerSession->getCustomerId());
  242. }
  243. return $this->customer;
  244. }
  245. /**
  246. * Retrieve checkout session model
  247. *
  248. * @return \Magento\Checkout\Model\Session
  249. */
  250. public function getCheckout()
  251. {
  252. return $this->checkoutSession;
  253. }
  254. /**
  255. * Retrieve sales quote model
  256. *
  257. * @return Quote
  258. */
  259. public function getQuote()
  260. {
  261. if (empty($this->quote)) {
  262. $this->quote = $this->getCheckout()->getQuote();
  263. }
  264. return $this->quote;
  265. }
  266. /**
  267. * @return bool
  268. */
  269. public function isCustomerLoggedIn()
  270. {
  271. return $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
  272. }
  273. }