在 Woocommerce Rest API 上使用 Rails 5 创建订单问题

Create Order issue with Rails 5 on Woocommerce Rest API

我有一个 rails 5 网络应用程序正在尝试 post 使用 woo commerce api 将订单返回到 wordpress 网站。

我使用的 gem 包装器是:

gem "woocommerce_api"

如果我对一个订单项进行硬编码,我可以完美地创建订单。但是我不知道如何添加多个订单项。请参阅下面的第一个 "working example",然后查看下面我尝试遍历订单项的尝试。

我收到的错误响应是:

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

显然我不应该在请求中放置一个 .each 循环...但我不知道任何其他方式?

正在工作POST请求

@cart_items = Cart.where(user_id: @user.id).all

data = {
  status: "processing", 
  currency: "AUD",
  set_paid: true,
  billing: {
    first_name: "#{@user.first_name}",
    last_name: "#{@user.last_name}",
    address_1: "#{@user.address}",
    address_2: "",
    city: "#{@user.suburb}",
    state: "#{@user.state}",
    postcode: "#{@user.postcode}",
    country: "Australia",
    email: "#{@user.email}",
    phone: "#{@user.phone}"
  },
  shipping: {
    first_name: "#{@user.first_name}",
    last_name: "#{@user.last_name}",
    address_1: "#{@user.address}",
    address_2: "",
    city: "#{@user.suburb}",
    state: "#{@user.state}",
    postcode: "#{@user.postcode}",
    country: "Australia",
  },
  line_items: [
      {
        :product_id => 123,
        :variation_id => 1,
        :quantity => 2
        :subtotal => "10.00",
        :total => "20.00"
      }
  ],
  shipping_lines: [
    {
      method_id: "flat_rate",
      method_title: "Flat Rate",
      total: "0"
    }
  ]
}

多个订单项请求失败

@cart_items = Cart.where(user_id: @user.id).all

data = {
  status: "processing", 
  currency: "AUD",
  set_paid: true,
  billing: {
    first_name: "#{@user.first_name}",
    last_name: "#{@user.last_name}",
    address_1: "#{@user.address}",
    address_2: "",
    city: "#{@user.suburb}",
    state: "#{@user.state}",
    postcode: "#{@user.postcode}",
    country: "Australia",
    email: "#{@user.email}",
    phone: "#{@user.phone}"
  },
  shipping: {
    first_name: "#{@user.first_name}",
    last_name: "#{@user.last_name}",
    address_1: "#{@user.address}",
    address_2: "",
    city: "#{@user.suburb}",
    state: "#{@user.state}",
    postcode: "#{@user.postcode}",
    country: "Australia",
  },
  line_items: [
      @cart_items.each do |ci|
      {
        :product_id => ci.product.woo_id.to_i,
        :variation_id => 0,
        :subtotal => ci.product.price.to_s,
        :total => ((ci.product.price.to_d * ci.quantity.to_i).round(2)).to_s
      }
    end
  ],
  shipping_lines: [
    {
      method_id: "flat_rate",
      method_title: "Flat Rate",
      total: "0"
    }
  ]
}

我认为问题与 each 方法的使用有关。

The most important thing to remember about the each method is that it does not change the return value. It implicitly returns the original array.

改为查看 mapcollect 方法,其中 "creates a new array containing the values returned by the block".

...
  line_items: @cart_items.map do |ci|
    {
      :product_id => ci.product.woo_id.to_i,
      :variation_id => 0,
      :subtotal => ci.product.price.to_s,
      :total => ((ci.product.price.to_d * ci.quantity.to_i).round(2)).to_s
    }
  end,
...

但是,正如您所说,在数据负载中使用方法并不好读,因此我建议您将其重构为一个变量:

@line_items = ...map function here...
data: {
...
  line_items: @line_items
...

或进入 Cart 模型中的实例方法。