我正在尝试使用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' => '[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
]
],
'coupon_lines'=>
[
[
'code'=>'wer',
'id'=>128,
'amount'=>'10.00',
'discount'=>'10'
]
]
];
$response = $woocommerce->post('orders', $data));
echo "<pre>";
print_r($response);
echo "</pre>";
exit;
对于任何寻找解决方案的人来说,目前都没有。但是有一个可以帮助你做折扣的工作。解决方案可以找到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 = price_total_of_single_item * quantity_of_item