WC_Abstract_Order::hold_applied_coupons()publicWC 1.0

Check and records coupon usage tentatively so that counts validation is correct. Display an error if coupon usage limit has been reached.

If you are using this method, make sure to release_held_coupons in case an Exception is thrown.

Метод класса: WC_Abstract_Order{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$WC_Abstract_Order = new WC_Abstract_Order();
$WC_Abstract_Order->hold_applied_coupons( $billing_email );
$billing_email(строка) (обязательный)
Billing email of order.

Код WC_Abstract_Order::hold_applied_coupons() WC 8.7.0

public function hold_applied_coupons( $billing_email ) {
	$held_keys          = array();
	$held_keys_for_user = array();
	$error              = null;

	try {
		foreach ( WC()->cart->get_applied_coupons() as $code ) {
			$coupon = new WC_Coupon( $code );
			if ( ! $coupon->get_data_store() ) {
				continue;
			}

			// Hold coupon for when global coupon usage limit is present.
			if ( 0 < $coupon->get_usage_limit() ) {
				$held_key = $this->hold_coupon( $coupon );
				if ( $held_key ) {
					$held_keys[ $coupon->get_id() ] = $held_key;
				}
			}

			// Hold coupon for when usage limit per customer is enabled.
			if ( 0 < $coupon->get_usage_limit_per_user() ) {

				if ( ! isset( $user_ids_and_emails ) ) {
					$user_alias          = get_current_user_id() ? wp_get_current_user()->ID : sanitize_email( $billing_email );
					$user_ids_and_emails = $this->get_billing_and_current_user_aliases( $billing_email );
				}

				$held_key_for_user = $this->hold_coupon_for_users( $coupon, $user_ids_and_emails, $user_alias );

				if ( $held_key_for_user ) {
					$held_keys_for_user[ $coupon->get_id() ] = $held_key_for_user;
				}
			}
		}
	} catch ( Exception $e ) {
		$error = $e;
	} finally {
		// Even in case of error, we will save keys for whatever coupons that were held so our data remains accurate.
		// We save them in bulk instead of one by one for performance reasons.
		if ( 0 < count( $held_keys_for_user ) || 0 < count( $held_keys ) ) {
			$this->get_data_store()->set_coupon_held_keys( $this, $held_keys, $held_keys_for_user );
		}
		if ( $error instanceof Exception ) {
			throw $error;
		}
	}
}