Magento 2结帐运费收集

Magento 2 checkout shipping rates collection

我正在尝试通过 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);
        }
    });
});

我已经尝试过@c0rewell 方式

  1. Company_Modulename/view/frontend/requirejs-config.js
var config = {
    map: {
        '*': {
            company: 'Company_Modulename/js/myjs'
        }
    }
};
  1. Company_Modulename/view/frontend/web/js/myjs.js

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) {
    $('div[name="shippingAddress.city"] select[name="city2"]').live('change', 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);
        }
    });
});

上面的sciprt成功强制刷新了运费,但是一直在加载

来自控制台的错误

resource-url-manager.js:35 Uncaught TypeError: Cannot read property 'getQuoteId' of undefined
at Object.getUrlForEstimationShippingMethodsByAddressId (resource-url-manager.js:35)
at Object.getRates (customer-address.js:26)
at HTMLSelectElement.<anonymous> (xongkir.js:17)
at HTMLDocument.dispatch (jquery.js:4624)
at HTMLDocument.elemData.handle (jquery.js:4292)

@Ansyori 我已经通过修改如下代码 运行 估计运输方法

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) {
$('div[name="shippingAddress.city"] select[name="city2"]').live('change', 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 == 'new-customer-address') {
                newAddressProcessor.getRates(address);
            }
});
});