使用woocommerce rest api应用优惠券

时间:2018-04-10 06:07:55

标签: php wordpress woocommerce orders woocommerce-rest-api

我正在尝试使用WooCommerce Rest API应用优惠券代码。我按照woo API文档中解释的方式进行操作。但它无论如何都无法发挥作用。

已应用优惠券代码,但不会应用折扣。

所以,如果我做错了或有任何办法,请告诉我。

我试过寻找解决方案,但到目前为止没有找到任何结果。

提前致谢

以下是我使用API​​作为要求下订单的数据

  $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
    ]
],
'coupon_lines'=>
[
    [
        'code'=>'wer',
        'id'=>128,
        'amount'=>'10.00',
        'discount'=>'10'
      ]
   ]
  ];

$response = $woocommerce->post('orders', $data));

echo "<pre>"; 
print_r($response); 
echo "</pre>";
exit;

1 个答案:

答案 0 :(得分:0)

对于任何寻求解决方案的人,目前都没有解决方案。但是有一个解决方法可以帮助您进行折扣。可以找到解决方案issuecomment-333092453

解决方案摘要:

getItemsCart() {
const {cartItems} = this.props;
let items = []
for (var i = 0; i < cartItems.length; i++) {
  const cartItem = cartItems[i]

  let item = {
    product_id: cartItem.product.id,
    quantity: cartItem.quantity,
    total: cartItem.total // The magic happens here, you apply the coupon to your item's price

  }
  items.push(item)
}
return items;
}
let data = {
  customer_id: user.id,
  customer_note: null,
  billing: customerInfo,
  line_items: this.getItemsCart(),
}
API.post('orders', data)

Total是折扣后的商品总价。确保包含此数量:total = price_total_of_single_item * quantity_of_item