totals.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /**
  6. * @api
  7. */
  8. define([
  9. 'ko',
  10. 'Magento_Checkout/js/model/quote',
  11. 'Magento_Customer/js/customer-data'
  12. ], function (ko, quote, customerData) {
  13. 'use strict';
  14. var quoteItems = ko.observable(quote.totals().items),
  15. cartData = customerData.get('cart'),
  16. quoteSubtotal = parseFloat(quote.totals().subtotal),
  17. subtotalAmount = parseFloat(cartData().subtotalAmount);
  18. quote.totals.subscribe(function (newValue) {
  19. quoteItems(newValue.items);
  20. });
  21. if (quoteSubtotal !== subtotalAmount) {
  22. customerData.reload(['cart'], false);
  23. }
  24. return {
  25. totals: quote.totals,
  26. isLoading: ko.observable(false),
  27. /**
  28. * @return {Function}
  29. */
  30. getItems: function () {
  31. return quoteItems;
  32. },
  33. /**
  34. * @param {*} code
  35. * @return {*}
  36. */
  37. getSegment: function (code) {
  38. var i, total;
  39. if (!this.totals()) {
  40. return null;
  41. }
  42. for (i in this.totals()['total_segments']) { //eslint-disable-line guard-for-in
  43. total = this.totals()['total_segments'][i];
  44. if (total.code == code) { //eslint-disable-line eqeqeq
  45. return total;
  46. }
  47. }
  48. return null;
  49. }
  50. };
  51. });