PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.7.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.7.1
1.7.1 1.7.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 All 51 releases
fluent-cart / app / Models / ProductDetail.php

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

185 lines 4.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\Models\Concerns\CanSearch;
7 use FluentCart\App\Models\Concerns\CanUpdateBatch;
8 use FluentCart\Framework\Database\Orm\Relations\BelongsTo;
9 use FluentCart\Framework\Database\Orm\Relations\HasMany;
10 use FluentCart\Framework\Database\Orm\Relations\HasOne;
11 use FluentCart\App\Models\WpModels\PostMeta;
12 use FluentCart\Framework\Support\Arr;
13
14 /**
15 * Product Details Model - DB Model for Product Details
16 *
17 * Database Model
18 *
19 *
20 * @package FluentCart\App\Models
21 *
22 * @version 1.0.0
23 */
24 class ProductDetail extends Model
25 {
26
27 use CanSearch, CanUpdateBatch;
28
29 protected $table = 'fct_product_details';
30
31 protected $guarded = [
32 'id',
33 ];
34
35 protected $fillable = [
36 'post_id',
37 'fulfillment_type',
38 'min_price',
39 'max_price',
40 'default_variation_id',
41 'variation_type',
42 'stock_availability',
43 'other_info',
44 'default_media',
45 'manage_stock',
46 'manage_downloadable'
47 ];
48
49 /**
50 * The attributes that should be cast.
51 *
52 * @var array
53 */
54 protected $casts = [
55 'post_id' => 'integer',
56 'min_price' => 'double',
57 'max_price' => 'double',
58 ];
59
60 protected $appends = ['featured_media', 'formatted_min_price', 'formatted_max_price'];
61
62 public function setOtherInfoAttribute($value)
63 {
64 if (is_array($value) || is_object($value)) {
65 $this->attributes['other_info'] = json_encode($value);
66 } else {
67 $this->attributes['other_info'] = $value;
68 }
69 }
70
71 public function getOtherInfoAttribute($value)
72 {
73 return !empty($value) ? json_decode($value, true) : null;
74 }
75
76
77 protected function getFormattedMinPriceAttribute()
78 {
79 return Helper::toDecimal($this->min_price);
80 }
81
82 protected function getFormattedMaxPriceAttribute()
83 {
84 return Helper::toDecimal($this->max_price);
85 }
86
87
88 /**
89 * One2One: Product Details belongs to one Product
90 *
91 * @return BelongsTo
92 */
93 public function product(): BelongsTo
94 {
95 return $this->belongsTo(Product::class, 'post_id', 'ID');
96 }
97
98 /**
99 * One2One: Product Details belongs to one Gallery Image
100 * @return HasOne
101 */
102 public function galleryImage(): HasOne
103 {
104 return $this->hasOne(PostMeta::class, 'post_id', 'post_id')
105 ->where('postmeta.meta_key', 'fluent-products-gallery-image');
106 }
107
108 // First element of Gallery Image which is considered as featured image
109 public function getFeaturedMediaAttribute()
110 {
111 $meta = $this->galleryImage;
112
113 if ($meta && is_array($meta->meta_value) && !empty($meta->meta_value)) {
114 return Arr::first($meta->meta_value); // Return the first element
115 }
116
117 return null;
118 }
119
120 public function variants(): HasMany
121 {
122 return $this->hasMany(ProductVariation::class, 'post_id', 'post_id')->orderBy('serial_index', 'asc');
123 }
124
125 public function setDefaultMediaAttribute($value)
126 {
127 if (is_array($value) || is_object($value)) {
128 $this->attributes['default_media'] = json_encode($value);
129 } else {
130 $this->attributes['default_media'] = $value;
131 }
132 }
133
134 public function getDefaultMediaAttribute($value)
135 {
136 return !empty($value) ? json_decode($value, true) : null;
137 }
138
139 public function hasPriceVariation()
140 {
141 return $this->variation_type === 'simple' && $this->max_price !== $this->min_price;
142 }
143
144 public function getStockAvailability($variationId = null)
145 {
146
147 if (!$this->manage_stock) {
148 $availability = [
149 'manage_stock' => false,
150 'availability' => __('In Stock', 'fluent-cart'),
151 'class' => 'in-stock',
152 'available_quantity' => null
153 ];
154 } else if ($this->stock_availability === 'in-stock') {
155 $availability = [
156 'manage_stock' => true,
157 'availability' => __('In Stock', 'fluent-cart'),
158 'class' => 'in-stock',
159 'available_quantity' => $this->stock_availability
160 ];
161 } else {
162 $availability = [
163 'manage_stock' => true,
164 'availability' => __('Out of Stock', 'fluent-cart'),
165 'class' => 'out-of-stock',
166 'available_quantity' => $this->stock_availability
167 ];
168 }
169
170 return apply_filters('fluent_cart/product_stock_availability', $availability, [
171 'detail' => $this,
172 'variation_id' => $variationId
173 ]);
174 }
175 public function getMinPriceAttribute()
176 {
177 return $this->variants()->min('item_price');
178 }
179
180 public function getMaxPriceAttribute()
181 {
182 return $this->variants()->max('item_price');
183 }
184 }
185