• Resolved marekinfomasters

    (@marekinfomasters)


    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

    (@wchavez)

    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

    (@marekinfomasters)

    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.

    Plugin Contributor alexmigf

    (@alexmigf)

    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.
    shanerutter

    (@shanerutter)

    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

    (@alexmigf)

    Hello,

    The work from @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.