Completed
Push — master ( f54ada...499e19 )
by Mike
22:45
created

wc-coupon-functions.php ➔ wc_get_coupon_id_by_code()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 18
rs 9.4285
1
<?php
2
/**
3
 * WooCommerce Coupons Functions
4
 *
5
 * Functions for coupon specific things.
6
 *
7
 * @author 		WooThemes
8
 * @category 	Core
9
 * @package 	WooCommerce/Functions
10
 * @version     2.1.0
11
 */
12
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit; // Exit if accessed directly
15
}
16
17
/**
18
 * Get coupon types.
19
 *
20
 * @return array
21
 */
22
function wc_get_coupon_types() {
23
	return (array) apply_filters( 'woocommerce_coupon_discount_types', array(
24
		'fixed_cart'      => __( 'Cart Discount', 'woocommerce' ),
25
		'percent'         => __( 'Cart % Discount', 'woocommerce' ),
26
		'fixed_product'   => __( 'Product Discount', 'woocommerce' ),
27
		'percent_product' => __( 'Product % Discount', 'woocommerce' )
28
	) );
29
}
30
31
/**
32
 * Get a coupon type's name.
33
 *
34
 * @param string $type (default: '')
35
 * @return string
36
 */
37
function wc_get_coupon_type( $type = '' ) {
38
	$types = wc_get_coupon_types();
39
	return isset( $types[ $type ] ) ? $types[ $type ] : '';
40
}
41
42
/**
43
 * Coupon types that apply to individual products. Controls which validation rules will apply.
44
 *
45
 * @since  2.5.0
46
 * @return array
47
 */
48
function wc_get_product_coupon_types() {
49
	return (array) apply_filters( 'woocommerce_product_coupon_types', array( 'fixed_product', 'percent_product' ) );
50
}
51
52
/**
53
 * Coupon types that apply to the cart as a whole. Controls which validation rules will apply.
54
 *
55
 * @since  2.5.0
56
 * @return array
57
 */
58
function wc_get_cart_coupon_types() {
59
	return (array) apply_filters( 'woocommerce_cart_coupon_types', array( 'fixed_cart', 'percent' ) );
60
}
61
62
/**
63
 * Check if coupons are enabled.
64
 * Filterable.
65
 *
66
 * @since  2.5.0
67
 *
68
 * @return array
69
 */
70
function wc_coupons_enabled() {
71
	return apply_filters( 'woocommerce_coupons_enabled', 'yes' === get_option( 'woocommerce_enable_coupons' ) );
72
}
73
74
/**
75
 * Get coupon code by ID.
76
 *
77
 * @since 2.7.0
78
 * @param int $id Coupon ID.
79
 * @return string
80
 */
81
function wc_get_coupon_code_by_id( $id ) {
82
	global $wpdb;
83
84
	$code = $wpdb->get_var( $wpdb->prepare( "
85
		SELECT post_title
86
		FROM $wpdb->posts
87
		WHERE ID = %d
88
		AND post_type = 'shop_coupon'
89
		AND post_status = 'publish';
90
	", $id ) );
91
92
	return (string) $code;
93
}
94
95
/**
96
 * Get coupon code by ID.
97
 *
98
 * @since 2.7.0
99
 * @param string $code
100
 * @param int $exclude Used to exclude an ID from the check if you're checking existance.
101
 * @return int
102
 */
103
function wc_get_coupon_id_by_code( $code, $exclude = 0 ) {
104
	global $wpdb;
105
106
	$ids = wp_cache_get( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $code, 'coupons' );
107
108
	if ( false === $ids ) {
109
		$sql = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish' ORDER BY post_date DESC;", $code );
110
		$ids = $wpdb->get_col( $sql );
111
112
		if ( $ids ) {
113
			wp_cache_set( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $code, $ids, 'coupons' );
114
		}
115
	}
116
117
	$ids = array_diff( array_filter( array_map( 'absint', (array) $ids ) ), array( $exclude ) );
118
119
	return apply_filters( 'woocommerce_get_coupon_id_from_code', absint( current( $ids ) ), $code, $exclude );
120
}
121