2016-11-18 53 views
0

我正在創建一個與Woo商業交互的應用程序。現在我可以顯示產品列表並訪問產品,但我無法創建訂單。與數組的Javascript序列化參數

這是創建功能和POST的順序:

this.order = function(products, address, tax, total){ 
    var dfd = $q.defer(); 
    var clientId = 2; 
    var items =[]; 
    for(var i = 0 ; i<products.length ; i++){ 
     var item = {product_id: products[i].id, 
        quantity: products[i].qty, 
        price: products[i].price, 
       }; 
     items.push(item); 
    } 

    $http({ 
    method: 'POST', 
    url: appConfig.DOMAIN_URL + '/wp-json/wc/v1/orders' , 
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
    paramSerializer: '$httpParamSerializerJQLike', 
    params: { 
      consumer_key: appConfig.KEY, 
      consumer_secret: appConfig.SECRET_KEY, 
      line_items: items, 
      customer_id: 1, 
      total: total, 
      status: 'completed', 
      shipping: { 
       first_name: address.full_name, 
       address_1: address.street, 
       city: address.city, 
       postcode: address.postal_code, 
       state: address.state 
      }, 
      shipping_lines: [ 
      { 
       method_id: 'flat_rate', 
       method_title: 'Flat Rate', 
       total: tax 
      } 
      ] 
     } 
    }) 
    .then(function(res){ 
     dfd.resolve(res); 
    }, function(error){ 
     dfd.reject(error); 
    }) 
    return dfd.promise; 
    } 

的迴應是:

{ 
    "code": "woocommerce_rest_required_product_reference", 
    "message": "Product ID or SKU is required", 
    "data": { 
     "status": 400 
    } 
} 

PARAMS似乎在那裏當我檢查與瀏覽器:

Name Value 
consumer_key ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 
consumer_secret cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 
customer_id 1 
line_items[][price] 24490 
line_items[][product_id] 27 
line_items[][quantity] 1 
shipping_lines[][method_id] flat_rate 
shipping_lines[][method_title] Flat Rate 
shipping_lines[][total] 0 
status completed 
total 24490 

我嘗試了硬編碼並使用Woocommerce文檔中的相同數據,但結果相同。

某些組合會拋出「需要產品ID」,「需要數量」或「需要SKU」。

嘗試POSTMAN和訂單創建沒有產品。

任何幫助表示讚賞

UPDATE

我試圖評論這一行:

paramSerializer: '$httpParamSerializerJQLike', 

現在訂單被創建,但沒有產品的數據,只有 「無陣」 PARAMS是所以即時通訊幾乎肯定是格式問題...

示例對象:

var data = { 
    payment_method: 'bacs', 
    payment_method_title: 'Direct Bank Transfer', 
    set_paid: true, 
    billing: { 
    first_name: 'John', 
    last_name: 'Doe', 
    address_1: '969 Market', 
    address_2: '', 
    city: 'San Francisco', 
    state: 'CA', 
    postcode: '94103', 
    country: 'US', 
    email: '[email protected]', 
    phone: '(555) 555-5555' 
    }, 
    shipping: { 
    first_name: 'John', 
    last_name: 'Doe', 
    address_1: '969 Market', 
    address_2: '', 
    city: 'San Francisco', 
    state: 'CA', 
    postcode: '94103', 
    country: 'US' 
    }, 
    line_items: [ 
    { 
     product_id: 93, 
     quantity: 2 
    }, 
    { 
     product_id: 22, 
     variation_id: 23, 
     quantity: 1 
    } 
    ], 
    shipping_lines: [ 
    { 
     method_id: 'flat_rate', 
     method_title: 'Flat Rate', 
     total: 10 
    } 
    ] 
}; 

回答

0

這是我的解決方法:

$http({ 
    method: 'POST', 
    url: appConfig.DOMAIN_URL + '/wp-json/wc/v1/orders' , 
    //headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
    headers: {'Content-Type': 'application/json'}, 
    //paramSerializer: '$httpParamSerializerJQLike', 
    params: { 
      consumer_key: appConfig.KEY, 
      consumer_secret: appConfig.SECRET_KEY 
     }, 
    data: { 
     line_items: items, 
     customer_id: 1, 
     status: 'pending', 
     shipping: { 
      first_name: address.full_name, 
      address_1: address.street, 
      city: address.city, 
      postcode: address.postal_code, 
      state: address.state 
     }, 
     shipping_lines: [ 
     { 
      method_id: 'flat_rate', 
      method_title: 'Flat Rate', 
      total: tax 
     } 
     ] 
    } 
    }) 
    .then(function(res){ 
     dfd.resolve(res); 
    }, function(error){ 
     dfd.reject(error); 
    }) 
    return dfd.promise; 
    } 
}) 
; 

不知道這是傳遞參數最乾淨的方式,但我不明白爲什麼paramserializer不工作。我從其他來源獲取了一些代碼。

希望它有幫助