-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIT-product-variants.php
128 lines (115 loc) · 5.15 KB
/
IT-product-variants.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
<?php
require 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$accessToken = $_ENV['ACCESS_TOKEN'] ?? null;
$apiPath = $_ENV['API_PATH'] ?? null;
$clientId = $_ENV['CLIENT_ID'] ?? null;
$clientSecret = $_ENV['CLIENT_SECRET'] ?? null;
// This chunk is to run a single product ID
$productId = 8832;
$productOptions = GetJson($apiPath . 'catalog/products/' . $productId . '/options');
$optionsToRemove = [];
foreach ($productOptions->data as $productOption) {
$optionIds = [];
foreach ($productOption->option_values as $optionValue) {
$optionIds[$optionValue->id] = ['label' => $optionValue->label, 'option_label' => $productOption->display_name];
}
$optionsToRemove[$productOption->id] = $optionIds;
}
$variants = GetJson($apiPath . 'catalog/products/' . $productId . '/variants');
foreach ($variants->data as $variant) {
$selectedOptions = $variant->option_values;
foreach ($selectedOptions as $selectedOption) {
if (isset($optionsToRemove[$selectedOption->option_id])) {
if (array_key_exists($selectedOption->id, $optionsToRemove[$selectedOption->option_id])) {
unset($optionsToRemove[$selectedOption->option_id][$selectedOption->id]);
}
}
}
}
foreach ($optionsToRemove as $index => $optionToRemove) {
$url = $apiPath . 'catalog/products/' . $productId . '/options/' . $index;
foreach ($optionToRemove as $option) {
echo 'Removing value ' . $option['label'] . ' for option ' . $option['option_label'] . PHP_EOL;
//DeleteRequest($url . '/values/' . $optionId);
}
}
// This chunk is to run for all products on the store
//$productResponse = GetJson($apiPath . 'catalog/products');
//$pages = $productResponse->meta->pagination->total_pages ?? 0;
//echo 'There are ' . $pages . ' pages of products' . PHP_EOL;
//$processedPages = 1;
//while ($processedPages <= $pages) {
// echo PHP_EOL;
// echo '**************' . PHP_EOL;
// echo 'Processing page ' . $processedPages . PHP_EOL;
// echo '**************';
// echo PHP_EOL . PHP_EOL;
// $productResponse = GetJson($apiPath . 'catalog/products?page=' . $processedPages);
// $products = $productResponse->data;
// foreach ($products as $product) {
// echo 'Processing product ' . $product->sku . PHP_EOL;
// $productId = $product->id;
// $productOptions = GetJson($apiPath . 'catalog/products/' . $productId . '/options');
// $optionsToRemove = [];
// foreach ($productOptions->data as $productOption) {
// $optionIds = [];
// foreach ($productOption->option_values as $optionValue) {
// $optionIds[] = ['id' => $optionValue->id, 'label' => $optionValue->label, 'option_label' => $productOption->display_name];
// }
// $optionsToRemove[$productOption->id] = $optionIds;
// }
// $variants = GetJson($apiPath . 'catalog/products/' . $productId . '/variants');
// foreach ($variants->data as $variant) {
// $selectedOptions = $variant->option_values;
// foreach ($selectedOptions as $selectedOption) {
// if (isset($optionsToRemove[$selectedOption->option_id])) {
// $pos = array_search($selectedOption->id, $optionsToRemove[$selectedOption->option_id]);
// if ($pos) {
// unset($optionsToRemove[$selectedOption->option_id][$pos]);
// }
// }
// }
// }
// foreach ($optionsToRemove as $index => $optionToRemove) {
// $url = $apiPath . 'catalog/products/' . $productId . '/options/' . $index;
// foreach ($optionToRemove as $option) {
// echo 'Removing value ' . $option['label'] . ' for option ' . $option['option_label'] . PHP_EOL;
// DeleteRequest($url . '/values/' . $option['id']);
// }
// }
// echo PHP_EOL;
// }
// $processedPages++;
//}
function GetJson($api_url)
{
global $accessToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Length: 0'));
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['X-Auth-Token: ' . $accessToken]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
return json_decode($response);
}
function DeleteRequest($api_url)
{
global $accessToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Length: 0'));
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['X-Auth-Token: ' . $accessToken]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
echo 'Sending DELETE request to ' . $api_url . PHP_EOL;
curl_exec($ch);
}