How to fixed the {variant_id: [{code: “invalid”, message: “is invalid”, options: []} in php

I am creating an app in which I have to use Checkout API. I am working in Laravel. While working with this API I am getting an error

errors: {line_items: [{variant_id: [{code: "invalid", message: "is invalid", options: []}]}]}

I have try all documents provided by shopify and answers provided by Shopify Experts and other developers through their blogs regarding this issue but it is not getting fixed. I am using following scope:

read_orders,write_products,write_script_tags,write_checkouts,read_themes,write_themes,write_checkouts,read_product_listings,unauthenticated_read_product_listings,unauthenticated_write_checkouts,unauthenticated_write_customers,unauthenticated_read_customer_tags

I have try to test it with hard coded variant’s ID but not works. I have test the product availability for online store. Nothing works. This is my store address.

https://studystore2019.myshopify.com

Please check my question and help me out. Thanks In Advance.

Ok, I have a few questions:

What does your actual query AND PHP code look like?

(ps - just a courteous reminder, don’t post any keys)

Are you sure the variant exists that you’re trying to pull?

Have you tried pulling other variants with the same code?

$newrule = (object)[
       "checkout" => (object)[
              "email" => $cEmail,
              "line_items" => [(object)[
                     "variant_id" => "31335443824743",
                     "quantity" => "1"
                ]],
             "shipping_address" => (object)[
                      "first_name" => $firstName,
                      "last_name"  => $lastName,
                      "address1"    => $address1,
                      "city"        => $city,
                      "province_code" => $province_code,
                      "country_code"  => $country_code,
                      "phone" => $phone,
                      "zip" => $zip
                     ]
                  ]    
             ];

Above is my post data that I am sending in my cURL hit. following is my cURL hit.

$access_token_url = "https://" . $query["shop"] . "/admin/api/2020-01/checkouts.json";
                $mycheckOUT = curl_init($access_token_url);
                  curl_setopt($mycheckOUT, CURLOPT_HEADER, FALSE);
                  curl_setopt($mycheckOUT, CURLOPT_ENCODING, 'gzip');
                  curl_setopt($mycheckOUT, CURLOPT_RETURNTRANSFER, TRUE);
                  curl_setopt($mycheckOUT, CURLOPT_FOLLOWLOCATION, TRUE);
                  curl_setopt($mycheckOUT, CURLOPT_MAXREDIRS, 3);
                  curl_setopt($mycheckOUT, CURLOPT_SSL_VERIFYPEER, FALSE);
                  curl_setopt($mycheckOUT, CURLOPT_USERAGENT, 'My New Shopify App v.1');
                  curl_setopt($mycheckOUT, CURLOPT_CONNECTTIMEOUT, 30);
                  curl_setopt($mycheckOUT, CURLOPT_TIMEOUT, 30);
                  curl_setopt($mycheckOUT, CURLOPT_CUSTOMREQUEST, 'POST');
                  // Setup headers
                  $request_headers[] = "X-Shopify-Access-Token: " . $shopData["access_token"];
                  $request_headers[] = 'Content-Type: application/json';
                  $request_headers[] = 'Accept-encoding: charset=utf-8';
                  $request_headers[] = 'Host:'. $query["shop"];
                  
                  curl_setopt($mycheckOUT, CURLOPT_HTTPHEADER, $request_headers);
                  $newrule1   = json_encode($newrule, true);
                  curl_setopt ($mycheckOUT, CURLOPT_POSTFIELDS, $newrule);
                  $resultrule = curl_exec($mycheckOUT);
                  $resultrulea = json_decode($resultrule,true);

Repose of above cURL is given below:

errors: {line_items: [{variant_id: [{code: "invalid", message: "is invalid", options: []}]}]}

Yes, I am trying to pull variants which are exits in my product. see the attached image:

Yes I have tried to pull other variants with same code.
Please check it and help me.

aahh … ok.

First off; I’d highly suggest you move to GraphQL. The REST API’s are going to all be phased out; and even now some of them are breaking more and more. You should move to GraphQL as soon as possible (meaning, like tomorrow … seriously)

Second: You need a bit more info in your line item. I’d assume that you’re getting that error because you’ve left out quite a bit of the information. I could be wrong - but I’m willing to bet filling that out just a bit more would do the trick. This IS a required field after all.
Let me know how it works out :slight_smile:

"line_items": [
  {
    "compare_at_price": "9.99",
    "fulfillment_service": "manual",
    "grams": "20",
    "id": 39072856,
    "line_price": "19.99",
    "price": "19.99",
    "product_id": 632910392,
    "quantity": "",
    "requires_shipping": false,
    "sku": "IPOD2008GREEN",
    "taxable": true,
    "title": "IPod Nano - 8GB",
    "variant_id": 39072856,
    "variant_title": "Green",
    "vendor": "Apple"
  }
]

Thanks for your reply,
I have fixed this error, I have moved to draft order API. it is working fine now.

1 Like

I am also facing the same issue.

1 Like

Hey I am facing same issue , can any one give Solution