I've been stuck trying to update or delete a metafield that i created on user registration via the shopify customer API.
At first i tried to update the metafield using the customer API but it told me that the metafield already existed and i cannot add it again.
Then i used the user ID and made a call to retrieve all the metafields linked to the user using the metafield API - this worked and it returned all the metafields for me.
However my issue starts when i want to either delete or update the metafield that i've created.
I've tried a lot of different variations of REST calls but nothing seems to work.
To try and delete i used:
$baseUrl = "https://".$apikey.":".$password."#".$hostname."/admin/";
......
$curl = curl_init($baseUrl.'metafields/'.$metaID.'.json');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// Make the REST call, returning the result
$response = curl_exec($curl);
var_dump($response);
This did not do anything, so i tried going a level deeper and used the following URL:
$curl = curl_init($baseUrl.'customers/'.$custID.'/metafields/'.$metaID.'.json');
This URL did not work either.
Then I decided that i might as well just try and update the metafield to the new value that it was supposed to have.
I then tried the tried the following to update the value:
$metafield = array('metafields' => array(array(
'id' => $metaID,
'value' => '3',
'value_type' => 'string',
)));
$curl_url = $baseUrl.'customers/'.$custID.'/metafields/'.$metaID.'.json';
$ch = curl_init($curl_url);
$data_string = json_encode(array('metafield'=>$metafield));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$server_output = curl_exec ($ch);
curl_close ($ch);
var_dump($server_output);
After that i tried the alternative URL:
$curl_url = $baseUrl.'metafields/'.$metaID.'.json';
Any idea where i messed up?
Part 2 works now - creating and updating a metafield
So after writing and testing different sets of code i got something to work.
$data = array('metafield' =>
array(
'key' => 'level',
'value' => '1',
'value_type' => 'string',
'namespace' => 'Wholesaler'
)
);
$curl_url = $baseUrl.'customers/'.$custID.'/metafields.json';
//$ch = curl_init($baseUrl.$sid.'.json'); //set the url
$session = curl_init($curl_url);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: POST') );
curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
if(ereg("^(https)",$curl_url)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec($session);
curl_close($session);
$json = json_decode( $response, true );
var_dump($json);
echo "<br>-------------------------------------------------<br>";
echo $curl_url;
This code allowed me to create a metafield for a user that did not have it as well as update that metafield, as long as the key field and namespace makes it unique it will create a new field, if you use a key and namespace that already exists it will update the value.
I hope this helps someone else out there.
I do this call and it works perfect everytime:
DELETE /admin/customers/123456789/metafields/987654321.json
Where my customer ID is 123456789 and the metafield I want to kill has the ID 987654321
Part one is done - Delete
After doing alot of research and trying different pieces of script the following worked for me:
$curl_url = $baseUrl.'customers/'.$custID.'/metafields/'.$metaID.'.json';
//$curl_url = $baseUrl.'metafields/'.$metaID.'.json';
$session = curl_init($curl_url);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
if(ereg("^(https)",$curl_url)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec($session);
curl_close($session);
$json = json_decode( $response, true );
var_dump($json);
I can now delete a metafield.
Related
Hello I am usind PHP curl in order to manage my inventory with Shopify.
Everthing workd fine except for inventory levels.
For example:
$url = "https://app:pwd#url.myshopify.com/admin/api/2021-10/inventory_levels/set.json";
$data_json=json_encode(
array(
"inventory_levels/set" => array(
"location_id" => $idlocation,
"inventory_item_id" => $invitem,
"available" => $qty
)
),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_json)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
I am pretty sure $idlocation, $qty and $invitem are valid real data, I verified that $invitem is an actual integer representing an id of an inventory_item_id.
The response I get (even trying with different approachs) is:
string(72) "{"errors":{"inventory_item_id":"Required parameter missing or invalid"}}"
What does it mean? And how I do pass a valid parameter?
Can't seem to find an error in my PHP code. I am trying to add a customer over Shopify API, below is my code. I tried the same URL using Postman and it worked - the user gets added when I add the same JSON string. So I know that API is enabled properly with correct permissions within Shopify.
$API_KEY = '****';
$PASS = '****';
$STORE_URL = '****';
$DATE = "2021-07";
$USER_EMAIL = 'test#test.com';
$baseUrl = 'https://'.$API_KEY.':'.$PASS.'#'.$STORE_URL.'/admin/api/'.$DATE.'/customers.json';
// post to: POST /admin/api/2021-07/customers.json
$data = array('customer' =>
array(
'first_name' => 'Promo',
'last_name' => 'User',
'email' => $USER_EMAIL,
'accepts_marketing' => 'true',
),
);
echo "test post data:";
echo json_encode($data);
$session = curl_init( $baseUrl );
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec($session);
curl_close($session);
$json = json_decode( $response, true );
echo "test return data:";
var_dump($json);
The code prints the following, returning NULL and no customer is added:
test post data:{"customer":{"first_name":"Promo","last_name":"User","email":"test#test.com","accepts_marketing":"true"}}test return data:NULL
Can anyone spot what am I doing wrong here?
Never mind, I just figured it out.
The line
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
Should be
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'POST');
I want to upload a file snippet-test.liquid onto the shopify theme assest.
this is my code :
$update_asset = json_encode(
array(
"asset" => array(
"key" => "snippets/snippet-test.liquid",
"value" => "hello"
)
)
);
$api_url = 'https://secret:secret#demo-gandharv.myshopify.com';
$url = $api_url . '/admin/themes/172870546/assests.json';
$session = curl_init();
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($session, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($session, CURLOPT_POSTFIELDS,$update_asset);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec($session);
curl_close($session);
$response = json_decode($response);
echo "<pre>";
print_r($response);
Please let me know where i am going wrong.
there is no error.but the file is not getting uploaded. anything i am doing wrong?
As we check the code, you didn't add "X-Shopify-Access-Token" in the headers. Please apply that in headers and your code must surely work then. Also check your theme id is correct as well as it should be a published theme id.
Thanks
I'm trying to make an CURL PUT REQUEST, my code looks like:
public function assignProfile($token, $organizationId, $profileId)
{
//define enviroment and path
$host = enviroment;
$path = "/admin/organizations/".$organizationId."/users";
$data_string = '["'.$profileId.'"]';
// set up the curl resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host.$path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer '.$token.'',
'Content-Length: ' . strlen($data_string)
));
echo "<br>OK<br>";
// execute the request
$output = curl_exec($ch);
// return ID for a new case
$output = json_decode($output);
var_dump($output);
}
Each part looks correct, when I var_dump $path, $host, even $data_string looks correct. However var_dump() at the end throw just NULL
I expect I'm doing something wrong or missing something really important.
May I ask you for some advise?
Thanks
EDIT:
What i do with it:
// define
define("Audavin","here is some uniqe ID");
.
.
.
$Users = new Users;
// this return Auth token ( I verify this work with echo )
$token = $Users->authorization();
// Calling method mentioned above
$Users->assignProfile($token,"here is org id", Audavin);
I would start by making sure the URL you're making the request to actually works and returns a valid response, you can do so by using a simple REST client (like POSTMAN chrome extension for example)
If you do get a response back, try and see if it's indeed a valid JSON, if not, that could be why you're not getting anything back from json_decode (more on return values here: http://php.net/manual/en/function.json-decode.php)
Finally, It is suggested you add curl_close($ch) to the end of your code to make sure your release the curl handle.
I'm developing a web application for my co-workers, so they'll have an iPad with an application, to package our orders.
Whole app is almost finished, one of the last steps is to change order status from processing to completed.
What I'm doing:
Get current order ID, then change status with curl or API. What happens is very strange, I get JSON data back, and the completed_at time updated, but status is still processing.
Below you will see my code:
$data = array("status" => "completed");
$data_string = json_encode($data);
$username = 'XXX'; // Add your own Consumer Key here
$password = 'XXX'; // Add your own Consumer Secret here
$ch = curl_init('https://www.deallerleukste.nl/wc-api/v2/orders/5764?consumer_key='.$username.'&consumer_secret='.$password);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
echo $result;
Anyone who sees what I'm doing wrong? Please help!
Regards,
Jelmer
Try Using this code
$data='{ "order": { "status": "completed" }}';
or
Dynamically
$data=json_encode( array( 'order' => array( 'status' => $status)));
in $status you can pass the status u want.
I see it is a quite old post, but today I ran into the same problem, so maybe it is useful.
You have to use PUT instead of POST here:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
and data should be a valid JSON in this format:
$data='{"status": "completed"}';