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 / AppliedCoupon.php

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

120 lines 3.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 use FluentCart\App\Models\Concerns\CanUpdateBatch;
5
6 /**
7 * AppliedCoupon Model - DB Model for Applied Coupons table
8 *
9 * Database Model
10 *
11 * @package FluentCart\App\Models
12 *
13 * @version 1.0.0
14 */
15 class AppliedCoupon extends Model
16 {
17 use CanUpdateBatch;
18
19 protected $table = 'fct_applied_coupons';
20
21 protected $primaryKey = 'id';
22
23 protected $guarded = ['id'];
24
25 protected $fillable = [
26 'order_id',
27 'coupon_id',
28 'code',
29 'amount',
30 ];
31
32 public function order()
33 {
34 return $this->belongsTo(Order::class, 'order_id', 'id');
35 }
36
37 public function coupon()
38 {
39 return $this->belongsTo(Coupon::class, 'code', 'id');
40 }
41
42 public function setSettingsAttribute($value)
43 {
44 if (is_array($value) || is_object($value)) {
45 $value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
46 }
47 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
48 $this->attributes['meta_value'] = $value;
49 }
50
51 public function getSettingsAttribute($value)
52 {
53 if (is_string($value)) {
54 $decoded = json_decode($value, true);
55 return $decoded ?: $value;
56 }
57 return $value;
58 }
59
60 public function setOtherInfoAttribute($value)
61 {
62 if (is_string($value)) {
63 $value = json_decode($value, true);
64 }
65
66 if (is_array($value) || is_object($value)) {
67 // Ensure `buy_products` and `get_products` are arrays before mapping
68 if (isset($value['buy_products']) && is_array($value['buy_products'])) {
69 $value['buy_products'] = array_map('intval', $value['buy_products']);
70 }
71
72 if (isset($value['get_products']) && is_array($value['get_products'])) {
73 $value['get_products'] = array_map('intval', $value['get_products']);
74 }
75
76 // Encode the modified object or array back to JSON
77 $this->attributes['other_info'] = json_encode($value);
78 } else {
79 // Handle non-array or non-object cases
80 $this->attributes['other_info'] = json_encode([]);
81 }
82
83 }
84
85 public function getOtherInfoAttribute($value)
86 {
87 return !empty($value) ? json_decode($value, true) : [];
88 }
89
90 public function setCategoriesAttribute($value)
91 {
92 if (is_array($value) || is_object($value)) {
93 $this->attributes['categories'] = json_encode($value);
94 } else {
95 $this->attributes['categories'] = json_encode([]);
96 }
97 }
98
99 public function getCategoriesAttribute($value)
100 {
101 return !empty($value) ? json_decode($value, true) : [];
102 }
103
104 public function setProductsAttribute($value)
105 {
106 if (is_array($value) || is_object($value)) {
107 // Convert each item to integer
108 $value = array_map('intval', $value);
109 $this->attributes['products'] = json_encode($value);
110 } else {
111 $this->attributes['products'] = json_encode([]);
112 }
113 }
114
115 public function getProductsAttribute($value)
116 {
117 return !empty($value) ? json_decode($value, true) : [];
118 }
119 }
120