简体   繁体   中英

Applying coupon using woocommerce rest api

I am trying to apply coupon code using WooCommerce Rest API. I am following the way that has been explained in woo API document. But it is not working anyhow.

Coupon code is applied but discount is not applied.

So can you please tell me if I am doing anything wrong or is there any way to do this.

I have tried searching for the solution, but found nothing so far.

Thanks in advance

Below is my data that I am using as Request to place order using 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;

For anyone looking for a solution, there is none at the moment. but there is a work around that can help you do the discounts. The solution can be found issuecomment-333092453

Solution summary:

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 is the total price of an item after discount. make sure this quantity is included: total = price_total_of_single_item * quantity_of_item

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM