Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying coupons using woocommerce rest api

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?

like image 956
SatheeshCK17 Avatar asked Apr 09 '16 12:04

SatheeshCK17


People also ask

How do I create a coupon programmatically in WooCommerce?

Let's do it and begin with something simple. $coupon = new WC_Coupon(); $coupon->set_code( 'blackfrd' ); // Coupon code $coupon->set_amount( 200 ); // Discount amount $coupon->save(); As you could've guessed, two methods set_code() and set_amount() are more than enough to create a coupon.

What can you do with WooCommerce REST API?

The WooCommerce REST API is an interface that you can use to access your WooCommerce store from outside WordPress. It was designed to make it easy for WooCommerce stores on WordPress to interact with other websites and applications over the Internet.

How do I manage Coupons in WooCommerce?

Go to WooCommerce -> Woo Discount Rule -> Add Rule. This will navigate to the rule page where you can create the WooCommerce discount rule and coupon code. You can create the WooCommerce discount code as a percentage discount, fixed price discount, free shipping, and more using the WooCommerce coupon generator plugin.


1 Answers

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',
    ]
]
like image 91
Gulshan kumar Avatar answered Oct 02 '22 23:10

Gulshan kumar