Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. define(
  2. [
  3. 'ko',
  4. 'jquery',
  5. 'Magento_Ui/js/form/form',
  6. 'uiRegistry',
  7. 'Magento_Checkout/js/model/quote',
  8. 'Magento_Checkout/js/model/resource-url-manager',
  9. 'mage/storage',
  10. 'Magento_Checkout/js/action/create-shipping-address',
  11. 'Magento_Checkout/js/model/error-processor',
  12. 'Magento_Checkout/js/model/shipping-service',
  13. 'Magento_Checkout/js/model/cart/totals-processor/default',
  14. 'Magento_Checkout/js/model/shipping-rate-registry'
  15. ],
  16. function (
  17. ko,
  18. $,
  19. Component,
  20. uiRegistry,
  21. quote,
  22. resourceUrlManager,
  23. storage,
  24. createShippingAddress,
  25. errorProcessor,
  26. shippingService,
  27. totalsDefaultProvider,
  28. rateRegistry
  29. ) {
  30. 'use strict';
  31.  
  32. var ClickAndCollectConfig = window.checkout.clickandcollect;
  33.  
  34. return Component.extend({
  35. defaults: {
  36. template: 'Convert_ClickAndCollect/cart/click-collect-stores'
  37. },
  38. formId: "#selectWarehouseForm",
  39. parent: {},
  40. postCode: ko.observable(''),
  41. wareHouses: ko.observableArray(),
  42. address: {},
  43. isWarehouseSelected: ko.observable(""),
  44.  
  45. /**
  46. * @override
  47. */
  48. initialize: function () {
  49. this._super();
  50. $(document).ready(() => this.ready());
  51. return this;
  52. },
  53.  
  54. ready: function () {
  55. this.parent = uiRegistry.get(this.parentName);
  56. },
  57.  
  58. selectWarehouse: function(warehouseId) {
  59. if ($('#s_method_freeshipping').length) {
  60. return false;
  61. }
  62. if (!this.validateForm(this.formId)) {
  63. return false;
  64. }
  65. if (warehouseId != null) {
  66. let body = $('body').loader();
  67. let uri = this.getFormAction();
  68. let payload = JSON.stringify({
  69. addressInformation: {
  70. address: {
  71. countryId: 'AU',
  72. postcode: this.postCode(),
  73. regionId: 0
  74. },
  75. shipping_carrier_code: ClickAndCollectConfig.shipping.carrier_code,
  76. shipping_method_code: warehouseId
  77. }
  78. });
  79. this.address = createShippingAddress(payload);
  80. shippingService.isLoading(true);
  81. storage.post(
  82. uri, payload, true
  83. ).done(
  84. (resp) => this.selectWarehouseSuccess(resp, this.getInputValue(warehouseId))
  85. ).fail(
  86. (resp) => this.selectWarehouseFail(resp)
  87. ).always(
  88. () => this.selectWarehouseAlways()
  89. );
  90. }
  91. return false;
  92. },
  93.  
  94. selectWarehouseSuccess: function(result, checked) {
  95. if (result) {
  96. rateRegistry.set(this.address.getCacheKey(), result);
  97. shippingService.setShippingRates(result);
  98. quote.setTotals(result);
  99. }
  100. this.isWarehouseSelected(checked);
  101. },
  102.  
  103. selectWarehouseFail: function(response) {
  104. console.error("There was an error saving shipping the method, no address will be used at this time");
  105. shippingService.setShippingRates([]);
  106. errorProcessor.process(response);
  107. },
  108.  
  109. selectWarehouseAlways: function() {
  110. let body = $('body').loader();
  111. body.loader('hide');
  112. shippingService.isLoading(false);
  113. },
  114.  
  115. validateForm: function (form) {
  116. return $(form).validation() && $(form).validation('isValid');
  117. },
  118.  
  119. getInputValue(warehouseId) {
  120. return ClickAndCollectConfig.shipping.carrier_code + "_" + warehouseId;
  121. },
  122.  
  123. getFormAction() {
  124. return resourceUrlManager.getUrlForTotalsEstimationForNewAddress(quote);
  125. },
  126. });
  127. }
  128. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement