我正在尝试通过 3d 方 JS 代码(而不是 Knockout)在结帐时触发运费记忆。触发它的最佳方式是什么?
现在我已将模板 onepage.phtml 替换为自定义模板并尝试这种方法,但它不起作用:
require([
'jquery',
'Magento_Checkout/js/model/quote'
], function($, quote) {
$('#target').on('click', function(e) {
console.log(quote.shippingAddress(quote.shippingAddress()));
});
});
最佳答案
好吧,伙计们。解决办法如下:
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/customer-address',
'Magento_Checkout/js/model/shipping-rate-processor/new-address',
], function($, quote, shippingService, rateRegistry, customerAddressProcessor, newAddressProcessor) {
$('#target').on('click', function(e) {
var address = quote.shippingAddress();
// clearing cached rates to retrieve new ones
rateRegistry.set(address.getCacheKey(), null);
var type = quote.shippingAddress().getType();
if (type) {
customerAddressProcessor.getRates(address);
} else {
newAddressProcessor.getRates(address);
}
});
});
关于Magento 2 结账运费收集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38522279/