Reputation: 822
I have Visited The official WooCommerce Rest API documentation "Create an Order" section. I am trying to create an order via the API, but when I want to create the order with the apply coupon, I don't know how to make it.
How I will pass the coupon code as a discount when I create the order through the REST API?
In the related official documentation I don't find any request parameter to pass a coupon code or a discount.
Please suggest how to pass the coupon code when creating order via rest API in WooCommerce.
Upvotes: 5
Views: 12076
Reputation: 261
Applying coupons to orders via API doesn't exist in v2. use v3.
Upvotes: 1
Reputation: 31
I have tried the following answer
The first step is to create a coupon from woocommerce admin backend and add your rules for this coupon (percentage of discount or discount amount or others..)
Then according to the order controller class that is part of the woocommerce rest api, when updating an order to attach a coupon you will need to include ONLY the coupon code as part of the coupon_lines array.
Below link of the controller: https://github.com/woocommerce/woocommerce/blob/master/includes/api/class-wc-rest-orders-controller.php
This is the part of the code that do validation on coupon_lines array and apply our coupon code to the woocommerce order
As you can see, It do validates that you are not passing a coupon id and you only pass coupon code before applying this code to the orders.
Below sample JSON that worked with me on updating the order
{
"coupon_lines": [
{
"code": "1122"
}
]
}
Once your coupon code is applied, the order will be returned with all other fields populated by woocommerce after doing the order total calculation & applying coupon discount
Below validation code from rest api order controller
foreach ( $request['coupon_lines'] as $item ) {
if ( is_array( $item ) ) {
if ( empty( $item['id'] ) ) {
if ( empty( $item['code'] ) ) {
throw new WC_REST_Exception( 'woocommerce_rest_invalid_coupon', __( 'Coupon code is required.', 'woocommerce' ), 400 );
}
$results = $order->apply_coupon( wc_clean( $item['code'] ) );
if ( is_wp_error( $results ) ) {
throw new WC_REST_Exception( 'woocommerce_rest_' . $results->get_error_code(), $results->get_error_message(), 400 );
}
}
}
}
Upvotes: 3
Reputation: 33
if you didn't found a way i wrote and answer just in case someone else comes here with the same question as i did :). After you create an order using the rest api you should update that order with the coupon_lines as suggested by others here. So in summary create an order using the rest api use the order_id returned by it to update (documentation here) the order with the coupon_lines to apply the coupon the user applies.
Upvotes: 1
Reputation: 645
you should add "coupon_lines" to your order object as stated here
Order properties detailed further on here enter link description here
so every order should have and "coupon_lines" array containing "id","code","amount" for you desired coupon which you get when you verify the coupon through the coupon API.
'coupon_lines' => [
[
'id' => 32,
'code' => testcoupon,
'amount' => '0.75',
]
]
Upvotes: 1
Reputation: 254492
You should need to add in your data array 'coupon_lines'
, something like:
'coupon_lines' => [
[
'code' => 'mycouponcode',
'discount' => '5',
'discount_tax' => '0.75',
'meta_data' => [
[
'key' => 'coupon_data',
'value' => [
'id' => '1234',
'code' => 'mycouponcode',
'amount' => '10',
/* ... and so on ... */
]
]
]
]
]
The meta_data
array is all the woocommerce coupon meta data. This should work…
Upvotes: 4
Reputation: 4419
This is because the API for creating coupon codes is separate from the ordering API. You need to define coupons using the Coupon API:
code: '10off',
discount_type: 'percent',
amount: '10',
individual_use: true,
exclude_sale_items: true,
minimum_amount: '100.00'
Then any coupons applied by the customer are automatically computed.
Upvotes: 2