-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathgw-create-coupon.php
277 lines (224 loc) · 8.76 KB
/
gw-create-coupon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
* Gravity Wiz // Gravity Forms // Create Coupons with Gravity Forms for Gravity Forms, WooCommerce, or Easy Digital Downloads
*
* Create coupons via Gravity Forms submissions. Map the coupon code to a field on the GF form and voila!
*
* @version 1.2.3
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link WooCommerce: http://gravitywiz.com/creating-coupons-woocommerce-gravity-forms/
* @link Gravity Forms: http://gravitywiz.com/creating-coupons-for-gf-coupons-add-on-with-gravity-forms/
*/
class GW_Create_Coupon {
var $_args;
public function __construct( $args = array() ) {
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'source_field_id' => false,
'name_field_id' => false,
'plugin' => 'gf', // accepts: 'gf', 'wc', 'edd'
'amount' => 0,
'type' => '', // accepts: 'fixed_cart', 'percent', 'fixed_product', 'percent_product'
'meta' => array(),
'required_fields' => array(),
) );
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
// make sure we're running the required minimum version of Gravity Forms and that WooCommerce is active
if ( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
return;
}
add_action( 'gform_after_submission', array( $this, 'create_coupon' ), 10, 2 );
}
public function create_coupon( $entry, $form ) {
if ( ! $this->is_applicable_form( $form ) || $this->should_abort( $entry ) ) {
return;
}
$coupon_codes = $this->get_coupon_codes( $entry, $this->_args['source_field_id'] );
foreach ( $coupon_codes as $coupon_code ) {
if ( $this->_args['name_field_id'] === false ) {
$coupon_name = $coupon_code;
} else {
$coupon_name = rgar( $entry, $this->_args['name_field_id'] );
$coupon_name = $coupon_name === '' ? $coupon_code : $coupon_name;
}
$amount = $this->_args['amount'];
$type = $this->_args['type'];
if ( is_callable( $amount ) ) {
$amount = call_user_func( $amount );
}
$plugin_func = array( $this, sprintf( 'create_coupon_%s', $this->_args['plugin'] ) );
if ( is_callable( $plugin_func ) ) {
call_user_func( $plugin_func, $coupon_name, $coupon_code, $amount, $type, $entry, $form );
}
}
}
public function create_coupon_edd( $coupon_name, $coupon_code, $amount, $type, $entry, $form ) {
if ( ! is_callable( 'edd_store_discount' ) ) {
return;
}
$meta = wp_parse_args( $this->_args['meta'], array(
'name' => $coupon_name,
'code' => $coupon_code,
'type' => $type,
'amount' => $amount,
'excluded_products' => array(),
'expiration' => '',
'is_not_global' => false,
'is_single_use' => false,
'max_uses' => '',
'min_price' => '',
'product_condition' => '',
'product_reqs' => array(),
'start' => '',
'uses' => '',
) );
// EDD will set it's own defaults in the edd_store_discount() so let's filter out our own empty defaults (they're just here for easier reference)
$meta = array_filter( $meta );
// EDD takes a $details array which has some different keys than the meta, let's map the keys to the expected format
$edd_post_keys = array(
'max_uses' => 'max',
'product_reqs' => 'products',
'excluded_products' => 'excluded-products',
'is_not_global' => 'not_global',
'is_single_use' => 'use_once',
);
foreach ( $meta as $key => $value ) {
$mod_key = rgar( $edd_post_keys, $key );
if ( $mod_key ) {
$meta[ $mod_key ] = $value;
}
}
edd_store_discount( $meta );
}
public function create_coupon_gf( $coupon_name, $coupon_code, $amount, $type, $entry, $form ) {
if ( ! class_exists( 'GFCoupons' ) ) {
return;
}
// hack to load GF Coupons data.php file
if ( is_callable( 'gf_coupons' ) ) {
gf_coupons()->get_config( array( 'id' => 0 ), false );
} else {
/** @noinspection PhpDynamicAsStaticMethodCallInspection */
GFCoupons::get_config( array( 'id' => 0 ), false );
}
$meta = wp_parse_args( $this->_args['meta'], array(
'form_id' => false,
'coupon_name' => $coupon_name,
'coupon_code' => strtoupper( $coupon_code ),
'coupon_type' => $type, // 'flat', 'percentage'
'coupon_amount' => $amount,
'coupon_start' => '', // MM/DD/YYYY
'coupon_expiration' => '', // MM/DD/YYYY
'coupon_limit' => false,
'coupon_stackable' => false,
) );
$form_id = $meta['form_id'] ? $meta['form_id'] : 0;
unset( $meta['form_id'] );
foreach ( $meta as $key => $value ) {
if ( $value instanceof Closure && is_callable( $value ) ) {
$meta[ $key ] = call_user_func( $value, $entry, $form, $this );
}
}
if ( is_callable( 'gf_coupons' ) ) {
$meta['gravityForm'] = $form_id ? $form_id : 0;
$meta['couponName'] = $meta['coupon_name'];
$meta['couponCode'] = $meta['coupon_code'];
$meta['couponAmountType'] = $meta['coupon_type'];
$meta['couponAmount'] = $meta['coupon_amount'];
$meta['startDate'] = $meta['coupon_start'];
$meta['endDate'] = $meta['coupon_expiration'];
$meta['usageLimit'] = $meta['coupon_limit'];
$meta['isStackable'] = $meta['coupon_stackable'];
$meta['usageCount'] = 0;
gf_coupons()->insert_feed( $form_id, true, $meta );
} else {
/** @noinspection PhpUndefinedClassInspection */
GFCouponsData::update_feed( 0, $form_id, true, $meta );
}
}
/**
* Create a WooCommerce coupon.
*
* @link https://gist.github.com/mikejolley/3969579#file-gistfile1-txt
*/
public function create_coupon_wc( $coupon_name, $coupon_code, $amount, $type, $entry, $form ) {
$start_date = rgar( $this->_args['meta'], 'start_date' );
if ( $start_date === '' || ! strtotime( $start_date ) ) {
$date = current_datetime();
$start_date = $date->format( 'Y-m-d H:i:s' );
}
// WooCommerce coupon uses the Post Title as the coupon code hence $coupon_code is assigned to Post Title and $coupon_name is assigned to the Post Excerpt
$coupon = array(
'post_title' => $coupon_code,
'post_excerpt' => $coupon_name,
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon',
'post_date' => $start_date,
);
$new_coupon_id = wp_insert_post( $coupon );
$meta = wp_parse_args( $this->_args['meta'], array(
'discount_type' => $type,
'coupon_amount' => $amount,
'individual_use' => 'yes',
'product_ids' => '',
'exclude_product_ids' => '',
'usage_limit' => '1',
'expiry_date' => '',
'apply_before_tax' => 'no',
'free_shipping' => 'no',
'exclude_sale_items' => 'no',
'product_categories' => '',
'exclude_product_categories' => '',
'minimum_amount' => '',
'customer_email' => '',
) );
foreach ( $meta as $meta_key => $meta_value ) {
if ( $meta_value instanceof Closure && is_callable( $meta_value ) ) {
$meta[ $meta_key ] = call_user_func( $meta_value, $entry, $form, $this );
}
update_post_meta( $new_coupon_id, $meta_key, $meta[ $meta_key ] );
}
}
public function get_coupon_codes( $entry, $source_field_id ) {
return array_filter( explode( "\n", rgar( $entry, $source_field_id ) ) );
}
function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return (int) $form_id === (int) $this->_args['form_id'];
}
public function should_abort( $entry ) {
if ( empty( $this->_args['required_fields'] ) ) {
return false;
}
foreach ( $this->_args['required_fields'] as $field_id ) {
$value = rgar( $entry, (string) $field_id );
if ( rgblank( $value ) ) {
return true;
}
}
return false;
}
}
# Configuration
new GW_Create_Coupon( array(
// ID of the form which will be used to create coupons
'form_id' => 608,
// ID of the field whose value will be used as the coupon code
'source_field_id' => 1,
// ID of the field whose value will be used as the title of the coupon
'name_field_id' => 2,
// which plugin the coupon should be created for (i.e. WooCommerce = 'wc')
'plugin' => '', // accepts: 'gf', 'wc', 'edd'
// type of coupon code to be created, available types will differ depending on the plugin
'type' => '',
// amount of the coupon discount
'amount' => 10,
// The IDs of the fields that must not be empty, else the coupon won't be created
'required_fields' => array( 1, 2, 1.3 ),
) );