checkout-data.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. * @api
  9. */
  10. define([
  11. 'jquery',
  12. 'Magento_Customer/js/customer-data',
  13. 'jquery/jquery-storageapi'
  14. ], function ($, storage) {
  15. 'use strict';
  16. var cacheKey = 'checkout-data',
  17. /**
  18. * @param {Object} data
  19. */
  20. saveData = function (data) {
  21. storage.set(cacheKey, data);
  22. },
  23. /**
  24. * @return {*}
  25. */
  26. initData = function () {
  27. return {
  28. 'selectedShippingAddress': null, //Selected shipping address pulled from persistence storage
  29. 'shippingAddressFromData': null, //Shipping address pulled from persistence storage
  30. 'newCustomerShippingAddress': null, //Shipping address pulled from persistence storage for customer
  31. 'selectedShippingRate': null, //Shipping rate pulled from persistence storage
  32. 'selectedPaymentMethod': null, //Payment method pulled from persistence storage
  33. 'selectedBillingAddress': null, //Selected billing address pulled from persistence storage
  34. 'billingAddressFromData': null, //Billing address pulled from persistence storage
  35. 'newCustomerBillingAddress': null //Billing address pulled from persistence storage for new customer
  36. };
  37. },
  38. /**
  39. * @return {*}
  40. */
  41. getData = function () {
  42. var data = storage.get(cacheKey)();
  43. if ($.isEmptyObject(data)) {
  44. data = $.initNamespaceStorage('mage-cache-storage').localStorage.get(cacheKey);
  45. if ($.isEmptyObject(data)) {
  46. data = initData();
  47. saveData(data);
  48. }
  49. }
  50. return data;
  51. };
  52. return {
  53. /**
  54. * Setting the selected shipping address pulled from persistence storage
  55. *
  56. * @param {Object} data
  57. */
  58. setSelectedShippingAddress: function (data) {
  59. var obj = getData();
  60. obj.selectedShippingAddress = data;
  61. saveData(obj);
  62. },
  63. /**
  64. * Pulling the selected shipping address from persistence storage
  65. *
  66. * @return {*}
  67. */
  68. getSelectedShippingAddress: function () {
  69. return getData().selectedShippingAddress;
  70. },
  71. /**
  72. * Setting the shipping address pulled from persistence storage
  73. *
  74. * @param {Object} data
  75. */
  76. setShippingAddressFromData: function (data) {
  77. var obj = getData();
  78. obj.shippingAddressFromData = data;
  79. saveData(obj);
  80. },
  81. /**
  82. * Pulling the shipping address from persistence storage
  83. *
  84. * @return {*}
  85. */
  86. getShippingAddressFromData: function () {
  87. return getData().shippingAddressFromData;
  88. },
  89. /**
  90. * Setting the shipping address pulled from persistence storage for new customer
  91. *
  92. * @param {Object} data
  93. */
  94. setNewCustomerShippingAddress: function (data) {
  95. var obj = getData();
  96. obj.newCustomerShippingAddress = data;
  97. saveData(obj);
  98. },
  99. /**
  100. * Pulling the shipping address from persistence storage for new customer
  101. *
  102. * @return {*}
  103. */
  104. getNewCustomerShippingAddress: function () {
  105. return getData().newCustomerShippingAddress;
  106. },
  107. /**
  108. * Setting the selected shipping rate pulled from persistence storage
  109. *
  110. * @param {Object} data
  111. */
  112. setSelectedShippingRate: function (data) {
  113. var obj = getData();
  114. obj.selectedShippingRate = data;
  115. saveData(obj);
  116. },
  117. /**
  118. * Pulling the selected shipping rate from local storage
  119. *
  120. * @return {*}
  121. */
  122. getSelectedShippingRate: function () {
  123. return getData().selectedShippingRate;
  124. },
  125. /**
  126. * Setting the selected payment method pulled from persistence storage
  127. *
  128. * @param {Object} data
  129. */
  130. setSelectedPaymentMethod: function (data) {
  131. var obj = getData();
  132. obj.selectedPaymentMethod = data;
  133. saveData(obj);
  134. },
  135. /**
  136. * Pulling the payment method from persistence storage
  137. *
  138. * @return {*}
  139. */
  140. getSelectedPaymentMethod: function () {
  141. return getData().selectedPaymentMethod;
  142. },
  143. /**
  144. * Setting the selected billing address pulled from persistence storage
  145. *
  146. * @param {Object} data
  147. */
  148. setSelectedBillingAddress: function (data) {
  149. var obj = getData();
  150. obj.selectedBillingAddress = data;
  151. saveData(obj);
  152. },
  153. /**
  154. * Pulling the selected billing address from persistence storage
  155. *
  156. * @return {*}
  157. */
  158. getSelectedBillingAddress: function () {
  159. return getData().selectedBillingAddress;
  160. },
  161. /**
  162. * Setting the billing address pulled from persistence storage
  163. *
  164. * @param {Object} data
  165. */
  166. setBillingAddressFromData: function (data) {
  167. var obj = getData();
  168. obj.billingAddressFromData = data;
  169. saveData(obj);
  170. },
  171. /**
  172. * Pulling the billing address from persistence storage
  173. *
  174. * @return {*}
  175. */
  176. getBillingAddressFromData: function () {
  177. return getData().billingAddressFromData;
  178. },
  179. /**
  180. * Setting the billing address pulled from persistence storage for new customer
  181. *
  182. * @param {Object} data
  183. */
  184. setNewCustomerBillingAddress: function (data) {
  185. var obj = getData();
  186. obj.newCustomerBillingAddress = data;
  187. saveData(obj);
  188. },
  189. /**
  190. * Pulling the billing address from persistence storage for new customer
  191. *
  192. * @return {*}
  193. */
  194. getNewCustomerBillingAddress: function () {
  195. return getData().newCustomerBillingAddress;
  196. },
  197. /**
  198. * Pulling the email address from persistence storage
  199. *
  200. * @return {*}
  201. */
  202. getValidatedEmailValue: function () {
  203. var obj = getData();
  204. return obj.validatedEmailValue ? obj.validatedEmailValue : '';
  205. },
  206. /**
  207. * Setting the email address pulled from persistence storage
  208. *
  209. * @param {String} email
  210. */
  211. setValidatedEmailValue: function (email) {
  212. var obj = getData();
  213. obj.validatedEmailValue = email;
  214. saveData(obj);
  215. },
  216. /**
  217. * Pulling the email input field value from persistence storage
  218. *
  219. * @return {*}
  220. */
  221. getInputFieldEmailValue: function () {
  222. var obj = getData();
  223. return obj.inputFieldEmailValue ? obj.inputFieldEmailValue : '';
  224. },
  225. /**
  226. * Setting the email input field value pulled from persistence storage
  227. *
  228. * @param {String} email
  229. */
  230. setInputFieldEmailValue: function (email) {
  231. var obj = getData();
  232. obj.inputFieldEmailValue = email;
  233. saveData(obj);
  234. },
  235. /**
  236. * Pulling the checked email value from persistence storage
  237. *
  238. * @return {*}
  239. */
  240. getCheckedEmailValue: function () {
  241. var obj = getData();
  242. return obj.checkedEmailValue ? obj.checkedEmailValue : '';
  243. },
  244. /**
  245. * Setting the checked email value pulled from persistence storage
  246. *
  247. * @param {String} email
  248. */
  249. setCheckedEmailValue: function (email) {
  250. var obj = getData();
  251. obj.checkedEmailValue = email;
  252. saveData(obj);
  253. }
  254. };
  255. });