PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.27
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.27
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 / OrderAddress.php

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

257 lines 6.2 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\AddressHelper;
6 use FluentCart\App\Services\Localization\LocalizationManager;
7 use FluentCart\Framework\Support\Collection;
8 use FluentCart\Framework\Support\Arr;
9
10 /**
11 * OrderItem Model - DB Model for Order Items
12 *
13 * Database Model
14 *
15 * @package FluentCart\App\Models
16 *
17 * @version 1.0.0
18 */
19 class OrderAddress extends Model
20 {
21 protected $table = 'fct_order_addresses';
22
23 /**
24 * The attributes that are mass assignable.
25 *
26 * @var array
27 */
28 protected $fillable = [
29 'id',
30 'order_id',
31 'type',
32 'name',
33 'address_1',
34 'address_2',
35 'city',
36 'state',
37 'postcode',
38 'country',
39 'meta',
40 ];
41 protected $appends = [
42 'email',
43 'first_name',
44 'last_name',
45 'full_name',
46 'formatted_address',
47 'company_name',
48 'phone',
49 'label'
50 ];
51
52 public function setMetaAttribute($value)
53 {
54 if ($value) {
55 $decoded = \json_encode($value, true);
56 if (!($decoded)) {
57 $decoded = '[]';
58 }
59 } else {
60 $decoded = '[]';
61 }
62
63 $this->attributes['meta'] = $decoded;
64 }
65
66 public function getMetaAttribute($value)
67 {
68 if (!$value) {
69 return [];
70 }
71
72 return \json_decode($value, true);
73 }
74
75 public function getFullNameAttribute(): ?string
76 {
77 return $this->name;
78 }
79
80 public function getFirstNameAttribute(): ?string
81 {
82 return explode(" ", $this->name ?? '')[0];
83 }
84
85 public function getLastNameAttribute(): ?string
86 {
87 $nameParts = explode(" ", $this->name ?? '');
88 return end($nameParts);
89 }
90
91 public function order(): \FluentCart\Framework\Database\Orm\Relations\belongsTo
92 {
93 return $this->belongsTo(Order::class, 'order_id', 'id');
94 }
95
96 public function getEmailAttribute(): ?string
97 {
98 $order = $this->order;
99 if ($order && $order->customer) {
100 return $order->customer->email ?? null;
101 }
102 return null;
103 }
104
105 public function getFormattedAddressAttribute(): array
106 {
107 return $this->getFormattedAddress();
108 }
109
110 public function getFormattedAddress($filtered = false): array
111 {
112 $address = [
113 'country' => AddressHelper::getCountryNameByCode($this->country),
114 'state' => AddressHelper::getStateNameByCode($this->state, $this->country),
115 'city' => $this->city,
116 'postcode' => $this->postcode,
117 'address_1' => $this->address_1,
118 'address_2' => $this->address_2,
119 'type' => $this->type,
120 'name' => $this->name,
121 'first_name' => $this->first_name,
122 'last_name' => $this->last_name,
123 'full_name' => $this->full_name,
124 'email' => $this->email,
125 'company_name' => $this->company_name,
126 'label' => $this->label
127 ];
128
129 if ($filtered) {
130 $address = array_filter($address);
131 }
132
133 $addressParts = [
134 trim(Arr::get($address, 'address_1') ?? ''),
135 trim(Arr::get($address, 'address_2') ?? ''),
136 trim(Arr::get($address, 'city') ?? ''),
137 trim(Arr::get($address, 'state') ?? ''),
138 trim(Arr::get($address, 'country') ?? ''),
139 ];
140
141 $addressParts = array_filter($addressParts, function ($part) {
142 return $part !== '';
143 });
144 $address['full_address'] = implode(', ', $addressParts);
145
146
147 return $address;
148 }
149
150
151 public function getAddressAsText($isHtml = false, $includeName = true, $separator = ', '): string
152 {
153 $address = $this->formatted_address;
154
155 $formatted = array_filter([
156 $includeName ? $address['name'] : '',
157 $address['address_1'],
158 $address['address_2'],
159 $address['city'],
160 $address['state'],
161 $address['postcode'],
162 $address['country']
163 ]);
164
165 return implode($separator, $formatted);
166 }
167
168 public function getFormattedDataForCheckout($prefix = 'billing_')
169 {
170 $data = [
171 '' . $prefix . 'address_id' => $this->id,
172 '' . $prefix . 'full_name' => $this->name,
173 '' . $prefix . 'address_1' => $this->address_1,
174 '' . $prefix . 'address_2' => $this->address_2,
175 '' . $prefix . 'city' => $this->city,
176 '' . $prefix . 'state' => $this->state,
177 '' . $prefix . 'phone' => $this->phone,
178 '' . $prefix . 'postcode' => $this->postcode,
179 '' . $prefix . 'country' => $this->country,
180 '' . $prefix . 'company_name' => $this->company_name,
181 ];
182
183 if ($prefix === 'billing_') {
184 unset($data['billing_full_name']);
185 }
186
187 return $data;
188 }
189
190 public function setCompanyNameAttribute($value)
191 {
192 if (!$value) {
193 return;
194 }
195
196 $meta = $this->meta;
197
198 if (!is_array($meta)) {
199 $meta = [];
200 }
201
202 Arr::set($meta, 'other_data.company_name', $value);
203
204 $this->attributes['meta'] = json_encode($meta);
205 }
206
207 public function getCompanyNameAttribute()
208 {
209 return Arr::get($this->meta, 'other_data.company_name', '');
210 }
211
212 public function setLabelAttribute($value)
213 {
214 if (!$value) {
215 return;
216 }
217
218 $meta = $this->meta;
219
220 if (!is_array($meta)) {
221 $meta = [];
222 }
223
224 Arr::set($meta, 'other_data.label', $value);
225
226 $this->attributes['meta'] = json_encode($meta);
227 }
228
229 public function getLabelAttribute()
230 {
231 return Arr::get($this->meta, 'other_data.label', '');
232 }
233
234 public function setPhoneAttribute($value)
235 {
236 if (!$value) {
237 return;
238 }
239
240 $meta = $this->meta;
241
242 if (!is_array($meta)) {
243 $meta = [];
244 }
245
246 Arr::set($meta, 'other_data.phone', $value);
247
248 $this->attributes['meta'] = json_encode($meta);
249 }
250
251 public function getPhoneAttribute()
252 {
253 return Arr::get($this->meta, 'other_data.phone', '');
254 }
255
256 }
257