checkout-data-resolver.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /**
  6. * Checkout adapter for customer data storage
  7. */
  8. define([
  9. 'Magento_Customer/js/model/address-list',
  10. 'Magento_Checkout/js/model/quote',
  11. 'Magento_Checkout/js/checkout-data',
  12. 'Magento_Checkout/js/action/create-shipping-address',
  13. 'Magento_Checkout/js/action/select-shipping-address',
  14. 'Magento_Checkout/js/action/select-shipping-method',
  15. 'Magento_Checkout/js/model/payment-service',
  16. 'Magento_Checkout/js/action/select-payment-method',
  17. 'Magento_Checkout/js/model/address-converter',
  18. 'Magento_Checkout/js/action/select-billing-address',
  19. 'Magento_Checkout/js/action/create-billing-address',
  20. 'underscore'
  21. ], function (
  22. addressList,
  23. quote,
  24. checkoutData,
  25. createShippingAddress,
  26. selectShippingAddress,
  27. selectShippingMethodAction,
  28. paymentService,
  29. selectPaymentMethodAction,
  30. addressConverter,
  31. selectBillingAddress,
  32. createBillingAddress,
  33. _
  34. ) {
  35. 'use strict';
  36. return {
  37. /**
  38. * Resolve estimation address. Used local storage
  39. */
  40. resolveEstimationAddress: function () {
  41. var address;
  42. if (checkoutData.getShippingAddressFromData()) {
  43. address = addressConverter.formAddressDataToQuoteAddress(checkoutData.getShippingAddressFromData());
  44. selectShippingAddress(address);
  45. } else {
  46. this.resolveShippingAddress();
  47. }
  48. if (quote.isVirtual()) {
  49. if (checkoutData.getBillingAddressFromData()) {
  50. address = addressConverter.formAddressDataToQuoteAddress(
  51. checkoutData.getBillingAddressFromData()
  52. );
  53. selectBillingAddress(address);
  54. } else {
  55. this.resolveBillingAddress();
  56. }
  57. }
  58. },
  59. /**
  60. * Resolve shipping address. Used local storage
  61. */
  62. resolveShippingAddress: function () {
  63. var newCustomerShippingAddress;
  64. if (!checkoutData.getShippingAddressFromData() &&
  65. window.checkoutConfig.shippingAddressFromData
  66. ) {
  67. checkoutData.setShippingAddressFromData(window.checkoutConfig.shippingAddressFromData);
  68. }
  69. newCustomerShippingAddress = checkoutData.getNewCustomerShippingAddress();
  70. if (newCustomerShippingAddress) {
  71. createShippingAddress(newCustomerShippingAddress);
  72. }
  73. this.applyShippingAddress();
  74. },
  75. /**
  76. * Apply resolved estimated address to quote
  77. *
  78. * @param {Object} isEstimatedAddress
  79. */
  80. applyShippingAddress: function (isEstimatedAddress) {
  81. var address,
  82. shippingAddress,
  83. isConvertAddress,
  84. addressData,
  85. isShippingAddressInitialized;
  86. if (addressList().length === 0) {
  87. address = addressConverter.formAddressDataToQuoteAddress(
  88. checkoutData.getShippingAddressFromData()
  89. );
  90. selectShippingAddress(address);
  91. }
  92. shippingAddress = quote.shippingAddress();
  93. isConvertAddress = isEstimatedAddress || false;
  94. if (!shippingAddress) {
  95. isShippingAddressInitialized = addressList.some(function (addressFromList) {
  96. if (checkoutData.getSelectedShippingAddress() == addressFromList.getKey()) { //eslint-disable-line
  97. addressData = isConvertAddress ?
  98. addressConverter.addressToEstimationAddress(addressFromList)
  99. : addressFromList;
  100. selectShippingAddress(addressData);
  101. return true;
  102. }
  103. return false;
  104. });
  105. if (!isShippingAddressInitialized) {
  106. isShippingAddressInitialized = addressList.some(function (addrs) {
  107. if (addrs.isDefaultShipping()) {
  108. addressData = isConvertAddress ?
  109. addressConverter.addressToEstimationAddress(addrs)
  110. : addrs;
  111. selectShippingAddress(addressData);
  112. return true;
  113. }
  114. return false;
  115. });
  116. }
  117. if (!isShippingAddressInitialized && addressList().length === 1) {
  118. addressData = isConvertAddress ?
  119. addressConverter.addressToEstimationAddress(addressList()[0])
  120. : addressList()[0];
  121. selectShippingAddress(addressData);
  122. }
  123. }
  124. },
  125. /**
  126. * @param {Object} ratesData
  127. */
  128. resolveShippingRates: function (ratesData) {
  129. var selectedShippingRate = checkoutData.getSelectedShippingRate(),
  130. availableRate = false;
  131. if (ratesData.length === 1) {
  132. //set shipping rate if we have only one available shipping rate
  133. selectShippingMethodAction(ratesData[0]);
  134. return;
  135. }
  136. if (quote.shippingMethod()) {
  137. availableRate = _.find(ratesData, function (rate) {
  138. return rate['carrier_code'] == quote.shippingMethod()['carrier_code'] && //eslint-disable-line
  139. rate['method_code'] == quote.shippingMethod()['method_code']; //eslint-disable-line eqeqeq
  140. });
  141. }
  142. if (!availableRate && selectedShippingRate) {
  143. availableRate = _.find(ratesData, function (rate) {
  144. return rate['carrier_code'] + '_' + rate['method_code'] === selectedShippingRate;
  145. });
  146. }
  147. if (!availableRate && window.checkoutConfig.selectedShippingMethod) {
  148. availableRate = window.checkoutConfig.selectedShippingMethod;
  149. selectShippingMethodAction(window.checkoutConfig.selectedShippingMethod);
  150. return;
  151. }
  152. //Unset selected shipping method if not available
  153. if (!availableRate) {
  154. selectShippingMethodAction(null);
  155. } else {
  156. selectShippingMethodAction(availableRate);
  157. }
  158. },
  159. /**
  160. * Resolve payment method. Used local storage
  161. */
  162. resolvePaymentMethod: function () {
  163. var availablePaymentMethods = paymentService.getAvailablePaymentMethods(),
  164. selectedPaymentMethod = checkoutData.getSelectedPaymentMethod();
  165. if (selectedPaymentMethod) {
  166. availablePaymentMethods.some(function (payment) {
  167. if (payment.method == selectedPaymentMethod) { //eslint-disable-line eqeqeq
  168. selectPaymentMethodAction(payment);
  169. }
  170. });
  171. }
  172. },
  173. /**
  174. * Resolve billing address. Used local storage
  175. */
  176. resolveBillingAddress: function () {
  177. var selectedBillingAddress,
  178. newCustomerBillingAddressData;
  179. if (!checkoutData.getBillingAddressFromData() &&
  180. window.checkoutConfig.billingAddressFromData
  181. ) {
  182. checkoutData.setBillingAddressFromData(window.checkoutConfig.billingAddressFromData);
  183. }
  184. selectedBillingAddress = checkoutData.getSelectedBillingAddress();
  185. newCustomerBillingAddressData = checkoutData.getNewCustomerBillingAddress();
  186. if (selectedBillingAddress) {
  187. if (selectedBillingAddress == 'new-customer-address' && newCustomerBillingAddressData) { //eslint-disable-line
  188. selectBillingAddress(createBillingAddress(newCustomerBillingAddressData));
  189. } else {
  190. addressList.some(function (address) {
  191. if (selectedBillingAddress == address.getKey()) { //eslint-disable-line eqeqeq
  192. selectBillingAddress(address);
  193. }
  194. });
  195. }
  196. } else {
  197. this.applyBillingAddress();
  198. }
  199. },
  200. /**
  201. * Apply resolved billing address to quote
  202. */
  203. applyBillingAddress: function () {
  204. var shippingAddress,
  205. isBillingAddressInitialized;
  206. if (quote.billingAddress()) {
  207. selectBillingAddress(quote.billingAddress());
  208. return;
  209. }
  210. if (quote.isVirtual()) {
  211. isBillingAddressInitialized = addressList.some(function (addrs) {
  212. if (addrs.isDefaultBilling()) {
  213. selectBillingAddress(addrs);
  214. return true;
  215. }
  216. return false;
  217. });
  218. }
  219. shippingAddress = quote.shippingAddress();
  220. if (!isBillingAddressInitialized &&
  221. shippingAddress &&
  222. shippingAddress.canUseForBilling() &&
  223. (shippingAddress.isDefaultShipping() || !quote.isVirtual())
  224. ) {
  225. //set billing address same as shipping by default if it is not empty
  226. selectBillingAddress(quote.shippingAddress());
  227. }
  228. }
  229. };
  230. });