|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LukePOLO\LaraCart\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use LukePOLO\LaraCart\CartItem; |
|
7
|
|
|
use LukePOLO\LaraCart\Exceptions\CouponException; |
|
8
|
|
|
use LukePOLO\LaraCart\Exceptions\InvalidPrice; |
|
9
|
|
|
use LukePOLO\LaraCart\LaraCart; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class CouponTrait. |
|
13
|
|
|
*/ |
|
14
|
|
|
trait CouponTrait |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var bool |
|
18
|
|
|
*/ |
|
19
|
|
|
public $appliedToCart = true; |
|
20
|
|
|
|
|
21
|
|
|
use CartOptionsMagicMethodsTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Sets all the options for the coupon. |
|
25
|
|
|
* |
|
26
|
|
|
* @param $options |
|
27
|
|
|
*/ |
|
28
|
|
|
public function setOptions($options) |
|
29
|
|
|
{ |
|
30
|
|
|
foreach ($options as $key => $value) { |
|
31
|
|
|
$this->$key = $value; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Checks to see if we can apply the coupon. |
|
37
|
|
|
* |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
|
|
public function canApply() |
|
41
|
|
|
{ |
|
42
|
|
|
try { |
|
43
|
|
|
$this->discount(true); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
return true; |
|
46
|
|
|
} catch (CouponException $e) { |
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the reason why a coupon has failed to apply. |
|
53
|
|
|
* |
|
54
|
|
|
* @deprecated 1.3 |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getMessage() |
|
59
|
|
|
{ |
|
60
|
|
|
try { |
|
61
|
|
|
$this->discount(true); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
return config('laracart.coupon_applied_message', 'Coupon Applied'); |
|
64
|
|
|
} catch (CouponException $e) { |
|
65
|
|
|
return $e->getMessage(); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Gets the failed message for a coupon. |
|
71
|
|
|
* |
|
72
|
|
|
* @return null|string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getFailedMessage() |
|
75
|
|
|
{ |
|
76
|
|
|
try { |
|
77
|
|
|
$this->discount(true); |
|
|
|
|
|
|
78
|
|
|
} catch (CouponException $e) { |
|
79
|
|
|
return $e->getMessage(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Checks the minimum subtotal needed to apply the coupon. |
|
85
|
|
|
* |
|
86
|
|
|
* @param $minAmount |
|
87
|
|
|
* @param $throwErrors |
|
88
|
|
|
* |
|
89
|
|
|
* @throws CouponException |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
public function checkMinAmount($minAmount, $throwErrors = true) |
|
94
|
|
|
{ |
|
95
|
|
|
$laraCart = \App::make(LaraCart::SERVICE); |
|
96
|
|
|
|
|
97
|
|
|
if ($laraCart->subTotal(false, false, false) >= $minAmount) { |
|
98
|
|
|
return true; |
|
99
|
|
|
} else { |
|
100
|
|
|
if ($throwErrors) { |
|
101
|
|
|
throw new CouponException('You must have at least a total of '.$laraCart->formatMoney($minAmount)); |
|
102
|
|
|
} else { |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns either the max discount or the discount applied based on what is passed through. |
|
110
|
|
|
* |
|
111
|
|
|
* @param $maxDiscount |
|
112
|
|
|
* @param $discount |
|
113
|
|
|
* @param $throwErrors |
|
114
|
|
|
* |
|
115
|
|
|
* @throws CouponException |
|
116
|
|
|
* |
|
117
|
|
|
* @return mixed |
|
118
|
|
|
*/ |
|
119
|
|
|
public function maxDiscount($maxDiscount, $discount, $throwErrors = true) |
|
120
|
|
|
{ |
|
121
|
|
|
if ($maxDiscount == 0 || $maxDiscount > $discount) { |
|
122
|
|
|
return $discount; |
|
123
|
|
|
} else { |
|
124
|
|
|
if ($throwErrors) { |
|
125
|
|
|
throw new CouponException('This has a max discount of '.\App::make(\LukePOLO\Laracart\LaraCart::SERVICE)->formatMoney($maxDiscount)); |
|
126
|
|
|
} else { |
|
127
|
|
|
return $maxDiscount; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Checks to see if the times are valid for the coupon. |
|
134
|
|
|
* |
|
135
|
|
|
* @param Carbon $startDate |
|
136
|
|
|
* @param Carbon $endDate |
|
137
|
|
|
* @param $throwErrors |
|
138
|
|
|
* |
|
139
|
|
|
* @throws CouponException |
|
140
|
|
|
* |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
|
|
public function checkValidTimes(Carbon $startDate, Carbon $endDate, $throwErrors = true) |
|
144
|
|
|
{ |
|
145
|
|
|
if (Carbon::now()->between($startDate, $endDate)) { |
|
146
|
|
|
return true; |
|
147
|
|
|
} else { |
|
148
|
|
|
if ($throwErrors) { |
|
149
|
|
|
throw new CouponException('This coupon has expired'); |
|
150
|
|
|
} else { |
|
151
|
|
|
return false; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Sets a discount to an item with what code was used and the discount amount. |
|
158
|
|
|
* |
|
159
|
|
|
* @param CartItem $item |
|
160
|
|
|
* @param $discountAmount |
|
161
|
|
|
* |
|
162
|
|
|
* @throws InvalidPrice |
|
163
|
|
|
*/ |
|
164
|
|
|
public function setDiscountOnItem(CartItem $item, $discountAmount) |
|
165
|
|
|
{ |
|
166
|
|
|
if (!is_numeric($discountAmount)) { |
|
167
|
|
|
throw new InvalidPrice('You must use a discount amount.'); |
|
168
|
|
|
} |
|
169
|
|
|
$this->appliedToCart = false; |
|
170
|
|
|
$item->code = $this->code; |
|
|
|
|
|
|
171
|
|
|
$item->discount = $discountAmount; |
|
|
|
|
|
|
172
|
|
|
$item->couponInfo = $this->options; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.