-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.php
281 lines (223 loc) · 11.3 KB
/
helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
require_once getcwd() . '/header.php';
use Bigcommerce\Api\Client as Bigcommerce;
if (!function_exists('transformSimpleProduct')) {
function transformSimpleProduct($product)
{
global $storeCurrency, $storeDomain;
$outOfStock = false;
if ($product->availability == "available") {
$outOfStock = true;
} else {
if ($product->inventory_tracking != "none") {
$outOfStock = ($product->inventory_level === 0
? true
: false);
}
}
$primaryImg = $product->primary_image;
$productImage = ($primaryImg->standard_url != null) ? $primaryImg->standard_url : "";
$productImageThumb = ($primaryImg->thumbnail_url != null) ? $primaryImg->thumbnail_url : "";
$formattedProduct = new \Swagger\Client\Model\Product();
$productVariant = new \Swagger\Client\Model\ProductVariants();
$productCategories = implode(",", $product->categories);
$formattedProduct->setCategories($productCategories);
$formattedProduct->setCurrency($storeCurrency);
$formattedProduct->setHandle($storeDomain . $product->custom_url);
$formattedProduct->setImageSourceUrl($productImage);
$formattedProduct->setImageThubnail($productImageThumb);
$formattedProduct->setIsDownloadable($product->type == "digital" ? true : false);
$formattedProduct->setIsVirtual($product->type == "digital" ? true : false);
$formattedProduct->setOutStock($outOfStock);
$formattedProduct->setPrice($product->price ? floatval($product->price) : 0.00);
$formattedProduct->setPriceCompare(0.00);
//$productTags = explode(",", $product['tags']);
//$formattedProduct->setProductTags($productTags);
$dateCreated = dateConvert($product->date_created);
$formattedProduct->setPublishedAt($dateCreated);
$formattedProduct->setRefId($product->id . "");
$formattedProduct->setSku($product->sku);
$formattedProduct->setSource("bigcommerce");
$formattedProduct->setTags("tags");
$formattedProduct->setTitle($product->name);
$formattedProduct->setType($product->type);
$formattedProduct->setUserId("");
$formattedProduct->setVendor("vendor");
$formattedProduct->variants = array();
$productVariant->setAttributes(array());
$productVariant->setCreated($dateCreated);
$productVariant->setImageSourceUrl($productImage);
$productVariant->setInStock(!$outOfStock);
$productVariant->setInventoryManagement($product->inventory_tracking || "");
$productVariant->setInventoryQuantity($product->inventory_level . "");
$productVariant->option1 = "";
$productVariant->setPrice($product->price ? floatval($product->price) : 0.00);
$productVariant->setPriceCompare(0.00);
$productVariant->setProductRefId($product->id . "");
$productVariant->setRefId($product->id . "");
$productVariant->setSku($product->sku . "");
$productVariant->setTaxable($product->tax_class_id);
$productVariant->setTitle($product->name);
$dateModified = dateConvert($product->date_modified);
$productVariant->setUpdated($dateModified);
$productVariant->setVisible($product->is_visible);
$productVariant->setWeight($product->weight);
array_push($formattedProduct->variants, $productVariant);
return $formattedProduct;
}
}
if (!function_exists('transformComplexProduct')) {
function transformComplexProduct($product)
{
global $storeCurrency, $storeDomain;
$outOfStock = false;
if ($product->availability == "available") {
$outOfStock = true;
} else {
if ($product->inventory_tracking != "none") {
$outOfStock = ($product->inventory_level === 0
? true
: false);
}
}
$primaryImg = $product->primary_image;
$productImage = ($primaryImg->standard_url != null) ? $primaryImg->standard_url : "";
$productImageThumb = ($primaryImg->thumbnail_url != null) ? $primaryImg->thumbnail_url : "";
$formattedProduct = new \Swagger\Client\Model\Product();
$productCategories = implode(",", $product->categories);
$formattedProduct->setCategories($productCategories);
$formattedProduct->setCurrency($storeCurrency);
$formattedProduct->setHandle($storeDomain . $product->custom_url);
$formattedProduct->setImageSourceUrl($productImage);
$formattedProduct->setImageThubnail($productImageThumb);
$formattedProduct->setIsDownloadable($product->type == "digital" ? true : false);
$formattedProduct->setIsVirtual($product->type == "digital" ? true : false);
$formattedProduct->setOutStock($outOfStock);
$formattedProduct->setPrice($product->price ? floatval($product->price) : 0.00);
$formattedProduct->setPriceCompare(0.00);
// $productTags = explode(",", $product['tags']);
// $formattedProduct->setProductTags($productTags);
$dateCreated = dateConvert($product->date_created);
$formattedProduct->setPublishedAt($dateCreated);
$formattedProduct->setRefId($product->id . "");
$formattedProduct->setSku($product->sku);
$formattedProduct->setSource("bigcommerce");
$formattedProduct->setTags("tags");
$formattedProduct->setTitle($product->name);
$formattedProduct->setType($product->type);
$formattedProduct->setUserId("");
$formattedProduct->setVendor("vendor");
$formattedProduct->variants = array();
foreach ($product->skus as $variant) {
$productVariant = new \Swagger\Client\Model\ProductVariants();
$productVariant->setAttributes(array());
$productVariant->setCreated($dateCreated);
$productVariant->setImageSourceUrl($variant->image_file ? $variant->image_file : $productImage); //todo
$productVariant->setInStock(!$outOfStock);
$productVariant->setInventoryManagement($product->inventory_tracking || "");
$productVariant->setInventoryQuantity($variant->inventory_level . "");
$productVariant->setPrice($variant->price ? floatval($variant->price) : floatval($variant->adjusted_price));
$productVariant->setPriceCompare(0.00);
$productVariant->setProductRefId($variant->product_id . "");
$productVariant->setRefId($variant->id . "");
$productVariant->setSku($variant->sku . "");
$productVariant->setTaxable($product->tax_class_id);
$productVariant->setTitle($product->name);
$dateModified = dateConvert($product->date_modified);
$productVariant->setUpdated($dateModified);
$productVariant->setVisible($product->is_visible);
$productVariant->setWeight($variant->weight ? $variant->weight : $product->weight);
array_push($formattedProduct->variants, $productVariant);
}
return $formattedProduct;
}
}
if (!function_exists('transformCustomer')) {
function transformCustomer($customer)
{
$addresses = $customer->addresses;
$areaCode = "000";
if (!isset($customer->phone) || $customer->phone == null) {
$customer->phone = "0000000000";
} else {
$customer->phone = preg_replace('/\D+/', "", $customer->phone);
if (strpos($customer->phone, "1") === 1) {
$customer->phone = ltrim($customer->phone, "1");
}
$customer->phone = ltrim($customer->phone, "+1");
$areaCode = substr($customer->phone, 0, 3);
}
$formattedCustomer = new \Swagger\Client\Model\Customer();
$dateCreated = dateConvert($customer->date_created);
$dateModified = dateConvert($customer->date_modified);
$formattedCustomer->setActivationDate($dateCreated);
$formattedCustomer->setAreaCode($areaCode);
$formattedCustomer->setAddress1($addresses[0]->street_1);
$formattedCustomer->setAddress2($addresses[0]->street_2);
$formattedCustomer->setCity($addresses[0]->city);
$formattedCustomer->setCompany($addresses[0]->company ? $addresses[0]->company : "");
$formattedCustomer->setCountry($addresses[0]->country);
$formattedCustomer->setProvince($addresses[0]->state);
$formattedCustomer->setZip($addresses[0]->zip);
$formattedCustomer->setEmail($customer->email);
$formattedCustomer->setFirstName($customer->first_name);
$formattedCustomer->setLastName($customer->last_name);
$formattedCustomer->setModifiedDate($dateModified);
$formattedCustomer->setOrdersCount(0);
$formattedCustomer->setPhone($customer->phone ? $customer->phone : "");
$formattedCustomer->setRefId($customer->id . "");
$formattedCustomer->setSignedUpAt($dateCreated);
$formattedCustomer->setTotalSpent(true ? floatval(0) : 0.00);
$formattedCustomer->setVerified(false);
return $formattedCustomer;
}
}
if (!function_exists('transformOrder')) {
function transformOrder($order)
{
$orderProducts = $order->products;
$dateCreated = dateConvert($order->date_created);
$dateModified = dateConvert($order->date_modified);
$totalShipping = $order->shipping_cost_inc_tax;
//$order['line_items'] = array_map("addProductReference", $order['line_items']);
$formattedOrder = new \Swagger\Client\Model\Order();
$formattedOrder->cart_token = "-";
$formattedOrder->contact = $order->customer_id;
$formattedOrder->setContactRefId($order->customer_id . "");
$formattedOrder->email = "email";
$formattedOrder->setFinancialStatus($order->payment_method);
$formattedOrder->integration_id = "";
$formattedOrder->setIqxOrder("");
$formattedOrder->setProcessedAt($dateCreated);
$formattedOrder->setRefId($order->id . "");
$formattedOrder->setSubtotalPrice($order->subtotal_ex_tax ? floatval($order->subtotal_ex_tax) : 0.00);
$formattedOrder->setTotalPrice($order->total_inc_tax ? floatval($order->total_inc_tax) : 0.00);
$formattedOrder->setTotalShipping($totalShipping ? floatval($totalShipping) : 0.00);
$formattedOrder->setTotalTax($order->total_tax ? floatval($order->total_tax) : 0.00);
$formattedOrder->setUserId("");
$lineItemsArray = array();
foreach ($orderProducts as $key => $lineItem) {
$orderLineItem = new \Swagger\Client\Model\OrderLineItems();
$orderLineItem->setFulfillableQuantity($lineItem->quantity ? intVal($lineItem->quantity) : 0);
$orderLineItem->setPrice($lineItem->base_price ? floatval($lineItem->base_price) : 0.00);
$orderLineItem->setOrderPrice(0.00);
$orderLineItem->setSku($lineItem->sku);
$orderLineItem->setName($lineItem->name);
$orderLineItem->setTitle($lineItem->name);
$orderLineItem->setQuantity(intVal($lineItem->quantity));
$orderLineItem->setGrams($lineItem->weight ? intVal($lineItem->weight) : 0);
$orderLineItem->setRequiresShipping(false);
$orderLineItem->setProductRefId($lineItem->product_id . "");
array_push($lineItemsArray, $orderLineItem);
}
$formattedOrder->setLineItems($lineItemsArray);
return $formattedOrder;
}
}
if (!function_exists('dateConvert')) {
function dateConvert($obj) {
$datetime = new DateTime($obj);
$dateCreated = $datetime->format(DateTime::ATOM);
return $dateCreated;
}
}