-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_customer.php
122 lines (103 loc) · 2.85 KB
/
create_customer.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
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$logfile = new Logger('create_customer_log');
$logfile->pushHandler(new StreamHandler('customer_import.log', Logger::INFO));
$accessToken = 'Enter-access-token-here';
$shopUrl = 'https://dev-store.myshopify.com/admin/api/2021-07/graphql.json';
$client = new Client([
'base_uri' => $shopUrl,
'headers' => [
'Content-Type' => 'application/json',
'X-Shopify-Access-Token' => $accessToken,
],
]);
$query = <<<QUERY
mutation customerCreate(\$input: CustomerInput!) {
customerCreate(input: \$input) {
userErrors {
field
message
}
customer {
id
email
phone
taxExempt
acceptsMarketing
firstName
lastName
addresses {
address1
city
province
country
phone
zip
lastName
firstName
}
}
}
}
QUERY;
$variables = [
'input' => [
'email' => 'steve@example.com',
'phone' => '+16465555559',
'firstName' => 'Steve',
'lastName' => 'Lastname',
'acceptsMarketing' => true,
"addresses" => [
[
"address1"=>"412 fake st",
"city"=>"Ottawa",
"province"=>"ON",
"country"=>"CA",
"phone"=>"+16465555559",
"zip"=>"A1A 4A1",
"lastName"=>"Lastname",
"firstName"=>"Steve"
]
],
]
];
$requestData = [
'json' => [
'query' => $query,
'variables' => $variables,
],
];
try {
$response = $client->request('POST', '', $requestData);
$body = $response->getBody();
$data = json_decode($body, true);
$email = $variables['input']['email'];
if (isset($data['errors'])) {
$error_message = "";
$errors = $data['errors'];
foreach($errors as $error){
$error_message .= $error['message'];
}
$logfile->error("error for $email: $error_message \n");
} else {
$field_errors = $data['data']['customerCreate']['userErrors'];
if(count($field_errors)>0){
$field_error_message = "";
foreach($field_errors as $error){
$field_error_message .= $error['message'];
}
$logfile->error("error for $email: $field_error_message \n");
echo "<div style='color:red;'>error for $email: ".$field_error_message. "</div>\n";
}else{
$id = $data['data']['customerCreate']['customer']['id'];
$logfile->info("Customer created successfully $email: $id");
echo $success_message = "<div style='color:green;'>Customer created: ".$id. "</div>\n";
}
}
} catch (\Exception $e) {
$statusCode = $e->getResponse()->getStatusCode();
echo $errorMessage = $e->getMessage();
}