wc_get_coupon_id_by_code

Get coupon code by ID.

Description

(int) wc_get_coupon_id_by_code( (string) $code, (int) $exclude = 0 ); 

Returns (int)

Parameters (2)

0. $code (string)
The code.
1. $exclude — Optional. (int)
Used to exclude an ID from the check if you're checking existence.

Usage

  1. if ( !function_exists( 'wc_get_coupon_id_by_code' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-coupon-functions.php'; 
  3.  
  4. // The code. 
  5. $code = ''; 
  6.  
  7. // Used to exclude an ID from the check if you're checking existence. 
  8. $exclude = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wc_get_coupon_id_by_code($code, $exclude); 
  12.  

Defined (1)

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

/includes/wc-coupon-functions.php  
  1. function wc_get_coupon_id_by_code( $code, $exclude = 0 ) { 
  2. $data_store = WC_Data_Store::load( 'coupon' ); 
  3. $ids = wp_cache_get( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $code, 'coupons' ); 
  4.  
  5. if ( false === $ids ) { 
  6. $ids = $data_store->get_ids_by_code( $code ); 
  7. if ( $ids ) { 
  8. wp_cache_set( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $code, $ids, 'coupons' ); 
  9.  
  10. $ids = array_diff( array_filter( array_map( 'absint', (array) $ids ) ), array( $exclude ) ); 
  11.  
  12. return apply_filters( 'woocommerce_get_coupon_id_from_code', absint( current( $ids ) ), $code, $exclude );