我想使用 REST API 删除所有变体而不传递变体 ID。我在 wooCommerce REST API 文档中没有看到任何说明。
我用另一种方法解决了这个问题。在此方法中创建的两个请求。首先,要调用产品,而不是将响应变体 ID 传递给另一个批量请求,它将删除所有变体,但仍然存在最大 100 个变体 ID 的限制。它不会在单个请求中删除超过 100 个变体。
如果有人知道更好的方法,请分享。
WooCommerce.get("products/794")
.then((response) => {
WooCommerce.post("products/"+response.data.id+"/variations/batch", response.data.variations)
.then((response) => {
console.log(response.data);
})
.catch((error) => {
console.log(error.response.data);
});
})
.catch((error) => {
console.log(error.response.data);
});
正如已经提到的,这里需要两个rest api请求。
我们需要发送一个来获取特定产品的所有变体
$response = wp_remote_get(
"https://your-store/wp-json/wc/v3/products/{$product_id}/variations",
array(
'headers' => $headers
)
);
然后我们需要获取这些变体的数组以将其发送到批处理请求:
$variations = json_decode( wp_remote_retrieve_body( $response ), true );
$variations_ids = wp_list_pluck( $variations, 'id' );
然后我们发送批量请求:
wp_remote_post(
"https://your-store/wp-json/wc/v3/products/{$product_id}/variations/batch",
array(
'headers' => $headers,
'body' => array( 'delete' => $variations_ids )
)
);
类似的,您可以在本教程中找到完整的代码https://rudrastyh.com/woocommerce/delete-all-product-variations.html