PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.4.0
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.4.0
1.6.6 1.6.5 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 All 49 releases
fluent-cart / app / Models / OrderItem.php

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

250 lines 5.9 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 use FluentCart\App\Helpers\Helper;
6 use FluentCart\App\Helpers\OrderItemHelper;
7 use FluentCart\App\Models\Concerns\CanSearch;
8 use FluentCart\App\Models\Concerns\CanUpdateBatch;
9 use FluentCart\App\Models\WpModels\PostMeta;
10 use FluentCart\Framework\Database\Orm\Relations\HasOne;
11
12 /**
13 * OrderItem Model - DB Model for Order Items
14 *
15 * Database Model
16 *
17 * @package FluentCart\App\Models
18 *
19 * @version 1.0.0
20 */
21 class OrderItem extends Model
22 {
23 use CanSearch, CanUpdateBatch;
24
25 protected $table = 'fct_order_items';
26
27 /**
28 * The attributes that are mass assignable.
29 *
30 * @var array
31 */
32 protected $fillable = [
33 'order_id',
34 'post_id',
35 'fulfillment_type',
36 'fulfilled_quantity',
37 'post_title',
38 'title',
39 'object_id',
40 'cart_index',
41 'quantity',
42 'unit_price',
43 'cost',
44 'subtotal',
45 'tax_amount',
46 'discount_total',
47 'refund_total',
48 'line_total',
49 'rate',
50 'other_info',
51 'line_meta',
52 'referrer',
53 'object_type',
54 'payment_type',
55 'created_at',
56 // 'shipping_charge'
57 ];
58 protected $appends = ['payment_info', 'setup_info', 'is_custom'];
59
60 protected $casts = [
61 'unit_price' => 'double',
62 'cost' => 'double',
63 'subtotal' => 'double',
64 'tax_amount' => 'double',
65 'shipping_charge' => 'double',
66 'discount_total' => 'double',
67 'line_total' => 'double',
68 'refund_total' => 'double',
69 ];
70
71 protected static function booted()
72 {
73 static::retrieved(function ($product) {
74 $product->append('formatted_total');
75 });
76 }
77
78 protected function getFormattedTotalAttribute()
79 {
80 if ($this->line_total == 0 && $this->discount_total == 0) {
81 return Helper::toDecimal($this->subtotal);
82 }
83 return Helper::toDecimal($this->line_total);
84 }
85
86 public function getCouponDiscountAttribute()
87 {
88 return (int) ($this->line_meta['coupon_discount'] ?? 0);
89 }
90
91 public function setOtherInfoAttribute($value)
92 {
93 if (is_array($value) || is_object($value)) {
94 $this->attributes['other_info'] = json_encode($value);
95 } else {
96 $this->attributes['other_info'] = $value;
97 }
98 }
99
100 public function getOtherInfoAttribute($value)
101 {
102 if (is_string($value)) {
103 $decoded = json_decode($value, true);
104 return is_array($decoded) ? $decoded : [];
105 }
106
107 return [];
108 }
109
110 public function setLineMetaAttribute($value)
111 {
112 if (is_array($value) || is_object($value)) {
113 $this->attributes['line_meta'] = json_encode($value);
114 } else {
115 $this->attributes['line_meta'] = [];
116 }
117 }
118
119 public function getLineMetaAttribute($value)
120 {
121 if (is_string($value)) {
122 $decoded = json_decode($value, true);
123 if ($decoded) {
124 return $decoded;
125 }
126 }
127
128 return [];
129 }
130
131 public function getFullNameAttribute($value)
132 {
133 return $this->title . ' ' . $this->post_title;
134 }
135
136 public function order()
137 {
138 return $this->belongsTo(Order::class, 'order_id', 'id');
139 }
140
141 public function product()
142
143 {
144 return $this->belongsTo(Product::class, 'post_id', 'ID');
145 }
146
147
148 public function variants()
149 {
150 return $this->belongsTo(ProductVariation::class, 'object_id', 'id');
151 }
152
153 public function product_downloads()
154 {
155 return $this->belongsTo(ProductDownload::class, 'post_id', 'post_id');
156 }
157
158 public function createItem($orderItems)
159 {
160 return $this->belongsTo(ProductVariation::class, 'variation_id', 'id');
161 }
162
163 public function processCustom($product, $orderId)
164 {
165 return (new OrderItemHelper())->processCustom($product, $orderId);
166 }
167
168 /**
169 * Get subscription payment info if available
170 *
171 * @return string
172 */
173 public function getPaymentInfoAttribute(): string
174 {
175 return $this->getSubscriptionInfo('payment');
176 }
177
178 /**
179 * Get subscription setup info if available
180 *
181 * @return string
182 */
183 public function getSetupInfoAttribute(): string
184 {
185 return $this->getSubscriptionInfo('setup');
186 }
187
188 /**
189 * Helper method to get subscription info
190 *
191 * @param string $type 'payment' or 'setup'
192 * @return string
193 */
194
195 private function getSubscriptionInfo(string $type): string
196 {
197 if ($this->payment_type !== 'subscription') {
198 return '';
199 }
200
201 $otherInfo = $this->other_info ?? [];
202 $unitPrice = $this->unit_price ?? 0;
203
204 if ($type === 'payment') {
205 return Helper::generateSubscriptionInfo($otherInfo, $unitPrice) ?? '';
206 }
207
208 if ($type === 'setup') {
209 return Helper::generateSetupFeeInfo($otherInfo) ?? '';
210 }
211
212 return '';
213 }
214
215 public function productImage(): HasOne
216 {
217 return $this->hasOne(PostMeta::class, 'post_id', 'post_id')
218 ->where('postmeta.meta_key', 'fluent-products-gallery-image');
219 }
220
221 public function variantImages(): HasOne
222 {
223 return $this->hasOne(ProductMeta::class, 'object_id', 'object_id')
224 ->where('object_type', 'product_variant_info')
225 ->where('meta_key', 'product_thumbnail');
226 }
227
228 public function getIsCustomAttribute()
229 {
230 return $this->other_info['is_custom'] ?? false;
231 }
232
233 public function getViewUrlAttribute()
234 {
235 return $this->is_custom
236 ? ($this->other_info['view_url'] ?? '')
237 : '';
238 }
239
240 public function getDisplayTitle()
241 {
242 if($this->post_title == $this->title) {
243 return $this->title;
244 }
245
246 return $this->post_title . ' - ' . $this->title;
247
248 }
249 }
250