| 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 attrMap(): HasMany |
| 126 |
{ |
| 127 |
return $this->hasMany(AttributeRelation::class, 'object_id', 'id'); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
public static function boot() |
| 132 |
{ |
| 133 |
parent::boot(); |
| 134 |
static::deleting(function ($model) { |
| 135 |
$model->attrMap()->delete(); |
| 136 |
}); |
| 137 |
} |
| 138 |
|
| 139 |
public function setDefaultMediaAttribute($value) |
| 140 |
{ |
| 141 |
if (is_array($value) || is_object($value)) { |
| 142 |
$this->attributes['default_media'] = json_encode($value); |
| 143 |
} else { |
| 144 |
$this->attributes['default_media'] = $value; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
public function getDefaultMediaAttribute($value) |
| 149 |
{ |
| 150 |
return !empty($value) ? json_decode($value, true) : null; |
| 151 |
} |
| 152 |
|
| 153 |
public function hasPriceVariation() |
| 154 |
{ |
| 155 |
return $this->variation_type === 'simple' && $this->max_price !== $this->min_price; |
| 156 |
} |
| 157 |
|
| 158 |
public function getStockAvailability($variationId = null) |
| 159 |
{ |
| 160 |
|
| 161 |
if (!$this->manage_stock) { |
| 162 |
$availability = [ |
| 163 |
'manage_stock' => false, |
| 164 |
'availability' => __('In Stock', 'fluent-cart'), |
| 165 |
'class' => 'in-stock', |
| 166 |
'available_quantity' => null |
| 167 |
]; |
| 168 |
} else if ($this->stock_availability === 'in-stock') { |
| 169 |
$availability = [ |
| 170 |
'manage_stock' => true, |
| 171 |
'availability' => __('In Stock', 'fluent-cart'), |
| 172 |
'class' => 'in-stock', |
| 173 |
'available_quantity' => $this->stock_availability |
| 174 |
]; |
| 175 |
} else { |
| 176 |
$availability = [ |
| 177 |
'manage_stock' => true, |
| 178 |
'availability' => __('Out of Stock', 'fluent-cart'), |
| 179 |
'class' => 'out-of-stock', |
| 180 |
'available_quantity' => $this->stock_availability |
| 181 |
]; |
| 182 |
} |
| 183 |
|
| 184 |
return apply_filters('fluent_cart/product_stock_availability', $availability, [ |
| 185 |
'detail' => $this, |
| 186 |
'variation_id' => $variationId |
| 187 |
]); |
| 188 |
} |
| 189 |
public function getMinPriceAttribute() |
| 190 |
{ |
| 191 |
return $this->variants()->min('item_price'); |
| 192 |
} |
| 193 |
|
| 194 |
public function getMaxPriceAttribute() |
| 195 |
{ |
| 196 |
return $this->variants()->max('item_price'); |
| 197 |
} |
| 198 |
} |
| 199 |
|