在从结帐输入邮政编码获取当前或更改的邮政编码时遇到一些问题,并且还尝试从组件“ getKiosPoints”再次触发该方法。
基本上,工作流程是检查输入中是否填写了邮政编码,以及是否将其用于调用API以获取位置。
当页面加载了邮政编码信息时,此方法正常工作。
问题是获取页面中更改时的邮政编码,然后使用插入的邮政编码触发方法“ getKiospoints”。
我使用的是“ quote.shippingAddress._latestValue.postcode”或“ quote.shippingAddress()。postcode”,但问题是,由于要进入存储区,所以我总是得到以前的邮政编码,而不是实际插入的。
这是我的代码:
define([
'jquery',
'ko',
'Magento_Checkout/js/model/quote',
'uiComponent'
], function ($,ko,quote,Component) {
'use strict';
var listLocations = ko.observableArray([]);
$(document).on('change',"[name='postcode']",function(){
console.log("what postcalcode");
console.log(quote.shippingAddress().postcode);
});
return Component.extend({
defaults: {
template: 'Cloud_Vasp/checkout/shipping/pickup-points'
},
getCode: function(){
return 'cloud_vasp_cloud_vasp';
},
initObservable: function () {
this._super();
this.selectedMethod = ko.computed(function() {
var method = quote.shippingMethod();
var selectedMethod = method != null ? method.carrier_code + '_' + method.method_code : null;
return selectedMethod;
}, this);
return this;
},
getKiosPoints: function () {
var postalCode = quote.shippingAddress._latestValue.postcode;
console.log(postalCode);
$.ajax
({
type: "POST",
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDc');
},
url: '...api/va.pt/GetServiceDeliveryPoints',
dataType: 'json',
contentType: 'application/json',
async: true,
//json object to sent to the authentication url
data: JSON.stringify({
"PostalCodeOrLocality": postalCode
}),
success: function (data) {
if(data.statusCode == 200){
var locations = data.response;
listLocations(data.response);
//return locations;
}
},
error: function( jqXhr, textStatus, errorThrown ){
return errorThrown;
}
})
return listLocations;
}
});
});
我想念什么吗?
答案 0 :(得分:0)
我还有另一个建议。您可以使用onepage.html并应用以下代码:-
require(
[
'jquery',
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/model/shipping-service',
'Magento_Checkout/js/model/shipping-rate-registry',
'Magento_Checkout/js/model/shipping-rate-processor/new-address',
'Magento_Checkout/js/model/shipping-rate-processor/customer-address',
'Magento_Customer/js/model/customer'
],
function ($, quote, shippingService, rateRegistry, defaultProcessor, customerAddressProcessor, customer) {
$(document).on('change',"#shipping-new-address-form [name='postcode']",function(e){
var elementValue = $(this).val();
//Apply your code here
});
});