I’m trying to sync my woocommerce store products quantity to an external API.
I need all product stock quantities in my shop to be retrieved from the supplier’s website API.
I always sync all stock quantities manually but it’s a huge work when you have more than 70 products with 5-6 variations each, so I decided to try their API to always have my product stock in sync with theirs automatically (note that I only need to retrieve stock info from them, not the other way round).
This is the code they provide:
require_once("client_v1.1/tl_api_client_class.php");
ini_set("error_reporting", E_ALL | E_NOTICE);
ini_set("display_errors", "on");
header("content-type: text/html; charset=utf-8");
//all API errors will be send through an Excepion
try {
//create a tl_api_client passing the login information (key, user, password)
$tl_client = new tl_api_client(
"YOUR_API_KEY",
"YOUR_API_USER",
"YOUR_API_PASSWORD"
);
//get english language details for the product with id 1149
//null if no result
$product = $tl_client->get_details(1149, "en", "USD");
//get quantity for each color
$color_quantity = array();
foreach( $product->attributes as $attribute ){
$result = $tl_client->get_quantity(20, 1);
/*Return null if no result for this param or a GetQuantityResult object
object(
[product_id]
[attributes_id]
[quantity]
)
*/
$color_quantity[$attribute->attributes_name] = $result->quantity;
}
} catch (Exception $e) {
//catch error
echo $e->getMessage();
exit;
}
//Do something with the result
?>
<html>
<head>
<style>
.categories{
float: left;
}
table{
border-collapse: collapse;
}
table td{
border: 1px solid #333;
}
</style>
</head>
<body>
<!-- example to show info about a product -->
<div style='clear: both;'>
<div><?php echo $product->parts_code; ?> - <?php echo $product->products_price; ?> EUR</div>
<table>
<tr>
<th>
color
</th>
<th>
quantity
</th>
</tr>
<?php foreach ($color_quantity as $color => $quantity): ?>
<tr>
<td align="center">
<?php echo $color; ?>
</td>
<td align="right">
<?php echo $quantity; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
</body>
</html>
I already have and API key, a user and a password. My guess is I need to make a new plugin, put the first part of this code in it (until it creates a tl_api_client), and then use a function to update my whole stock. Could anybody help with this last part (given my first assumption is correct)?
Thanks a lot in advance
Alessio
EDIT: Ok, I got it 90% done, but I’m missing one last bit, the part where a variation’s stock is updated with the new $stock value. what I did til now (and it works):
require_once("client_v1.1/tl_api_client_class.php");
ini_set("error_reporting", E_ALL | E_NOTICE);
ini_set("display_errors", "on");
header("content-type: text/html; charset=utf-8");
add_filter( 'init', 'update_stock', 10, 1);
function update_stock( $stock ){
$args = array(
'post_type' => 'product'
);
$i=0;
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
global $woocommerce, $product, $post;
$colorvalues = get_the_terms( $product->id, 'pa_color');
$available_variations = $product->get_available_variations();
foreach ( $available_variations as $available_variation) {
$variation_id = $available_variation['variation_id'];
$variation_object = get_post($variation_id);
$parentsku = $product->get_sku();
$tlsku = (int)substr($parentsku, -4);
$varsku = get_post_meta( $variation_object->ID, '_sku', true);
$tlvarid = (int)substr($varsku, -1);
try {
//create a tl_api_client passing the login information (key, user, password)
$tl_client = new tl_api_client(
"**",
"***",
"****"
);
$stock = $tl_client->get_quantity($tlsku, $tlvarid);
} catch (Exception $e) {
//catch error
echo $e->getMessage();
exit;
}
}
$i++;
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
}