-
Notifications
You must be signed in to change notification settings - Fork 0
/
Coupons.php
297 lines (272 loc) · 10.1 KB
/
Coupons.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/**
* Coupons
*
* @author Graham Schmidt
* @created @2012-05-10
*/
class Coupons {
/**
* Get the discount type for the coupon
*
* @author Graham Schmidt
* @param coupon_code - string
* @return mixed (string/boolean) - type of discount (or boolean false)
*/
public static function getDiscountDetails($coupon_code = '')
{
global $finder_db;
if ($coupon_code == '') {
return false;
}
$sql =
"SELECT
coupon_id
, coupon_code
, description
, value
, type
, req_skus
, can_recur
, count
, expiration
, active
, is_giftcard
, coupon_source
, order_type
, crm_class
FROM coupons
WHERE coupon_code = '$coupon_code'
";
try {
$query = $finder_db->query($sql);
$query->tossIfNoRows();
return $query->fetchRow();
} catch (fNoRowsException $e) {
return false;
}
}
/**
* Get the discount type
*/
public static function getDiscountType($coupon_code = '')
{
$details = self::getDiscountDetails($coupon_code);
return $details['type'];
}
/**
* Get the discount description
*/
public static function getDiscountDescription($coupon_code = '', $custom_message = '')
{
if ($coupon_code == '') {
return false;
}
$details = self::getDiscountDetails($coupon_code);
if ($details['type'] == 'Percent Discount') {
$percent_val = $details['value'] * 100;
if ($details['req_skus'] != '') {
$coupon_response = $details['description'];
} else {
$coupon_response = "Save $percent_val% with your promo code: ".$coupon_code;
}
}
if ($details['type'] == 'Free Shipping') {
$coupon_response = "You'll receive free shipping with your promo code: ".$coupon_code;
}
if ($details['type'] == 'Shipping Discount') {
$discount = number_format($details['value'], 2);
$coupon_response = "Save $$discount off shipping with your promo code: ".$coupon_code;
}
if ($details['type'] == 'Buy 1 Get 1 Free') {
$coupon_response = $details['description'];
}
if ($details['type'] == 'Flat Discount') {
$discount = number_format($details['value'], 2);
if ($details['req_skus'] != '') {
$coupon_response = $details['description'];
} else {
$coupon_response = "Save $$discount off the total order with your promo code: ".$coupon_code;
}
}
if ($custom_message != '') {
//replace all variables (coupon_code, discount, percent_val)
(isset($coupon_code) ? $coupon_response = str_replace('{coupon_code}', $coupon_code, $custom_message) : '');
(isset($discount) ? $coupon_response = str_replace('{discount}', $discount, $custom_message) : '');
(isset($percent_val) ? $coupon_response = str_replace('{percent_val}', $percent_val, $custom_message) : '');
}
return $coupon_response;
}
/**
* Apply the discount to a price
*
* @author Graham Schmidt
* @param coupon_code - string
* @return mixed (string/boolean) - type of discount (or boolean false)
*/
public static function getDiscountOnPrice($coupon_code = '', $price_in_dollars = 0, $custom_message = '')
{
if ($coupon_code == '' || $price_in_dollars == 0) {
return false;
}
$details = self::getDiscountDetails($coupon_code);
$free_shipping = false;
if ($details['type'] == 'Percent Discount') {
$discount = round($price_in_dollars * $details['value'], 2);
$percent_val = $details['value'] * 100;
$discount = number_format($discount, 2);
$coupon_response = "Save $percent_val% with your promo code: ".$coupon_code;
}
if ($details['type'] == 'Free Shipping') {
$free_shipping = true;
$discount = 4.99; // default shipping
$coupon_response = "You'll receive free shipping with your promo code: ".$coupon_code;
}
if ($details['type'] == 'Shipping Discount') {
$discount = number_format($details['value'], 2);
$coupon_response = "Save $$discount off shipping with your promo code: ".$coupon_code;
}
if ($details['type'] == 'Buy 1 Get 1 Free') {
// @todo calculate price based on basket
$discount = 0;
$coupon_response = $details['description'];
}
if ($details['type'] == 'Flat Discount') {
$discount = number_format($details['value'], 2);
$coupon_response = "Save $$discount off the total order with your promo code: ".$coupon_code;
}
if ($custom_message != '') {
//replace all variables (coupon_code, discount, percent_val)
$coupon_response = (isset($coupon_code) ? str_replace('{coupon_code}', $coupon_code, $custom_message) : $coupon_response);
$coupon_response = (isset($discount) ? str_replace('{discount}', $discount, $coupon_response) : $coupon_response);
$coupon_response = (isset($percent_val) ? str_replace('{percent_val}', $percent_val, $coupon_response) : $coupon_response);
}
return array(
'discount' => $discount,
'type' => $details['type'],
'free_shipping' => $free_shipping,
'value' => $details['value'],
'description' => $details['description'],
'response' => $coupon_response,
);
}
/**
* Determines if coupon is valid or not
*
* @author Graham Schmidt
* @param coupon_code - string
* @return boolean
*/
public static function isValidCoupon($coupon_code = '')
{
$details = self::getDiscountDetails($coupon_code);
return $details['coupon_code'] == $coupon_code;
}
/**
* Check if the coupon code is valid to use by a type
*
* @author Graham Schmidt
* @param coupon_code - string
* @return boolean
*/
public static function isType($coupon_code = '', $type = '')
{
$details = self::getDiscountDetails($coupon_code);
$types = explode("|", $details['coupon_source']);
foreach ($types as $coupon_type) {
if ($type == $coupon_type) {
return true;
}
}
return false;
}
/**
* Check if the coupon code applies to a particular SKU based on req_skus
* on the coupon_code
*
* @author Graham Schmidt
* @param coupon_code - string
* @param sku - string
* @return boolean
*/
public static function appliesToSku($coupon_code = '', $sku = '')
{
$details = self::getDiscountDetails($coupon_code);
$types = explode("|", $details['coupon_source']);
$requirment_met = false;
if ($details['req_skus'] != '') {
$required_skus = explode('|', $details['req_skus']);
foreach ($required_skus as $req_sku) {
if ($details['type'] == 'Buy 1 Get 1 Free'
&& $req_sku != ''
&& preg_match($req_sku, $sku)
) {
$requirment_met = true;
} else if ($req_sku === $sku) {
$requirment_met = true;
}
}
} else {
$requirment_met = true;
}
return $requirment_met;
}
/**
* Check if the requirements are met for a coupon
*
* @author Graham Schmidt
* @param
* @return booelan
*/
public static function checkRequirementsMet($shopping_cart = array(), $req_skus_string = '', $coupon_type = '')
{
if ($req_skus_string != '') {
$required_skus = explode('|', $req_skus_string);
foreach ($required_skus as $req_sku) {
// Skip blanks
if ($req_sku != '') {
// Special Type
if ($coupon_type == 'Buy 1 Get 1 Free') {
// go through each item in cart...check if SKU matches
// RegEx and there are an even number of items in cart
if (self::checkItemsMatchInCart($shopping_cart, $req_sku) > 1) {
foreach ($shopping_cart as $sku => $quantity) {
if (preg_match($req_sku, $sku)) {
return true;
}
}
}
} else {
// Req_sku must be in basket
if ( is_array($shopping_cart)
&& array_key_exists($req_sku, $shopping_cart)
) {
return true;
}
}
}
}
} else {
return true;
}
// Not met
return false;
}
/**
* Check how many items in cart match a RegEx pattern
*
* @author Graham Schmidt
* @created @2012-06-22
*/
public static function checkItemsMatchInCart($shopping_cart, $regex)
{
unset($items_matched);
$items_matched = 0;
foreach ($shopping_cart as $sku => $quantity) {
if (preg_match($regex, $sku)) {
$items_matched += $quantity;
}
}
return $items_matched;
}
}