-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeactivateProducts.php
99 lines (75 loc) · 3.22 KB
/
deactivateProducts.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
<?php
include 'inc/config.php';
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$filename = htmlspecialchars(basename( $_FILES["fileToUpload"]["name"]));
echo "Die Datei ". $filename . " wurde hochgeladen!<br><br>";
echo '<a href="getUploadStatus.php"><button>Uploadstatus prüfen</button></a>';
$products = readProductsFromCsv($target_file);
//Only 500 products per api request
$prod = array_chunk($products, 500);
$uploadIds = "";
foreach($prod as &$value){
//If sent via "activate products" activate instead of deactivate
if(isset($_POST["activate"])){
$result = deactivateProducts($url, $accessToken, $value, true);
}
else{
$result = deactivateProducts($url, $accessToken, $value, false);
}
logMe($result);
$uploadIds .= $result["links"][0]["href"] . "\r\n";
}
} else {
echo "Fehler beim Upload...";
}
$file = 'inc/uploadId.txt';
//Write upload id to file
file_put_contents($file, $uploadIds);
}
function readProductsFromCsv($csvPath){
$products = array();
if (($handle = fopen($csvPath, "r")) !== FALSE) {
fgetcsv($handle); //Skip first line
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
array_push($products, $data[0]);
}
fclose($handle);
} else {
echo "Fehler beim Lesen der Datei...";
}
return $products;
}
function deactivateProducts($url, $accessToken, $products, $status){
$json = generateProductDeactivateJsonFromArray($products, $status);
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . '/v3/products/active-status');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$headers = array();
$headers[] = 'Authorization: Bearer ' . $accessToken;
$headers[] = 'X-Request-Timestamp: ' . getCurrentDateTimeOttoReplaced();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return json_decode($result, true);
}
function generateProductDeactivateJsonFromArray($products, $status){
$json = '{"status": [';
foreach($products as &$value){
$json .= '{"sku": "' . $value . '","active": ' . var_export($status, true) . '},';
}
//Remove last comma from json
$json = substr_replace($json, "", -1);
$json .= ']}';
return $json;
}