PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
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 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / api / OrderItems.php

OrderItems.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at api/OrderItems.php

124 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\Api;
4
5 use FluentCart\App\App;
6 use FluentCart\App\Models\OrderItem;
7 use FluentCart\Framework\Support\Arr;
8
9 class OrderItems
10 {
11 use SanitizerTrait;
12
13 public function __construct()
14 {
15 $this->rules = [
16 "order_id" => 'intval',
17 "object_id" => 'intval',
18 "object_type" => 'sanitize_text_field',
19 "quantity" => 'intval',
20 "item_name" => 'sanitize_text_field',
21 "item_price" => 'floatval',
22 "total" => 'floatval',
23 "line_total" => 'floatval'
24 ];
25 }
26
27 public function setOtherInfoAttribute($value)
28 {
29 if (is_array($value) || is_object($value)) {
30 $this->attributes['other_info'] = json_encode($value);
31 } else {
32 $this->attributes['other_info'] = $value;
33 }
34 }
35
36 public function getOtherInfoAttribute($value)
37 {
38 return !empty($value) ? json_decode($value) : null;
39 }
40
41 public function setLineMetaAttribute($value)
42 {
43 if (is_array($value) || is_object($value)) {
44 $this->attributes['line_meta'] = json_encode($value);
45 } else {
46 $this->attributes['line_meta'] = $value;
47 }
48 }
49
50 public function getLineMetaAttribute($value)
51 {
52 return !empty($value) ? json_decode($value) : null;
53 }
54
55
56 /**
57 * @param $orderId
58 * @return array
59 */
60 public function getItems($orderId)
61 {
62 return OrderItem::query()->where('order_id', $orderId)->get()->toArray();
63 }
64
65 /**
66 *
67 * @param $orderId
68 * @param $items
69 * @return array
70 * @throws \Exception
71 */
72 public function updateOrInsertOrderItems($orderId, $items): array
73 {
74
75 if (empty($items) || !is_array($items)) {
76
77 throw new \Exception(esc_html__('No items given.', 'fluent-cart'));
78 }
79
80 foreach ($items as $idx => $item) {
81
82 $item = $this->sanitize($item);
83 $item['order_id'] = $orderId;
84
85 /**
86 * object_id ::
87 * object_id:
88 *
89 */
90 $orderItem = OrderItem::query()->where('order_id', $orderId)
91 ->where('object_id', $item['object_id'])
92 ->first();
93
94 if (empty($orderItem)) {
95
96 $item['cart_index'] = $idx + 1;
97 OrderItem::create($item);
98
99 } else {
100
101 $orderItem->update($item);
102 }
103 }
104
105 return [
106 'message' => __('Order items updated.', 'fluent-cart')
107 ];
108 }
109
110 public static function getTopProductsSold($queryParams)
111 {
112 return OrderItem::search(Arr::only($queryParams, ['created_at']))
113 ->select('object_id')
114 ->selectRaw('SUM(quantity) as total_sold')
115 ->groupBy('object_id')
116 ->orderBy(sanitize_sql_orderby('total_sold'), sanitize_sql_orderby('DESC'))
117 ->having('total_sold', Arr::get($queryParams, 'operator'), Arr::get($queryParams, 'total_sold'))
118 ->with(['product' => function ($query) {
119 $query->select('ID', 'post_title');
120 }])
121 ->take(5)->get();
122 }
123 }
124