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;
}
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
$varianturl ="https://".$api_key.":".$password."@".$shop."/admin/api/2019-07/variants/15990192209979.json"; //15990192209979 this is a variant id
print_r(getVariant($varianturl));
print_r(updateVariant($varianturl));die;
function getVariant($url) {
$headers = [];
$headers[] = "Authorization: Basic ".base64_encode($api_key.":".$password)."";
$headers[] = 'X-Shopify-Access-Token:'.$password;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
$data = curl_exec($ch);
curl_close($ch);
list($message_headers, $message_body) = preg_split("/\r\n\r\n|\n\n|\r\r/", $data, 2);
return $message_body;
}
function updateVariant($url) {
$params = [];
$params['variant'] = array(
"id"=> 15990192209979, //this is product id not variant id
"option1"=> "Not Pink",
"price"=> "99.00"
);
$headers = [];
$headers = array("Content-Type: application/json; charset=utf-8", 'Expect:');
$headers[] = "Authorization: Basic ".base64_encode($api_key.":".$password)."";
$headers[] = 'X-Shopify-Access-Token:'.$password;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//deepak
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT, 'ohShopify-php-api-client');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
$data = curl_exec($ch);
curl_close($ch);
list($message_headers, $message_body) = preg_split("/\r\n\r\n|\n\n|\r\r/", $data, 2);
return $message_body;
}
For more knowledge about private app work kindly go through this link