我正在尝试将我的woocommerce商店产品数量同步到外部API。
我需要从供应商的网站API检索我店中的所有产品库存数量。
我总是手动同步所有库存数量,但是如果你有超过70种产品,每种产品有5-6种差异,这是一项巨大的工作,所以我决定尝试使用他们的API来让我的产品库存自动与他们同步(请注意我只需要从他们那里检索股票信息,而不是反过来。 这是他们提供的代码:
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>
我已经拥有了API密钥,用户和密码。我的猜测是我需要创建一个新的插件,将此代码的第一部分放入其中(直到它创建一个tl_api_client),然后使用一个函数来更新我的整个库存。任何人都可以帮助这最后一部分(鉴于我的第一个假设是正确的)?
提前多多感谢 Alessio的
编辑:好的,我完成了90%,但我最后一点错过了,变量股票用新的股票价值更新的部分。我现在所做的事情(并且有效):
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();
}