I try to update a variants option1
. If you look at the code below: The function getVariant
will return a json of this variant, so the basic call as well as the authentication to the API works. The function updateVariant
however only returns: {"errors":{"variant":"Required parameter missing or invalid"}}
Most google results suggest to solve this error I have to set the Content-Type, which I did. But it did not change anything. What do I miss here?
I try to reproduce the call in this api reference: https://help.shopify.com/en/api/reference/products/product-variant#update-2019-07
$varianturl ="https://".$api_key.":".$password."@".$shop."/admin/api/2019-07/variants/15990192209979.json";
print_r(getVariant($varianturl));
print_r(updateVariant($varianturl));
function getVariant($url) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function updateVariant($url) {
$ch = curl_init();
$params = array(
"id"=> 15990192209979,
"option1"=> "Not Pink",
"price"=> "99.00"
);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
2
Answers
You cannot expect a GET data structure to just work as a PUT data structure. Instead, use the data from the GET to map to a PUT following the guidelines of a PUT on a variant, something the documentation clearly shows you. Simply saying option1 is equal to ‘test’ is never going to work, what is Shopify supposed to do with that? Be more specific. Provide an ID for example as a start.
As i can see you are using private app concept to fetch and update the data from Shopify .
Kindly replace your code by this code
For more knowledge about private app work kindly go through this link
https://help.shopify.com/en/api/getting-started/authentication/private-authentication#make-authenticated-requests?