| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Models; |
| 4 |
use FluentCart\App\Models\Concerns\CanUpdateBatch; |
| 5 |
|
| 6 |
/** |
| 7 |
* AppliedCoupon Model - DB Model for Applied Coupons table |
| 8 |
* |
| 9 |
* Database Model |
| 10 |
* |
| 11 |
* @package FluentCart\App\Models |
| 12 |
* |
| 13 |
* @version 1.0.0 |
| 14 |
*/ |
| 15 |
class AppliedCoupon extends Model |
| 16 |
{ |
| 17 |
use CanUpdateBatch; |
| 18 |
|
| 19 |
protected $table = 'fct_applied_coupons'; |
| 20 |
|
| 21 |
protected $primaryKey = 'id'; |
| 22 |
|
| 23 |
protected $guarded = ['id']; |
| 24 |
|
| 25 |
protected $fillable = [ |
| 26 |
'order_id', |
| 27 |
'coupon_id', |
| 28 |
'code', |
| 29 |
'amount', |
| 30 |
]; |
| 31 |
|
| 32 |
public function order() |
| 33 |
{ |
| 34 |
return $this->belongsTo(Order::class, 'order_id', 'id'); |
| 35 |
} |
| 36 |
|
| 37 |
public function coupon() |
| 38 |
{ |
| 39 |
return $this->belongsTo(Coupon::class, 'code', 'id'); |
| 40 |
} |
| 41 |
|
| 42 |
public function setSettingsAttribute($value) |
| 43 |
{ |
| 44 |
if (is_array($value) || is_object($value)) { |
| 45 |
$value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 46 |
} |
| 47 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 48 |
$this->attributes['meta_value'] = $value; |
| 49 |
} |
| 50 |
|
| 51 |
public function getSettingsAttribute($value) |
| 52 |
{ |
| 53 |
if (is_string($value)) { |
| 54 |
$decoded = json_decode($value, true); |
| 55 |
return $decoded ?: $value; |
| 56 |
} |
| 57 |
return $value; |
| 58 |
} |
| 59 |
|
| 60 |
public function setOtherInfoAttribute($value) |
| 61 |
{ |
| 62 |
if (is_string($value)) { |
| 63 |
$value = json_decode($value, true); |
| 64 |
} |
| 65 |
|
| 66 |
if (is_array($value) || is_object($value)) { |
| 67 |
// Ensure `buy_products` and `get_products` are arrays before mapping |
| 68 |
if (isset($value['buy_products']) && is_array($value['buy_products'])) { |
| 69 |
$value['buy_products'] = array_map('intval', $value['buy_products']); |
| 70 |
} |
| 71 |
|
| 72 |
if (isset($value['get_products']) && is_array($value['get_products'])) { |
| 73 |
$value['get_products'] = array_map('intval', $value['get_products']); |
| 74 |
} |
| 75 |
|
| 76 |
// Encode the modified object or array back to JSON |
| 77 |
$this->attributes['other_info'] = json_encode($value); |
| 78 |
} else { |
| 79 |
// Handle non-array or non-object cases |
| 80 |
$this->attributes['other_info'] = json_encode([]); |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
public function getOtherInfoAttribute($value) |
| 86 |
{ |
| 87 |
return !empty($value) ? json_decode($value, true) : []; |
| 88 |
} |
| 89 |
|
| 90 |
public function setCategoriesAttribute($value) |
| 91 |
{ |
| 92 |
if (is_array($value) || is_object($value)) { |
| 93 |
$this->attributes['categories'] = json_encode($value); |
| 94 |
} else { |
| 95 |
$this->attributes['categories'] = json_encode([]); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public function getCategoriesAttribute($value) |
| 100 |
{ |
| 101 |
return !empty($value) ? json_decode($value, true) : []; |
| 102 |
} |
| 103 |
|
| 104 |
public function setProductsAttribute($value) |
| 105 |
{ |
| 106 |
if (is_array($value) || is_object($value)) { |
| 107 |
// Convert each item to integer |
| 108 |
$value = array_map('intval', $value); |
| 109 |
$this->attributes['products'] = json_encode($value); |
| 110 |
} else { |
| 111 |
$this->attributes['products'] = json_encode([]); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
public function getProductsAttribute($value) |
| 116 |
{ |
| 117 |
return !empty($value) ? json_decode($value, true) : []; |
| 118 |
} |
| 119 |
} |
| 120 |
|