|
1
|
|
|
<?php if ( !defined( 'ABSPATH' ) ) exit; |
|
2
|
|
|
class wps_coupon_ctr { |
|
3
|
|
|
/** Define the main directory containing the template for the current plugin |
|
4
|
|
|
* @var string |
|
5
|
|
|
*/ |
|
6
|
|
|
private $template_dir; |
|
7
|
|
|
/** |
|
8
|
|
|
* Define the directory name for the module in order to check into frontend |
|
9
|
|
|
* @var string |
|
10
|
|
|
*/ |
|
11
|
|
|
private $plugin_dirname = WPS_COUPON_DIR; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
function __construct() { |
|
|
|
|
|
|
14
|
|
|
$this->template_dir = WPS_COUPON_PATH . WPS_COUPON_DIR . "/templates/"; |
|
15
|
|
|
add_shortcode( 'wps_coupon', array($this, 'display_coupons') ); |
|
16
|
|
|
|
|
17
|
|
|
$wpshop_shop_type = get_option('wpshop_shop_type', WPSHOP_DEFAULT_SHOP_TYPE); |
|
18
|
|
|
if ( $wpshop_shop_type == 'sale' ) { |
|
19
|
|
|
add_action( 'init', array( $this, 'create_coupon_custom_type' ) ); |
|
20
|
|
|
add_action('add_meta_boxes', array( $this, 'add_meta_boxes') ); |
|
21
|
|
|
add_action('manage_'.WPSHOP_NEWTYPE_IDENTIFIER_COUPON.'_posts_custom_column', array( $this, 'wps_coupons_custom_columns')); |
|
22
|
|
|
add_filter('manage_edit-'.WPSHOP_NEWTYPE_IDENTIFIER_COUPON.'_columns', array( $this, 'wps_coupons_edit_columns')); |
|
23
|
|
|
add_action('save_post', array($this, 'save_coupon_custom_informations')); |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Create Coupon custom post type |
|
29
|
|
|
*/ |
|
30
|
|
View Code Duplication |
function create_coupon_custom_type() { |
|
|
|
|
|
|
31
|
|
|
register_post_type(WPSHOP_NEWTYPE_IDENTIFIER_COUPON, array( |
|
32
|
|
|
'labels' => array( |
|
33
|
|
|
'name' => __('Coupons', 'wpshop'), |
|
34
|
|
|
'singular_name' => __('coupon', 'wpshop'), |
|
35
|
|
|
'add_new' => __('Add coupon', 'wpshop'), |
|
36
|
|
|
'add_new_item' => __('Add New coupon', 'wpshop'), |
|
37
|
|
|
'edit' => __('Edit', 'wpshop'), |
|
38
|
|
|
'edit_item' => __('Edit coupon', 'wpshop'), |
|
39
|
|
|
'new_item' => __('New coupon', 'wpshop'), |
|
40
|
|
|
'view' => __('View coupon', 'wpshop'), |
|
41
|
|
|
'view_item' => __('View coupon', 'wpshop'), |
|
42
|
|
|
'search_items' => __('Search coupons', 'wpshop'), |
|
43
|
|
|
'not_found' => __('No coupons found', 'wpshop'), |
|
44
|
|
|
'not_found_in_trash' => __('No coupons found in trash', 'wpshop'), |
|
45
|
|
|
'parent-item-colon' => '' |
|
46
|
|
|
), |
|
47
|
|
|
'description' => __('This is where store coupons are stored.', 'wpshop'), |
|
48
|
|
|
'public' => true, |
|
49
|
|
|
'show_ui' => true, |
|
50
|
|
|
'capability_type' => 'post', |
|
51
|
|
|
'publicly_queryable' => false, |
|
52
|
|
|
'exclude_from_search' => true, |
|
53
|
|
|
'show_in_menu' => 'edit.php?post_type='.WPSHOP_NEWTYPE_IDENTIFIER_ORDER, |
|
54
|
|
|
'show_in_admin_bar' => false, |
|
55
|
|
|
'hierarchical' => false, |
|
56
|
|
|
'show_in_nav_menus' => false, |
|
57
|
|
|
'rewrite' => false, |
|
58
|
|
|
'query_var' => true, |
|
59
|
|
|
'supports' => array( 'title', ), |
|
60
|
|
|
'has_archive' => false |
|
61
|
|
|
)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Add Meta box to Coupon cutom post type |
|
66
|
|
|
*/ |
|
67
|
|
|
function add_meta_boxes() { |
|
|
|
|
|
|
68
|
|
|
add_meta_box('wpshop_coupon_main_info', __('Informations', 'wpshop'), array( $this, 'wps_coupon_info_box'), WPSHOP_NEWTYPE_IDENTIFIER_COUPON, 'normal', 'high'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Coupon custom type Meta box content |
|
73
|
|
|
*/ |
|
74
|
|
|
function wps_coupon_info_box() { |
|
|
|
|
|
|
75
|
|
|
$output = ''; |
|
76
|
|
|
$default_currency = wpshop_tools::wpshop_get_currency( false ); |
|
77
|
|
|
// Coupon Datas |
|
78
|
|
|
$metadata = get_post_custom(); |
|
79
|
|
|
$coupon_code = !empty($metadata['wpshop_coupon_code'][0]) ? $metadata['wpshop_coupon_code'][0] : null; |
|
80
|
|
|
$coupon_discount_amount = !empty($metadata['wpshop_coupon_discount_value'][0]) ? $metadata['wpshop_coupon_discount_value'][0] : null; |
|
81
|
|
|
$wpshop_coupon_discount_type = !empty($metadata['wpshop_coupon_discount_type'][0]) ? $metadata['wpshop_coupon_discount_type'][0] : null; |
|
82
|
|
|
|
|
83
|
|
|
$coupon_receiver = !empty($metadata['wpshop_coupon_individual_use'][0]) ? unserialize($metadata['wpshop_coupon_individual_use'][0]) : array(); |
|
84
|
|
|
$coupon_limit_usage = !empty($metadata['wpshop_coupon_usage_limit'][0]) ? $metadata['wpshop_coupon_usage_limit'][0] : ''; |
|
85
|
|
|
|
|
86
|
|
|
$wpshop_coupon_minimum_amount = ( !empty($metadata['wpshop_coupon_minimum_amount'][0]) ) ? $metadata['wpshop_coupon_minimum_amount'][0] : ''; |
|
87
|
|
|
$wpshop_coupon_minimum_amount = unserialize( $wpshop_coupon_minimum_amount ); |
|
88
|
|
|
ob_start(); |
|
89
|
|
|
require( wpshop_tools::get_template_part( WPS_COUPON_DIR, $this->template_dir, "backend", "coupon-metabox") ); |
|
90
|
|
|
$output = ob_get_contents(); |
|
91
|
|
|
ob_end_clean(); |
|
92
|
|
|
|
|
93
|
|
|
echo $output; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Add custom columns to coupons list in administration |
|
98
|
|
|
*/ |
|
99
|
|
|
function wps_coupons_custom_columns( $column ) { |
|
|
|
|
|
|
100
|
|
|
global $post; |
|
101
|
|
|
|
|
102
|
|
|
$metadata = get_post_custom(); |
|
103
|
|
|
$wpshop_coupon_discount_type = !empty($metadata['wpshop_coupon_discount_type'][0]) ? $metadata['wpshop_coupon_discount_type'][0] : null; |
|
104
|
|
|
switch( $column ){ |
|
105
|
|
|
case "coupon_code": |
|
106
|
|
|
echo $metadata['wpshop_coupon_code'][0]; |
|
107
|
|
|
break; |
|
108
|
|
|
case "coupon_discount_amount": |
|
109
|
|
|
$currency = wpshop_tools::wpshop_get_currency( false ); |
|
110
|
|
|
echo $metadata['wpshop_coupon_discount_value'][0].' '.( (!empty($wpshop_coupon_discount_type) && $wpshop_coupon_discount_type == 'percent') ? '%' : $currency) ; |
|
111
|
|
|
break; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Set the custom colums |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
|
|
function wps_coupons_edit_columns() { |
|
|
|
|
|
|
120
|
|
|
$columns = array( |
|
121
|
|
|
'cb' => '<input type="checkbox" />', |
|
122
|
|
|
'title' => __('Name', 'wpshop'), |
|
123
|
|
|
'coupon_code' => __('Code', 'wpshop'), |
|
124
|
|
|
'coupon_discount_amount' => __('Discount amount', 'wpshop'), |
|
125
|
|
|
); |
|
126
|
|
|
return $columns; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Save custom informations on Save post action |
|
131
|
|
|
*/ |
|
132
|
|
|
function save_coupon_custom_informations( $post_id ) { |
|
|
|
|
|
|
133
|
|
|
if ( wp_is_post_revision( $post_id ) ) |
|
134
|
|
|
return; |
|
135
|
|
|
|
|
136
|
|
|
if( !empty($post_id) && (get_post_type($post_id) == WPSHOP_NEWTYPE_IDENTIFIER_COUPON) ) { |
|
137
|
|
|
$wps_coupon_mdl = new wps_coupon_model(); |
|
138
|
|
|
|
|
139
|
|
|
$data = array( |
|
140
|
|
|
'wpshop_coupon_mini_amount' => !empty( $_POST['wpshop_coupon_mini_amount'] ) ? sanitize_text_field( $_POST['wpshop_coupon_mini_amount'] ) : '', |
|
141
|
|
|
'wpshop_coupon_min_mount_shipping_rule' => !empty( $_POST['wpshop_coupon_min_mount_shipping_rule'] ) ? sanitize_text_field( $_POST['wpshop_coupon_min_mount_shipping_rule'] ) : '', |
|
142
|
|
|
'coupon_code' => !empty( $_POST['coupon_code'] ) ? sanitize_text_field( $_POST['coupon_code'] ) : '', |
|
143
|
|
|
'coupon_discount_amount' => !empty( $_POST['coupon_discount_amount'] ) ? sanitize_text_field( $_POST['coupon_discount_amount'] ) : '', |
|
144
|
|
|
'wpshop_coupon_discount_type' => !empty( $_POST['coupon_type'] ) ? sanitize_text_field( $_POST['coupon_type'] ) : '', |
|
145
|
|
|
'coupon_receiver' => !empty( $_POST['coupon_receiver'] ) ? (array) $_POST['coupon_receiver'] : '', |
|
146
|
|
|
'coupon_usage_limit' => !empty( $_POST['coupon_usage_limit'] ) ? sanitize_text_field( $_POST['coupon_usage_limit'] ) : '', |
|
147
|
|
|
'post_ID' => $post_id, |
|
148
|
|
|
); |
|
149
|
|
|
|
|
150
|
|
|
$wps_coupon_mdl->save_coupons_informations( $data ); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* APPLY COUPON |
|
156
|
|
|
* @param string $code |
|
157
|
|
|
* @return array |
|
158
|
|
|
*/ |
|
159
|
|
|
function applyCoupon( $code ) { |
|
|
|
|
|
|
160
|
|
|
global $wpdb, $wpshop_cart; |
|
161
|
|
|
|
|
162
|
|
|
/** Default currency **/ |
|
163
|
|
|
$default_currency = wpshop_tools::wpshop_get_currency( false ); |
|
164
|
|
|
|
|
165
|
|
|
$coupon_infos = array(); |
|
166
|
|
|
|
|
167
|
|
|
/** Coupon infos **/ |
|
168
|
|
|
$query = $wpdb->prepare(' |
|
169
|
|
|
SELECT META.post_id |
|
170
|
|
|
FROM '.$wpdb->prefix.'postmeta META |
|
171
|
|
|
LEFT JOIN '.$wpdb->prefix.'posts POSTS ON POSTS.ID = META.post_id |
|
172
|
|
|
WHERE |
|
173
|
|
|
POSTS.post_type = %s AND |
|
174
|
|
|
META.meta_key = "wpshop_coupon_code" AND |
|
175
|
|
|
META.meta_value = %s AND |
|
176
|
|
|
POSTS.post_status = %s |
|
177
|
|
|
', WPSHOP_NEWTYPE_IDENTIFIER_COUPON, $code, 'publish'); |
|
178
|
|
|
$result = $wpdb->get_row($query); |
|
179
|
|
|
|
|
180
|
|
|
if ( !empty($result) ) { |
|
181
|
|
|
$coupon_amount = get_post_meta( $result->post_id, 'wpshop_coupon_discount_value', true ); |
|
182
|
|
|
|
|
183
|
|
|
if ( !empty($coupon_amount) && $coupon_amount > 0) { |
|
184
|
|
|
$coupon_usage = get_post_meta( $result->post_id, '_wpshop_coupon_usage', true ); |
|
185
|
|
|
$coupon_usage_limit = get_post_meta( $result->post_id, 'wpshop_coupon_usage_limit', true ); |
|
186
|
|
|
$coupon_individual_usage = get_post_meta( $result->post_id, 'wpshop_coupon_individual_use', true ); |
|
187
|
|
|
|
|
188
|
|
|
$coupon_order_amount_mini = get_post_meta( $result->post_id, 'wpshop_coupon_minimum_amount', true); |
|
189
|
|
|
|
|
190
|
|
|
$current_user_id = get_current_user_id(); |
|
191
|
|
|
$individual_usage = $usage_limit = false; |
|
192
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
/** Checking coupon params & logged user **/ |
|
195
|
|
|
if ( (!empty($coupon_individual_usage) || !empty($coupon_usage_limit) ) && $current_user_id == 0) { |
|
196
|
|
|
return array('status' => false, 'message' => __('You must be logged to use this coupon','wpshop')); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** Individual use checking **/ |
|
200
|
|
|
if ( !empty($coupon_individual_usage) ) { |
|
201
|
|
|
|
|
202
|
|
|
if ( in_array($current_user_id, $coupon_individual_usage) ) { |
|
203
|
|
|
$individual_usage = true; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
else { |
|
207
|
|
|
$individual_usage = true; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
/** Checking Usage limitation **/ |
|
212
|
|
|
if ($individual_usage) { |
|
213
|
|
|
if ( !empty($coupon_usage_limit) ) { |
|
214
|
|
|
|
|
215
|
|
|
if( ( !empty($coupon_usage) && array_key_exists($current_user_id, $coupon_usage) ) || empty($coupon_usage ) || empty($coupon_usage[$current_user_id]) ) { |
|
216
|
|
|
$usage_limit = ( ( !empty($coupon_usage_limit) && $coupon_usage[$current_user_id] < $coupon_usage_limit) || empty($coupon_usage) || empty($coupon_usage[$current_user_id]) ) ? true : false; |
|
217
|
|
|
} |
|
218
|
|
|
elseif( empty($coupon_usage) ) { |
|
219
|
|
|
$usage_limit = true; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
else { |
|
223
|
|
|
$usage_limit = true; |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
else { |
|
227
|
|
|
return array('status' => false, 'message' => __('You are not allowed to use this coupon','wpshop')); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
/** Apply Coupon **/ |
|
232
|
|
|
if ( $usage_limit ) { |
|
233
|
|
|
/** Check orderamount Limit **/ |
|
234
|
|
|
$order_amount_limit = true; |
|
235
|
|
|
|
|
236
|
|
|
if ( !empty($coupon_order_amount_mini) && !empty($coupon_order_amount_mini['amount']) ) { |
|
237
|
|
|
|
|
238
|
|
|
if ( !empty($coupon_order_amount_mini) && !empty($coupon_order_amount_mini['shipping_rule']) && $coupon_order_amount_mini['shipping_rule'] == 'shipping_cost' && $_SESSION['cart']['order_grand_total_before_discount'] < $coupon_order_amount_mini['amount'] ) { |
|
239
|
|
|
$coupon_infos = array('status' => false, 'message' => __('This coupon is available for an order from ','wpshop').' '.$coupon_order_amount_mini['amount'].' '.$default_currency ); |
|
240
|
|
|
$order_amount_limit = false; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
elseif( !empty($coupon_order_amount_mini) && !empty($coupon_order_amount_mini['shipping_rule']) && $coupon_order_amount_mini['shipping_rule'] == 'no_shipping_cost' && $_SESSION['cart']['order_total_ttc'] < $coupon_order_amount_mini['amount'] ) { |
|
244
|
|
|
$coupon_infos = array('status' => false, 'message' => __('This coupon is available for an order from ','wpshop').' '.$coupon_order_amount_mini['amount'].' '.$default_currency.' '.__('without shipping cost', 'wpshop') ); |
|
245
|
|
|
$order_amount_limit = false; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
} |
|
249
|
|
|
if ( $order_amount_limit ) { |
|
250
|
|
|
$_SESSION['cart']['coupon_id'] = $result->post_id; |
|
251
|
|
|
$coupon_infos = array('status' => true, 'message' => ''); |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
else { |
|
255
|
|
|
$coupon_infos = array('status' => false, 'message' => __('You are not allowed to use this coupon','wpshop') ); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
} |
|
259
|
|
|
else { |
|
260
|
|
|
$coupon_infos = array('status' => false, 'message' => __('This coupon is not valid','wpshop')); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
} |
|
264
|
|
|
else { |
|
265
|
|
|
$coupon_infos = array('status' => false, 'message' => __('This coupon doesn`t exist','wpshop')); |
|
266
|
|
|
} |
|
267
|
|
|
return $coupon_infos; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Display coupons list |
|
272
|
|
|
* @param integer $customer_id |
|
273
|
|
|
* @return string |
|
274
|
|
|
*/ |
|
275
|
|
|
function display_coupons( $customer_id = '' ) { |
|
|
|
|
|
|
276
|
|
|
$is_from_admin = ( !empty($customer_id) ) ? true : false; |
|
277
|
|
|
$customer_id = ( !empty($customer_id) ) ? $customer_id : get_current_user_id(); |
|
278
|
|
|
$coupons_mdl = new wps_coupon_model(); |
|
279
|
|
|
$coupons = $coupons_mdl->get_coupons(); |
|
280
|
|
|
$output = $coupons_rows = ''; |
|
281
|
|
|
|
|
282
|
|
|
if( !empty($coupons) ) { |
|
283
|
|
|
foreach( $coupons as $coupon ) { |
|
284
|
|
|
$coupon_individual_usage = get_post_meta( $coupon->ID, 'wpshop_coupon_individual_use', true ); |
|
285
|
|
|
if( empty($coupon_individual_usage) || ( !empty($coupon_individual_usage) && in_array( $customer_id , $coupon_individual_usage) ) ) { |
|
286
|
|
|
$coupon_code = get_post_meta( $coupon->ID, 'wpshop_coupon_code', true ); |
|
287
|
|
|
$coupon_value = get_post_meta( $coupon->ID, 'wpshop_coupon_discount_value', true ); |
|
288
|
|
|
$discount_type = get_post_meta( $coupon->ID, 'wpshop_coupon_discount_type', true ); |
|
289
|
|
|
$coupon_date = get_post_meta( $coupon->ID, 'wpshop_coupon_expiry_date', true); |
|
290
|
|
|
$coupon_validity_date = ( !empty($coupon_date) ) ? $coupon_date : __( 'No validity date', 'wpshop'); |
|
291
|
|
|
$coupon_value .= ( !empty($discount_type) && $discount_type == 'amount') ? wpshop_tools::wpshop_get_currency( false ) : '%'; |
|
292
|
|
|
ob_start(); |
|
293
|
|
|
require( wpshop_tools::get_template_part( WPS_COUPON_DIR, $this->template_dir, "frontend", "coupon") ); |
|
294
|
|
|
$coupons_rows .= ob_get_contents(); |
|
295
|
|
|
ob_end_clean(); |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
ob_start(); |
|
299
|
|
|
require( wpshop_tools::get_template_part( WPS_COUPON_DIR, $this->template_dir, "frontend", "coupons") ); |
|
300
|
|
|
$output .= ob_get_contents(); |
|
301
|
|
|
ob_end_clean(); |
|
302
|
|
|
|
|
303
|
|
|
} |
|
304
|
|
|
else { |
|
305
|
|
|
$output = '<div class="wps-alert-info">' .__( 'Sorry ! No available coupon', 'wpshop' ) .'</div>'; |
|
306
|
|
|
} |
|
307
|
|
|
return $output; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
function getCoupons() { |
|
|
|
|
|
|
311
|
|
|
$wps_coupon_mdl = new wps_coupon_model(); |
|
312
|
|
|
$result = $wps_coupon_mdl->get_coupons(); |
|
313
|
|
|
unset($wps_coupon_mdl); |
|
314
|
|
|
return $result; |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.