Completed
Pull Request — master (#11757)
by Claudio
14:32
created

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

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 13
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
 *
79
 * @param int $id Coupon ID.
80
 *
81
 * @return string
82
 */
83
function wc_get_coupon_code_by_id( $id ) {
84
	global $wpdb;
85
86
	$code = $wpdb->get_var( $wpdb->prepare( "
87
		SELECT post_title
88
		FROM $wpdb->posts
89
		WHERE ID = %d
90
		AND post_type = 'shop_coupon'
91
		AND post_status = 'publish';
92
	", $id ) );
93
94
	return (string) $code;
95
}
96