Created
July 28, 2020 12:17
-
-
Save djboris88/81e05f6783d4d0d5287bb0df72b74271 to your computer and use it in GitHub Desktop.
Example for TradeGecko API which updates the stock levels for the variant.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
use Symfony\Component\HttpClient\HttpClient; | |
use Symfony\Contracts\HttpClient\ResponseInterface; | |
/** | |
* Using two Composer packages: | |
* - symfony/var-dumper | |
* - symfony/http-client | |
*/ | |
require './vendor/autoload.php'; | |
// Try to authorize access token | |
$token = ''; | |
$url = 'https://api.tradegecko.com/'; | |
$client = HttpClient::create([ | |
'auth_bearer' => $token | |
]); | |
try { | |
$product_variant_id = 75577508; | |
$stock_to_update_to = 200; | |
dump('Vanted stock: ' . $stock_to_update_to); | |
// Get product variant current stock | |
$response = $client->request('GET', "${url}variants/${product_variant_id}"); | |
$variant = maybe_json_decode($response)->variant; | |
$current_stock = intval($variant->available_stock); | |
dump('Current stock: ' . $current_stock); | |
if ($current_stock === $stock_to_update_to) { | |
dd('Current stock is the same as desired, no need to update'); | |
} | |
// Update stock | |
$difference = $stock_to_update_to - $current_stock; | |
dump('Difference equals to: ' . $difference); | |
// First create stock_adjustment object | |
$response = $client->request('POST', "${url}stock_adjustments", [ | |
'body' => [ | |
'stock_adjustment' => [ | |
'company_id' => 61642642, // Create a fake one if needed | |
'issued_at' => date('c'), | |
'billing_address_id' => 79642685, // Create a fake one if needed | |
'shipping_address_id' => 79642685, // Create a fake one if needed | |
'stock_adjustment_reason_id' => "1245613" // Create a fake one if needed | |
] | |
] | |
]); | |
$stock_adjustment_id = maybe_json_decode($response)->stock_adjustment->id; | |
// Then create stock_adjustment_line_item | |
// NOTE that you can create multiple stock_adjustment_line_items per single stock_adjustment object | |
$response = $client->request('POST', "${url}stock_adjustment_line_items", [ | |
'body' => [ | |
'stock_adjustment_line_item' => [ | |
'quantity' => $difference, | |
'variant_id' => $product_variant_id, | |
'stock_adjustment_id' => intval($stock_adjustment_id), | |
] | |
] | |
]); | |
dump(maybe_json_decode($response)->stock_adjustment_line_item); | |
// Get product variant new stock | |
$response = $client->request('GET', "${url}variants/${product_variant_id}"); | |
$variant = maybe_json_decode($response)->variant; | |
$current_stock = intval($variant->available_stock); | |
dump('New stock: ' . $current_stock); | |
} catch (Throwable $e) { | |
dd($e); | |
} | |
/** | |
* @param ResponseInterface $response | |
* @return object|null | |
*/ | |
function maybe_json_decode(ResponseInterface $response): ?object | |
{ | |
try { | |
$validResponse = $response->getContent(); | |
} catch (Throwable $e) { | |
dump($e); | |
dd(json_decode($response->getContent(false))); | |
} | |
return json_decode($validResponse) ?: null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment