0

我正在创建一个以 Woo 商务为内容的应用程序。到目前为止,我可以显示产品列表并访问产品,但我无法创建订单。

这是创建和 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
    }
}

当我使用浏览器检查时,参数似乎在那里:

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 并创建没有产品的订单。

任何帮助表示赞赏

更新

我试着评论这一行:

paramSerializer: '$httpParamSerializerJQLike',

现在创建了订单,但没有产品数据,只分配了“非数组”参数,所以我几乎可以肯定是格式问题......

示例对象:

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: 'john.doe@example.com',
    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
    }
  ]
};
4

1 回答 1

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 不起作用。我从其他来源获取了一些代码。

希望能帮助到你

于 2016-11-18T21:16:16.827 回答