WordPress at Your Fingertips
function is not described


DataStore::get_coupon_id() public WC 1.0

Get coupon ID for an order.

Tries to get the ID from order item meta, then falls back to a query of published coupons.

{} It's a method of the class: DataStore{}

No Hooks.


Int. Coupon ID on success, 0 on failure.


$result = DataStore::get_coupon_id( $coupon_item );
$coupon_item(\WC_Order_Item_Coupon) (required)
The coupon order item object.

Code of DataStore::get_coupon_id() WC 6.1.1

public static function get_coupon_id( \WC_Order_Item_Coupon $coupon_item ) {
	// First attempt to get coupon ID from order item data.
	$coupon_data = $coupon_item->get_meta( 'coupon_data', true );

	// Normal checkout orders should have this data.
	// See: https://github.com/woocommerce/woocommerce/blob/3dc7df7af9f7ca0c0aa34ede74493e856f276abe/includes/abstracts/abstract-wc-order.php#L1206.
	if ( isset( $coupon_data['id'] ) ) {
		return $coupon_data['id'];

	// Try to get the coupon ID using the code.
	return wc_get_coupon_id_by_code( $coupon_item->get_code() );