PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / app / Models / Coupon.php

Coupon.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Models/Coupon.php

233 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Models;
4
5
6 use FluentCart\App\Models\Concerns\CanSearch;
7 use FluentCart\App\Models\Concerns\HasActivity;
8 use FluentCart\App\Services\DateTime\DateTime;
9 use FluentCart\Framework\Support\Arr;
10
11 class Coupon extends Model
12 {
13 use CanSearch, HasActivity;
14
15 protected $table = 'fct_coupons';
16
17 protected $primaryKey = 'id';
18
19 protected $guarded = ['id'];
20
21 protected $fillable = [
22 'parent',
23 'title',
24 'code',
25 'status',
26 'type',
27 'conditions',
28 'amount',
29 'stackable',
30 'priority',
31 'use_count',
32 'notes',
33 'show_on_checkout',
34 'start_date',
35 'end_date',
36 ];
37
38 protected $casts = [
39 'max_uses' => 'integer',
40 ];
41
42
43 public function setConditionsAttribute($value)
44 {
45
46 if ($value) {
47 $decoded = \json_encode($value, true);
48 if (!($decoded)) {
49 $decoded = '[]';
50 }
51 } else {
52 $decoded = '[]';
53 }
54
55 $this->attributes['conditions'] = $decoded;
56 }
57
58 public function getConditionsAttribute($value)
59 {
60 if (!$value) {
61 return [];
62 }
63
64 return \json_decode($value, true);
65 }
66
67 // public function setMaxPerCustomer($value){
68 // //dd($value);
69 // return empty($value)? $value: ((intval)($value));
70 // }
71
72 // public function getMaxPerCustomer($value){
73 // return empty($value)? ((intval)($value)): $value;
74 // }
75
76 public function appliedCoupons()
77 {
78 return $this->hasMany(AppliedCoupon::class, 'coupon_id', 'id');
79 }
80
81 public function orders(): \FluentCart\Framework\Database\Orm\Relations\BelongsToMany
82 {
83 return $this->belongsToMany(Order::class, 'fct_applied_coupons', 'coupon_id', 'order_id');
84 }
85
86 public function setSettingsAttribute($value)
87 {
88 if (is_array($value) || is_object($value)) {
89 $value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
90 }
91 $this->attributes['settings'] = $value;
92 }
93
94 public function getSettingsAttribute($value)
95 {
96 if (is_string($value)) {
97 $decoded = json_decode($value, true);
98 return $decoded ?: $value;
99 }
100
101 return $value;
102 }
103
104 public function setOtherInfoAttribute($value)
105 {
106 if (is_string($value)) {
107 $value = json_decode($value, true);
108 }
109
110 if (is_array($value) || is_object($value)) {
111 // Ensure `buy_products` and `get_products` are arrays before mapping
112 if (isset($value['buy_products']) && is_array($value['buy_products'])) {
113 $value['buy_products'] = array_map('intval', $value['buy_products']);
114 }
115
116 if (isset($value['get_products']) && is_array($value['get_products'])) {
117 $value['get_products'] = array_map('intval', $value['get_products']);
118 }
119
120 // Encode the modified object or array back to JSON
121 $this->attributes['other_info'] = json_encode($value);
122 } else {
123 // Handle non-array or non-object cases
124 $this->attributes['other_info'] = json_encode([]);
125 }
126
127 }
128
129 public function getOtherInfoAttribute($value)
130 {
131 return !empty($value) ? json_decode($value, true) : [];
132 }
133
134 public function setCategoriesAttribute($value)
135 {
136 if (is_array($value) || is_object($value)) {
137 $this->attributes['categories'] = json_encode($value);
138 } else {
139 $this->attributes['categories'] = json_encode([]);
140 }
141 }
142
143 public function getCategoriesAttribute($value)
144 {
145 return !empty($value) ? json_decode($value, true) : [];
146 }
147
148 public function setProductsAttribute($value)
149 {
150 if (is_array($value) || is_object($value)) {
151 // Convert each item to integer
152 $value = array_map('intval', $value);
153 $this->attributes['products'] = json_encode($value);
154 } else {
155 $this->attributes['products'] = json_encode([]);
156 }
157 }
158
159 public function getProductsAttribute($value)
160 {
161 return !empty($value) ? json_decode($value, true) : [];
162 }
163
164 public function getEndDate()
165 {
166 return $this->end_date;
167 }
168
169 public function getStatus()
170 {
171 return $this->status;
172 }
173
174 public function setStatus($value)
175 {
176 $this->status = $value;
177 }
178
179 public function getMeta($metaKey, $default = null)
180 {
181 $exist = Meta::query()
182 ->where('object_type', 'coupon')
183 ->where('object_id', $this->id)
184 ->where('meta_key', $metaKey)->first();
185
186 if ($exist) {
187 return $exist->meta_value;
188 }
189
190 return $default;
191 }
192
193 public function updateMeta($metaKey, $metaValue)
194 {
195 $exist = Meta::query()
196 ->where('object_type', 'coupon')
197 ->where('object_id', $this->id)
198 ->where('meta_key', $metaKey)->first();
199
200 if ($exist) {
201 $exist->meta_value = $metaValue;
202 $exist->save();
203 } else {
204 $exist = Meta::query()->create([
205 'object_id' => $this->id,
206 'object_type' => 'coupon',
207 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
208 'meta_key' => $metaKey,
209 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
210 'meta_value' => $metaValue,
211 ]);
212 }
213
214 return $exist;
215 }
216
217 public function scopeActive($query)
218 {
219 return $query->where('status', 'active')
220 ->where(function ($query) {
221 $query->whereNull('end_date')
222 ->orWhere('end_date', '=', '0000-00-00 00:00:00')
223 ->orWhere('end_date', '>', DateTime::gmtNow());
224 });
225 }
226
227 public function isRecurringDiscount()
228 {
229 return Arr::get($this->conditions, 'is_recurring', false) === 'yes';
230 }
231
232 }
233