Hello, I am also working on it. I found that the Woocommerce REST API in the metadata section, you can send the stock for each location.
Example:
meta_data:[{
“id”:3386,
“key”:_stock_at_999″,
“value”:”32″
}]
Ok thanks @wchavez, that could work. The challenge would be to identify what the number is per location _stock_at_999?
Thanks for the hint.
Hello @marekinfomasters
It’s the location ID.
I could add that data to the API, i just need some time 🙂
-
This reply was modified 6 years ago by
alexmigf.
Hi, I needed the ability to use the REST api also, as you say you can use the meta_data way to set the actual location stock level. But I also need the abilty to enable / disable the stock locations as well. So I quickly made this.
Will show “stock_location” when get products/*
Allow update stock_locations when you PUT products/*
Its a temp solutuon until Alex get somthing into the actual plugin 🙂
/*
* Include stock_location
*/
function custom_woocommerce_rest_prepare_product($response, $post) {
$post_id = is_callable(array($post, 'get_id')) ? $post->get_id() : (!empty($post->ID) ? $post->ID : null);
if (empty($response->data['stock_locations'])) {
$terms = array();
foreach (wp_get_post_terms($post_id, 'location') as $term) {
$terms[] = array(
'id' => $term->term_id,
'name' => $term->name,
'slug' => $term->slug,
);
}
$response->data['stock_locations'] = $terms;
}
return $response;
}
add_filter('woocommerce_rest_prepare_product', 'custom_woocommerce_rest_prepare_product', 10, 2); // WC 2.6.x
add_filter('woocommerce_rest_prepare_product_object', 'custom_woocommerce_rest_prepare_product', 10, 2); // WC 3.x
/*
* Update stock_location
*/
function custom_woocommerce_rest_insert_product($post, $request) {
if (isset($request['stock_locations']) && is_array($request['stock_locations']) && sizeof($request['stock_locations'])) {
$terms = array_map('absint', $request['stock_locations']);
wp_set_object_terms($post->id, $terms, 'location');
}
}
add_action('woocommerce_rest_insert_product', 'custom_woocommerce_rest_insert_product', 10, 3); // WC 2.6.x
add_action('woocommerce_rest_insert_product_object', 'custom_woocommerce_rest_insert_product', 10, 3); // WC 3.x
Hello,
The work from @shanerutter was included in the recent release. Thank you so much!