php – Applying coupons using woocommerce rest api-ThrowExceptions

Exception or error:

I am developing an application using WooCommerce rest API v3. Now I’m trying to integrate coupons from my WooCommerce website to my application. I’m creating order and applying coupons like this

"coupon_lines":[{
    'code'=>'coupon1',
    'amount'=>'1.00'
}]

I’ve set a usage limit per user in woocommerce website.

When I ordered directly from the website, usage restrictions are applied correctly. i.e, a user cannot use a coupon when a limit is reached.
But when I make an order via REST API, the restrictions are not applied.

Additionally, I got a reply from another forum stating that coupon apply feature is not yet available via rest API. But, while looking through the code of create_order API endpoint, I found that the webhook for applying coupon is called there.

 do_action( 'woocommerce_order_add_coupon', $this->id, $item_id, $code, $discount_amount, $discount_amount_tax ); 

in add_coupon(). But I didn’t find the add_action('woocommerce_order_add_coupon',...) . Can anyone tell me where should I write this action definition so that it shouldn’t get overwritten when updating WooCommerce?

How to solve:

Can you please add coupons id and after check?

"coupon_lines":[{
        'id' => '',
        'code'=>'coupon1',
        'amount'=>'1.00'
}]

Answer´╝Ü

you need to add “coupon_lines” to your order object as stated here
Every order should have and “coupon_lines” array containing “id”,”code”,”amount” for you desired coupon.

https://woocommerce.github.io/woocommerce-rest-api-docs/v3.html#view-customer-orders

'coupon_lines' => [
    [
        'id' => 55,
        'code' => free50,
        'amount' => '10.75',
    ]
]

Leave a Reply

Your email address will not be published. Required fields are marked *