PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.21
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.21
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 trunk All 48 releases
fluent-cart / app / Models / OrderItem.php

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

232 lines 5.5 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 return Helper::toDecimal($this->subtotal);
81 }
82
83 public function setOtherInfoAttribute($value)
84 {
85 if (is_array($value) || is_object($value)) {
86 $this->attributes['other_info'] = json_encode($value);
87 } else {
88 $this->attributes['other_info'] = $value;
89 }
90 }
91
92 public function getOtherInfoAttribute($value)
93 {
94 if (is_string($value)) {
95 $decoded = json_decode($value, true);
96 return is_array($decoded) ? $decoded : [];
97 }
98
99 return [];
100 }
101
102 public function setLineMetaAttribute($value)
103 {
104 if (is_array($value) || is_object($value)) {
105 $this->attributes['line_meta'] = json_encode($value);
106 } else {
107 $this->attributes['line_meta'] = [];
108 }
109 }
110
111 public function getLineMetaAttribute($value)
112 {
113 if (is_string($value)) {
114 $decoded = json_decode($value, true);
115 if ($decoded) {
116 return $decoded;
117 }
118 }
119
120 return [];
121 }
122
123 public function getFullNameAttribute($value)
124 {
125 return $this->title . ' ' . $this->post_title;
126 }
127
128 public function order()
129 {
130 return $this->belongsTo(Order::class, 'order_id', 'id');
131 }
132
133 public function product()
134
135 {
136 return $this->belongsTo(Product::class, 'post_id', 'ID');
137 }
138
139
140 public function variants()
141 {
142 return $this->belongsTo(ProductVariation::class, 'object_id', 'id');
143 }
144
145 public function product_downloads()
146 {
147 return $this->belongsTo(ProductDownload::class, 'post_id', 'post_id');
148 }
149
150 public function createItem($orderItems)
151 {
152 return $this->belongsTo(ProductVariation::class, 'variation_id', 'id');
153 }
154
155 public function processCustom($product, $orderId)
156 {
157 return (new OrderItemHelper())->processCustom($product, $orderId);
158 }
159
160 /**
161 * Get subscription payment info if available
162 *
163 * @return string
164 */
165 public function getPaymentInfoAttribute(): string
166 {
167 return $this->getSubscriptionInfo('payment');
168 }
169
170 /**
171 * Get subscription setup info if available
172 *
173 * @return string
174 */
175 public function getSetupInfoAttribute(): string
176 {
177 return $this->getSubscriptionInfo('setup');
178 }
179
180 /**
181 * Helper method to get subscription info
182 *
183 * @param string $type 'payment' or 'setup'
184 * @return string
185 */
186
187 private function getSubscriptionInfo(string $type): string
188 {
189 if ($this->payment_type !== 'subscription') {
190 return '';
191 }
192
193 $otherInfo = $this->other_info ?? [];
194 $unitPrice = $this->unit_price ?? 0;
195
196 if ($type === 'payment') {
197 return Helper::generateSubscriptionInfo($otherInfo, $unitPrice) ?? '';
198 }
199
200 if ($type === 'setup') {
201 return Helper::generateSetupFeeInfo($otherInfo) ?? '';
202 }
203
204 return '';
205 }
206
207 public function productImage(): HasOne
208 {
209 return $this->hasOne(PostMeta::class, 'post_id', 'post_id')
210 ->where('postmeta.meta_key', 'fluent-products-gallery-image');
211 }
212
213 public function variantImages(): HasOne
214 {
215 return $this->hasOne(ProductMeta::class, 'object_id', 'object_id')
216 ->where('object_type', 'product_variant_info')
217 ->where('meta_key', 'product_thumbnail');
218 }
219
220 public function getIsCustomAttribute()
221 {
222 return $this->other_info['is_custom'] ?? false;
223 }
224
225 public function getViewUrlAttribute()
226 {
227 return $this->is_custom
228 ? ($this->other_info['view_url'] ?? '')
229 : '';
230 }
231 }
232