| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Models; |
| 4 |
|
| 5 |
use FluentCart\App\Models\Concerns\CanSearch; |
| 6 |
|
| 7 |
/** |
| 8 |
* Order Meta Model - DB Model for Order Meta table |
| 9 |
* |
| 10 |
* Database Model |
| 11 |
* |
| 12 |
* @package FluentCart\App\Models |
| 13 |
* |
| 14 |
* @version 1.0.0 |
| 15 |
*/ |
| 16 |
class OrderMeta extends Model |
| 17 |
{ |
| 18 |
use CanSearch; |
| 19 |
|
| 20 |
protected $table = 'fct_order_meta'; |
| 21 |
|
| 22 |
protected $fillable = [ |
| 23 |
'order_id', |
| 24 |
'meta_key', |
| 25 |
'meta_value', |
| 26 |
]; |
| 27 |
|
| 28 |
public function setMetaValueAttribute($value) |
| 29 |
{ |
| 30 |
if (is_array($value) || is_object($value)) { |
| 31 |
$value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 32 |
} |
| 33 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 34 |
$this->attributes['meta_value'] = $value; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
public function getMetaValueAttribute($value) |
| 39 |
{ |
| 40 |
if (is_string($value)) { |
| 41 |
$decoded = json_decode($value, true); |
| 42 |
return $decoded ?: $value; |
| 43 |
} |
| 44 |
|
| 45 |
return $value; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
/** |
| 50 |
* One2One: OrderTransaction belongs to one Order |
| 51 |
* |
| 52 |
* @return \FluentCart\Framework\Database\Orm\Relations\BelongsTo |
| 53 |
*/ |
| 54 |
public function order() { |
| 55 |
return $this->belongsTo( Order::class, 'order_id', 'id' ); |
| 56 |
} |
| 57 |
|
| 58 |
public function updateMeta($metaKey, $metaValue) |
| 59 |
{ |
| 60 |
$exist = OrderMeta::query()->where('order_id', $this->id) |
| 61 |
->where('meta_key', $metaKey) |
| 62 |
->first(); |
| 63 |
|
| 64 |
if ($exist) { |
| 65 |
$exist->meta_value = $metaValue; |
| 66 |
$exist->save(); |
| 67 |
} else { |
| 68 |
$exist = OrderMeta::query()->create([ |
| 69 |
'order_id' => $this->id, |
| 70 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 71 |
'meta_key' => $metaKey, |
| 72 |
//phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 73 |
'meta_value' => $metaValue |
| 74 |
]); |
| 75 |
} |
| 76 |
|
| 77 |
return $exist; |
| 78 |
} |
| 79 |
} |
| 80 |
|