Automattic\WooCommerce\Admin\API\Reports\Coupons
DataStore::get_coupon_id()
Get coupon ID for an order.
Tries to get the ID from order item meta, then falls back to a query of published coupons.
Method of the class: DataStore{}
No Hooks.
Return
Int
. Coupon ID on success, 0 on failure.
Usage
$result = DataStore::get_coupon_id( $coupon_item );
- $coupon_item(\WC_Order_Item_Coupon) (required)
- The coupon order item object.
DataStore::get_coupon_id() DataStore::get coupon id code WC 8.7.0
public static function get_coupon_id( \WC_Order_Item_Coupon $coupon_item ) { // First attempt to get coupon ID from order item data. $coupon_info = $coupon_item->get_meta( 'coupon_info', true ); if ( $coupon_info ) { return json_decode( $coupon_info, true )[0]; } $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() ); }