populate-shipping-address.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. define(
  16. [
  17. 'jquery',
  18. 'Magento_Checkout/js/model/address-converter',
  19. 'Magento_Checkout/js/model/quote',
  20. 'uiRegistry',
  21. 'Magento_Checkout/js/checkout-data',
  22. 'Magento_Checkout/js/model/checkout-data-resolver',
  23. 'Amazon_Payment/js/model/storage'
  24. ],
  25. function ($, addressConverter, quote, registry, checkoutData, checkoutDataResolver, amazonStorage) {
  26. 'use strict';
  27. /**
  28. * Populate shipping address form in shipping step from quote model *
  29. */
  30. function populateShippingForm() {
  31. var shippingAddressData = checkoutData.getShippingAddressFromData();
  32. registry.async('checkoutProvider')(function (checkoutProvider) {
  33. checkoutProvider.set(
  34. 'shippingAddress',
  35. $.extend({}, checkoutProvider.get('shippingAddress'), shippingAddressData)
  36. );
  37. });
  38. checkoutDataResolver.resolveShippingAddress();
  39. }
  40. /**
  41. * Populate shipping address form in shipping step from quote model
  42. * @private
  43. */
  44. return function () {
  45. //check to see if user is logged out of amazon (otherwise shipping form won't be in DOM)
  46. if (!amazonStorage.isAmazonAccountLoggedIn) {
  47. populateShippingForm();
  48. }
  49. //subscribe to logout and trigger shippingform population when logged out.
  50. amazonStorage.isAmazonAccountLoggedIn.subscribe(function (loggedIn) {
  51. if (!loggedIn) {
  52. populateShippingForm();
  53. }
  54. });
  55. };
  56. }
  57. );