|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CouponHelper. |
|
4
|
|
|
* |
|
5
|
|
|
* This helper class should ONLY be used for unit tests!. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\WooCommerce\RestApi\UnitTests\Helpers; |
|
9
|
|
|
|
|
10
|
|
|
defined( 'ABSPATH' ) || exit; |
|
11
|
|
|
|
|
12
|
|
|
use \WC_Coupon; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class CouponHelper. |
|
16
|
|
|
*/ |
|
17
|
|
|
class CouponHelper { |
|
18
|
|
|
|
|
19
|
|
|
protected static $custom_types = array(); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Create a dummy coupon. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $coupon_code |
|
25
|
|
|
* @param array $meta |
|
26
|
|
|
* |
|
27
|
|
|
* @return WC_Coupon |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function create_coupon( $coupon_code = 'dummycoupon', $meta = array() ) { |
|
30
|
|
|
// Insert post |
|
31
|
|
|
$coupon_id = wp_insert_post( |
|
32
|
|
|
array( |
|
33
|
|
|
'post_title' => $coupon_code, |
|
34
|
|
|
'post_type' => 'shop_coupon', |
|
35
|
|
|
'post_status' => 'publish', |
|
36
|
|
|
'post_excerpt' => 'This is a dummy coupon', |
|
37
|
|
|
) |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
$meta = wp_parse_args( |
|
41
|
|
|
$meta, |
|
42
|
|
|
array( |
|
43
|
|
|
'discount_type' => 'fixed_cart', |
|
44
|
|
|
'coupon_amount' => '1', |
|
45
|
|
|
'individual_use' => 'no', |
|
46
|
|
|
'product_ids' => '', |
|
47
|
|
|
'exclude_product_ids' => '', |
|
48
|
|
|
'usage_limit' => '', |
|
49
|
|
|
'usage_limit_per_user' => '', |
|
50
|
|
|
'limit_usage_to_x_items' => '', |
|
51
|
|
|
'expiry_date' => '', |
|
52
|
|
|
'free_shipping' => 'no', |
|
53
|
|
|
'exclude_sale_items' => 'no', |
|
54
|
|
|
'product_categories' => array(), |
|
55
|
|
|
'exclude_product_categories' => array(), |
|
56
|
|
|
'minimum_amount' => '', |
|
57
|
|
|
'maximum_amount' => '', |
|
58
|
|
|
'customer_email' => array(), |
|
59
|
|
|
'usage_count' => '0', |
|
60
|
|
|
) |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
// Update meta. |
|
64
|
|
|
foreach ( $meta as $key => $value ) { |
|
65
|
|
|
update_post_meta( $coupon_id, $key, $value ); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return new WC_Coupon( $coupon_code ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Delete a coupon. |
|
73
|
|
|
* |
|
74
|
|
|
* @param $coupon_id |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function delete_coupon( $coupon_id ) { |
|
79
|
|
|
wp_delete_post( $coupon_id, true ); |
|
80
|
|
|
|
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Register a custom coupon type. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $coupon_type |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function register_custom_type( $coupon_type ) { |
|
90
|
|
|
static $filters_added = false; |
|
91
|
|
|
if ( isset( self::$custom_types[ $coupon_type ] ) ) { |
|
92
|
|
|
return; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
self::$custom_types[ $coupon_type ] = "Testing custom type {$coupon_type}"; |
|
96
|
|
|
|
|
97
|
|
|
if ( ! $filters_added ) { |
|
98
|
|
|
add_filter( 'woocommerce_coupon_discount_types', array( __CLASS__, 'filter_discount_types' ) ); |
|
99
|
|
|
add_filter( 'woocommerce_coupon_get_discount_amount', array( __CLASS__, 'filter_get_discount_amount' ), 10, 5 ); |
|
100
|
|
|
add_filter( 'woocommerce_coupon_is_valid_for_product', '__return_true' ); |
|
101
|
|
|
$filters_added = true; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Unregister custom coupon type. |
|
107
|
|
|
* |
|
108
|
|
|
* @param $coupon_type |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function unregister_custom_type( $coupon_type ) { |
|
111
|
|
|
unset( self::$custom_types[ $coupon_type ] ); |
|
112
|
|
|
if ( empty( self::$custom_types ) ) { |
|
113
|
|
|
remove_filter( 'woocommerce_coupon_discount_types', array( __CLASS__, 'filter_discount_types' ) ); |
|
114
|
|
|
remove_filter( 'woocommerce_coupon_get_discount_amount', array( __CLASS__, 'filter_get_discount_amount' ) ); |
|
115
|
|
|
remove_filter( 'woocommerce_coupon_is_valid_for_product', '__return_true' ); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Register custom discount types. |
|
121
|
|
|
* |
|
122
|
|
|
* @param array $discount_types |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function filter_discount_types( $discount_types ) { |
|
126
|
|
|
return array_merge( $discount_types, self::$custom_types ); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get custom discount type amount. Works like 'percent' type. |
|
131
|
|
|
* |
|
132
|
|
|
* @param float $discount |
|
133
|
|
|
* @param float $discounting_amount |
|
134
|
|
|
* @param array|null $item |
|
135
|
|
|
* @param bool $single |
|
136
|
|
|
* @param WC_Coupon $coupon |
|
137
|
|
|
* |
|
138
|
|
|
* @return float |
|
139
|
|
|
*/ |
|
140
|
|
|
public static function filter_get_discount_amount( $discount, $discounting_amount, $item, $single, $coupon ) { |
|
141
|
|
|
if ( ! isset( self::$custom_types [ $coupon->get_discount_type() ] ) ) { |
|
142
|
|
|
return $discount; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return (float) $coupon->get_amount() * ( $discounting_amount / 100 ); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|