Title: Rest api
Last modified: May 20, 2020

---

# Rest api

 *  Resolved [marekinfomasters](https://wordpress.org/support/users/marekinfomasters/)
 * (@marekinfomasters)
 * [6 years ago](https://wordpress.org/support/topic/rest-api-84/)
 * Hi Alex
 * You wouldn’t perhaps consider enhancing your plugin with a restAPI that I could
   use defined endpoints? I’ve dev a bespoke integration in c# using the woocommerce.
   Net restAPI for our Erp system that allows me to utilize the woocommerce endpoints
   for products and categories and attributes etc. I’d really like to tap into your
   plugin to assign store locations and stock on hand per product per location.
 * Regards Marek

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [wchavez](https://wordpress.org/support/users/wchavez/)
 * (@wchavez)
 * [6 years ago](https://wordpress.org/support/topic/rest-api-84/#post-12871641)
 * 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″ }]
 *  Thread Starter [marekinfomasters](https://wordpress.org/support/users/marekinfomasters/)
 * (@marekinfomasters)
 * [6 years ago](https://wordpress.org/support/topic/rest-api-84/#post-12871714)
 * Ok thanks [@wchavez](https://wordpress.org/support/users/wchavez/), that could
   work. The challenge would be to identify what the number is per location _stock_at_**
   999**?
 * Thanks for the hint.
 *  Plugin Contributor [alexmigf](https://wordpress.org/support/users/alexmigf/)
 * (@alexmigf)
 * [6 years ago](https://wordpress.org/support/topic/rest-api-84/#post-12871857)
 * Hello [@marekinfomasters](https://wordpress.org/support/users/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](https://wordpress.org/support/users/alexmigf/).
 *  [shanerutter](https://wordpress.org/support/users/shanerutter/)
 * (@shanerutter)
 * [6 years ago](https://wordpress.org/support/topic/rest-api-84/#post-12884018)
 * 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
       ```
   
 *  Plugin Contributor [alexmigf](https://wordpress.org/support/users/alexmigf/)
 * (@alexmigf)
 * [6 years ago](https://wordpress.org/support/topic/rest-api-84/#post-12890132)
 * Hello,
 * The work from [@shanerutter](https://wordpress.org/support/users/shanerutter/)
   was included in the recent release. Thank you so much!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Rest api’ is closed to new replies.

 * ![](https://ps.w.org/stock-locations-for-woocommerce/assets/icon.svg?rev=2471943)
 * [Stock Locations for WooCommerce](https://wordpress.org/plugins/stock-locations-for-woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/stock-locations-for-woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/stock-locations-for-woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/stock-locations-for-woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/stock-locations-for-woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/stock-locations-for-woocommerce/reviews/)

## Tags

 * [restapi](https://wordpress.org/support/topic-tag/restapi/)

 * 5 replies
 * 4 participants
 * Last reply from: [alexmigf](https://wordpress.org/support/users/alexmigf/)
 * Last activity: [6 years ago](https://wordpress.org/support/topic/rest-api-84/#post-12890132)
 * Status: resolved