x

Cannot Create Variant with SquareConnect

Using SquareConnect's PHP Sdk, I am trying to create a very basic variant product using their API.

`

require('connect-php-sdk-master/autoload.php');
$access_token="SECRETACCESS TOKEN";
$location_id="LOCATION ID"; //only need the one
// Configure OAuth2 access token for authorization: oauth2
SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken($access_token);

$api_instance = new SquareConnect\Api\CatalogApi();
$object_id = "OBJECTIDTHATWORKS"; // string 
$include_related_objects = true; // 

//print out the objectid.  Works perfectly!
try {
   $result = $api_instance->retrieveCatalogObject($object_id,$include_related_objects);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling CatalogApi->retrieveCatalogObject: ', $e->getMessage(), PHP_EOL;
}

//now create the variant and it will fail

    $var_api = new \SquareConnect\Api\V1ItemsApi();
    $variation = new \SquareConnect\Model\V1Variation(); // \SquareConnect\Model\V1Variation | An object containing the fields to POST for the request.  See the corresponding object definition for field details.
    $variation->setName("JERSUB");
    $variation->setSku("JERSUPPERSKU");



      try {
          $meresult = $var_api->createVariation($location_id, $object_id, $variation);
          print_r($meresult);
      } catch (Exception $e) {
          echo 'Exception when calling V1ItemsApi->createVariation: ', $e->getMessage(), PHP_EOL;
      }

`

No matter what I do I always get a 400 Bad request. Exception when calling V1ItemsApi->createVariation: [HTTP/1.1 400 Bad Request] {"type":"bad_request","message":"BadRequest"}

I have tried just passing in a blank variation object like the documentation, but it still does not work. How do I get around or diagnose the error?

2,546 Views
Message 1 of 3
Report
2 REPLIES 2

Figured it out.  I was using the old V1 api which is not supported it seems.

I found the better way of doing it. It seems like a convoluted way of doing it, but in retro-spect is a very OO approach.  You have to first make a copy of the existing catalog object.  You then need to set the types (ITEM_VARIATION)/prices/etc and ID to be something else.  Then use an upsert object to upload the copy.  Was a bit tricky but managed to figure it out.

 

 

 

2,537 Views
Message 2 of 3
Report
Alumni

Glad you were able to get everything sorted out, @devstcs! Also, thank you for sharing your solution. I'm sure it will come in handy for other Sellers running into the same issue.

Puka - She/They
Seller Community UX Designer
2,531 Views
Message 3 of 3
Report