wc_update_coupon_usage_counts( int $order_id )

Update used coupon amount for each coupon within an order.


Description Description


Parameters Parameters

$order_id

(Required) Order ID.


Top ↑

Source Source

File: includes/wc-order-functions.php

function wc_update_coupon_usage_counts( $order_id ) {
	$order = wc_get_order( $order_id );

	if ( ! $order ) {
		return;
	}

	$has_recorded = $order->get_data_store()->get_recorded_coupon_usage_counts( $order );

	if ( $order->has_status( 'cancelled' ) && $has_recorded ) {
		$action = 'reduce';
		$order->get_data_store()->set_recorded_coupon_usage_counts( $order, false );
	} elseif ( ! $order->has_status( 'cancelled' ) && ! $has_recorded ) {
		$action = 'increase';
		$order->get_data_store()->set_recorded_coupon_usage_counts( $order, true );
	} else {
		return;
	}

	if ( count( $order->get_coupon_codes() ) > 0 ) {
		foreach ( $order->get_coupon_codes() as $code ) {
			if ( ! $code ) {
				continue;
			}

			$coupon  = new WC_Coupon( $code );
			$used_by = $order->get_user_id();

			if ( ! $used_by ) {
				$used_by = $order->get_billing_email();
			}

			switch ( $action ) {
				case 'reduce':
					$coupon->decrease_usage_count( $used_by );
					break;
				case 'increase':
					$coupon->increase_usage_count( $used_by, $order );
					break;
			}
		}
		$order->get_data_store()->release_held_coupons( $order, true );
	}
}

Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.