PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.23
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.23
1.6.5 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 All 48 releases
fluent-cart / app / Helpers / OrderItemHelper.php

OrderItemHelper.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.23, at app/Helpers/OrderItemHelper.php

137 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Helpers;
4
5 use FluentCart\App\Models\OrderItem;
6 use FluentCart\Framework\Support\Arr;
7
8 class OrderItemHelper
9 {
10 private $orderId = null;
11
12 /**
13 * @return array
14 * sanitized order_item
15 */
16 public function sanitize($orderItem)
17 {
18 $rules = [
19 "order_id" => 'intval',
20 "post_id" => 'intval',
21 "variation_type" => 'sanitize_text_field',
22 "quantity" => 'intval',
23 "item_name" => 'sanitize_text_field',
24 "item_price" => 'floatval',
25 "item_total" => 'floatval',
26 "line_total" => 'floatval'
27 ];
28
29 foreach ($rules as $key => $value) {
30 if (isset($orderItem[$key])) {
31 $orderItem[$key] = call_user_func($value, $orderItem[$key]);
32 }
33 }
34
35 return $orderItem;
36 }
37
38 public function mapOrderItem($product, $quantity = 0)
39 {
40 $total = floatval(Arr::get($product, 'detail.item_price') * $quantity);
41 $detail = Arr::get($product, 'detail');
42
43 return array(
44 'order_id' => intval($this->orderId),
45 'post_id' => intval(Arr::get($product, 'ID')),
46 'fulfillment_type' => Arr::get($detail, 'item_type'),
47 'quantity' => intval($quantity),
48 'item_name' => Arr::get($product, 'post_title'),
49 'item_price' => floatval(Arr::get($detail, 'item_price')),
50 'tax_amount' => 0,
51 'discount_total' => 0,
52 'item_total' => $total,
53 'line_total' => $total,
54 );
55 }
56
57 public function saveOrderItems($products)
58 {
59 foreach ($products as $product) {
60 $product = $this->sanitize($product);
61
62 $id = Arr::get($product, 'id', false);
63 $orderItem = OrderItem::find($id);
64
65 if (empty($orderItem)) {
66 OrderItem::create($product);
67 } else {
68 $orderItem->update($product);
69 }
70 }
71
72 return [
73 'message' => __('Product Updates!', 'fluent-cart')
74 ];
75 }
76
77 /**
78 * @return array
79 * processed orderItem from product
80 */
81 public function processProducts($orderId, $items)
82 {
83 if (empty($items)) {
84 throw new \Exception(esc_html__('Please add some products!', 'fluent-cart'));
85 }
86
87 if (!$orderId) {
88 throw new \Exception(esc_html__('Order Not valid!', 'fluent-cart'));
89 }
90
91 $this->orderId = $orderId;
92 $processedData = [];
93
94 foreach ($items as $item) {
95 if (isset($item['ID'])) {
96 $data = $this->mapOrderItem($item);
97 array_push($processedData, $data);
98 }
99 };
100
101 return $processedData;
102 }
103
104
105 public function processCustom($product, $orderId)
106 {
107 $price = Arr::get($product, 'item_price', false);
108 $quantity = Arr::get($product, 'quantity', false);
109
110 if (!Arr::get($product, 'item_name', false)) {
111 throw new \Exception(esc_html__('Item must have a name!', 'fluent-cart'));
112 }
113
114 if (!$price || !$quantity) {
115 throw new \Exception(esc_html__('Price, Quantity field should not be empty or zero!', 'fluent-cart'));
116 }
117
118 //$total = floatVal($price * 100 * $quantity);
119 $total = floatVal($price * $quantity);
120
121 $type = Arr::get($product, 'fulfillment_type', 'physical');
122
123 $otherData = [
124 'order_id' => $orderId,
125 'variation_type' => $type,
126 //'item_price' => $price * 100,
127 'item_price' => $price,
128 'item_total' => $total,
129 'line_total' => $total,
130 'tax_amount' => 0,
131 'discount_total' => 0,
132 ];
133
134 return $this->sanitize(array_merge($product, $otherData));
135 }
136 }
137