简体   繁体   中英

How to apply coupon using Woocommerce REST API when creating new order from android application?

I am using woocommerce REST API for eCommerce android app and everything working good except coupon when creating new order from app.

Using this API to create order

https://my-domain.com/wp-json/wc/v2/orders

and passing some json data including

"coupon_lines" => [
     "id" => 111,
     "code" => "coupon code",
     "discount" => "10.0", 
     "discount_tax" => "0"
]

by this way coupon will be applied to order but amount is not being reduced from total. Can anyone please help me? I am new to woocommerce.

I have searched for that and got that woocommerce REST API is not providing functionality to calculate coupon discount so I have to do it manually but don't know how to do it? and from where to start?

Even I got one solution that I can calculate coupon discount at client side and I did it but it's to tricky as there are lots of variations for coupon so my code broke down for some coupons. Totally stuck in this since last two days Please help me

You can start with the following way of the code. you have to customize the response of the order rest API where you can make your custom code for the calculate the coupon amount pass your custom response to the API.

add_filter( 'woocommerce_rest_prepare_shop_order_object', 'custom_change_shop_order_response', 10, 3 );
function custom_change_shop_order_response( $response, $object, $request ) {
  //here you can get the three parameters.
 // In the $response you can get the default response of the orders.
 // In the $request you can get the request.here you can get a coupon code 
// do your magic code here
}

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