user2029890
user2029890

Reputation: 2713

Updating price on Shopify via API not working PHP and curl

My products do not have any variants. I can update non-variant values but for some reason I cannot update price which is part of the one (seemingly required) variant.

 $data = array(
              'product' => array(
                                 'id' => $id,                                       
                                  'title' => $title,
                                  'body_html' => $description,                             
                                  'tags' => $tags,
                                   'variants' => array(array(
                                                       'id' => $variant_id,
                                                       'product_id' => $id,                                                      
                                                       'price' => $price))
                                      )   
                     );


        $url = "https://$api_key:[email protected]/admin/products/$id.json";
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec ($curl);       
        curl_close($curl);

        $result = json_decode($result);
        print_r($result);

The title, body and tags update fine but the price does not. I compared against the raw product json and it seems fine. What am I doing wrong?

Thanks

Upvotes: 0

Views: 1658

Answers (2)

Leathel
Leathel

Reputation: 385

I just figured this out, if you haven't already (I know I'm late to the party on this one)

I too was unable to update the price of a single item, however if I had a single variant associated with that product, I could reference that ID, and it updates the price. The only caveat is you need a variant on all your products (which is not ideal for me).

Also I see you declared two arrays inside your variant, which isn't necessary.

It looks like this

$data = array(
'product' => array(
    'id' => "Product ID Here",                                       
    'title' => $title,
    'body_html' => $description,                             
    'tags' => $tags,
    'variant'=> array(
        'id' => "Variant ID Here",
        'price' => "199.99"
        )
    )   
);

Upvotes: 0

Nguyen Duy Phong
Nguyen Duy Phong

Reputation: 124

Upvotes: 0

Related Questions