【问题标题】:Cant get the current postcode无法获取当前邮政编码
【发布时间】:2019-03-08 15:46:03
【问题描述】:

我在从结帐输入邮政编码获取当前或更改的邮政编码时遇到一些问题,并且还尝试从我的组件“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;


        }
    });
});

我错过了什么吗?

【问题讨论】:

    标签: magento knockout.js magento2 magento2.2


    【解决方案1】:

    我还有一个建议。您可以使用 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
    
        });
    
    });
    

    【讨论】:

      猜你喜欢
      • 2019-08-02
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 2013-04-05
      相关资源
      最近更新 更多