| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api; |
| 4 |
|
| 5 |
use FluentCart\App\Models\OrderMeta; |
| 6 |
|
| 7 |
class OrderMetaApi |
| 8 |
{ |
| 9 |
|
| 10 |
/** |
| 11 |
* |
| 12 |
* @param $orderId |
| 13 |
* @param $val |
| 14 |
* @return bool |
| 15 |
*/ |
| 16 |
public static function updateDiscountMeta($orderId, $val): bool |
| 17 |
{ |
| 18 |
$ins = new static(); |
| 19 |
|
| 20 |
return $ins->update($orderId, 'order_discount', $val); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* |
| 26 |
* @param $orderId |
| 27 |
* @param $val |
| 28 |
* @return bool |
| 29 |
*/ |
| 30 |
public static function updateShippingMeta($orderId, $val): bool |
| 31 |
{ |
| 32 |
$ins = new static(); |
| 33 |
|
| 34 |
return $ins->update($orderId, 'order_shipping', $val); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
/** |
| 39 |
* |
| 40 |
* @param $orderId |
| 41 |
* @return mixed|string |
| 42 |
*/ |
| 43 |
public static function getDiscountMeta($orderId) |
| 44 |
{ |
| 45 |
$ins = new static(); |
| 46 |
|
| 47 |
return $ins->getMetaByKey('order_discount', $orderId); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* |
| 53 |
* @param $orderId |
| 54 |
* @return mixed|string |
| 55 |
*/ |
| 56 |
public static function getShippingMeta($orderId) |
| 57 |
{ |
| 58 |
$ins = new static(); |
| 59 |
|
| 60 |
return $ins->getMetaByKey('order_shipping', $orderId); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
/** |
| 65 |
* Alias of all |
| 66 |
* @param $orderId |
| 67 |
* @return null |
| 68 |
*/ |
| 69 |
protected function all($orderId) |
| 70 |
{ |
| 71 |
return $this->getAll($orderId); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* |
| 77 |
* @param $orderId |
| 78 |
* @param $meta_key |
| 79 |
* @param $meta_value |
| 80 |
* @return bool |
| 81 |
*/ |
| 82 |
public function update($orderId, $meta_key, $meta_value) |
| 83 |
{ |
| 84 |
$meta = new OrderMeta(); |
| 85 |
|
| 86 |
$meta_key = sanitize_text_field($meta_key); |
| 87 |
|
| 88 |
$existingMeta = $meta::where('order_id', $orderId)->where('meta_key', $meta_key)->first(); |
| 89 |
|
| 90 |
if ($existingMeta) { |
| 91 |
$existingMeta->meta_value = $meta_value; |
| 92 |
return $existingMeta->save(); |
| 93 |
} |
| 94 |
|
| 95 |
$meta->order_id = $orderId; |
| 96 |
$meta->meta_key = $meta_key; |
| 97 |
$meta->meta_value = $meta_value; |
| 98 |
|
| 99 |
return $meta->save(); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
/** |
| 104 |
* |
| 105 |
* @param $orderId |
| 106 |
* @return mixed |
| 107 |
*/ |
| 108 |
public function getAll($orderId) |
| 109 |
{ |
| 110 |
|
| 111 |
return OrderMeta::query()->where('order_id', $orderId)->get(); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
/** |
| 116 |
* |
| 117 |
* @param $key |
| 118 |
* @param $orderId |
| 119 |
* @return mixed |
| 120 |
*/ |
| 121 |
protected function getMetaByKey($key, $orderId, $def = '') |
| 122 |
{ |
| 123 |
$vl = OrderMeta::query()->where('order_id', $orderId)->where('meta_key', $key)->first(); |
| 124 |
|
| 125 |
if (empty($vl)) { |
| 126 |
|
| 127 |
return $def; |
| 128 |
} |
| 129 |
|
| 130 |
return $vl->meta_value; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
/** |
| 135 |
* |
| 136 |
* @param $method |
| 137 |
* @param $arguments |
| 138 |
* @return mixed |
| 139 |
*/ |
| 140 |
public static function __callStatic($method, $arguments) |
| 141 |
{ |
| 142 |
$ins = new static(); |
| 143 |
|
| 144 |
return call_user_func_array([$ins, $method], $arguments); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|