wc_update_coupon_usage_counts

Update used coupon amount for each coupon within an order.

Description

wc_update_coupon_usage_counts( (int) $order_id ); 

Parameters (1)

0. $order_id (int)
The order id.

Usage

  1. if ( !function_exists( 'wc_update_coupon_usage_counts' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-order-functions.php'; 
  3.  
  4. // The order id. 
  5. $order_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wc_update_coupon_usage_counts($order_id); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-order-functions.php  
  1. function wc_update_coupon_usage_counts( $order_id ) { 
  2. if ( ! $order = wc_get_order( $order_id ) ) { 
  3. return; 
  4.  
  5. $has_recorded = $order->get_data_store()->get_recorded_coupon_usage_counts( $order ); 
  6.  
  7. if ( $order->has_status( 'cancelled' ) && $has_recorded ) { 
  8. $action = 'reduce'; 
  9. $order->get_data_store()->set_recorded_coupon_usage_counts( $order, false ); 
  10. } elseif ( ! $order->has_status( 'cancelled' ) && ! $has_recorded ) { 
  11. $action = 'increase'; 
  12. $order->get_data_store()->set_recorded_coupon_usage_counts( $order, true ); 
  13. } else { 
  14. return; 
  15.  
  16. if ( sizeof( $order->get_used_coupons() ) > 0 ) { 
  17. foreach ( $order->get_used_coupons() as $code ) { 
  18. if ( ! $code ) { 
  19. continue; 
  20.  
  21. $coupon = new WC_Coupon( $code ); 
  22.  
  23. if ( ! $used_by = $order->get_user_id() ) { 
  24. $used_by = $order->get_billing_email(); 
  25.  
  26. switch ( $action ) { 
  27. case 'reduce' : 
  28. $coupon->decrease_usage_count( $used_by ); 
  29. break; 
  30. case 'increase' : 
  31. $coupon->increase_usage_count( $used_by ); 
  32. break;