woocommerce api 用于通过变体 ID 更新变体库存

woocommerce api for variation stock update by variation id

我想使用 variation id 更新 woocommerce 变体产品库存。

我试过了,但只更新了主要 inventory Stock quantity 没有更新变体数量。这是我的代码:

$product_id = 7559;
$variance_id = 122;
$get_stocks = $woocommerce->get('products/'.$product_id);
$stock_quantity_old = $get_stocks["stock_quantity"];
$data = [
        'stock_quantity' => $stock_quantity_old - 1
    ];
$result = $woocommerce->put('products/'.$product_id, $data);    

此代码仅更新主要库存库存数量。 我想更新差异数量。

首先你需要设置manage_stock为真然后使用stock_quantity。请使用下面的代码。

$product_id = 7559;
$variance_id = 122;
$get_stocks = $woocommerce->get('products/'.$product_id);
$stock_quantity_old = $get_stocks["stock_quantity"];
$data = [
        'manage_stock' => true,
        'stock_quantity' => $stock_quantity_old - 1
    ];
$result = $woocommerce->put('products/'.$product_id.'/variations/'.$variance_id, $data);