Magento 2结账运费汇率

时间:2016-07-22 08:57:44

标签: magento knockout.js magento2

我试图通过3d party JS代码结账时触发运费回收,而不是Knockout。什么是触发它的最佳方式?

现在我已经用自定义模板替换了模板onepage.phtml并尝试了这种方法,但它不起作用:

    require([
        'jquery',
        'Magento_Checkout/js/model/quote'
    ], function($, quote) {
        $('#target').on('click', function(e) {
            console.log(quote.shippingAddress(quote.shippingAddress()));
        });
    });

3 个答案:

答案 0 :(得分:2)

好的,伙计们。这是解决方案:

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);
        }
    });
});

答案 1 :(得分:0)

我已经尝试过@ c0rewell方式

  1. Company_Modulename /视图/前端/ requirejs-config.js
  2. var config = {
        map: {
            '*': {
                company: 'Company_Modulename/js/myjs'
            }
        }
    };
    
    1. Company_Modulename /视图/前端/网络/ JS / myjs.js
    2. 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)
      

答案 2 :(得分:0)

@Ansyori 我已设法通过修改下面的代码来运行estimat-shipping-methods

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);
            }
});
});