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

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

117 lines 3.1 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\Models\Concerns\CanSearch;
7 use FluentCart\Framework\Database\Orm\Relations\HasMany;
8
9 /**
10 * Shipping Zone Model - DB Model for Shipping Zones
11 *
12 * @package FluentCart\App\Models
13 * @version 1.0.0
14 */
15 class ShippingZone extends Model
16 {
17 use CanSearch;
18
19 protected $table = 'fct_shipping_zones';
20
21 protected $appends = ['formatted_region'];
22
23 /**
24 * The attributes that are mass assignable.
25 *
26 * @var array
27 */
28 protected $fillable = [
29 'name',
30 'region',
31 'order',
32 'shipping_class_id',
33 'meta'
34 ];
35
36 /**
37 * Get the shipping methods for this zone.
38 */
39 public function methods(): HasMany
40 {
41 return $this->hasMany(ShippingMethod::class, 'zone_id', 'id')
42 ->orderBy('id', 'DESC');
43 }
44
45 public function shippingClass(): \FluentCart\Framework\Database\Orm\Relations\BelongsTo
46 {
47 return $this->belongsTo(ShippingClass::class, 'shipping_class_id', 'id');
48 }
49
50 /**
51 * Check if this zone applies to a given country.
52 *
53 * @param string $country
54 * @return bool
55 */
56 public function appliesToCountry(string $country): bool
57 {
58 if ($this->region === 'all') {
59 return true;
60 }
61
62 if ($this->region === 'selection') {
63 $meta = $this->meta;
64 $countries = isset($meta['countries']) ? $meta['countries'] : [];
65 $selectionType = isset($meta['selection_type']) ? $meta['selection_type'] : 'included';
66
67 if ($selectionType === 'excluded') {
68 return !in_array($country, $countries);
69 }
70
71 return in_array($country, $countries);
72 }
73
74 return $this->region === $country;
75 }
76
77 public function setMetaAttribute($value)
78 {
79 if ($value && is_array($value)) {
80 $this->attributes['meta'] = \json_encode($value);
81 } else {
82 $this->attributes['meta'] = '{}';
83 }
84 }
85
86 public function getMetaAttribute($value)
87 {
88 if (!$value) {
89 return [];
90 }
91 return \json_decode($value, true) ?: [];
92 }
93
94 public function getFormattedRegionAttribute()
95 {
96 if ($this->region === 'all') {
97 return __('Whole World', 'fluent-cart');
98 }
99 if ($this->region === 'selection') {
100 $meta = $this->meta;
101 $countries = $meta['countries'] ?? [];
102 $type = $meta['selection_type'] ?? 'included';
103 $count = count($countries);
104 if ($type === 'excluded') {
105 /* translators: %d is the number of excluded countries */
106 return sprintf(__('All except %d countries', 'fluent-cart'), $count);
107 }
108 /* translators: %d is the number of selected countries */
109 return sprintf(_n('%d country', '%d countries', $count, 'fluent-cart'), $count);
110 }
111 if (!empty($this->region)) {
112 return AddressHelper::getCountryNameByCode($this->region);
113 }
114 return '';
115 }
116 }
117