Support » Plugin: Stock Dependencies for WooCommerce » Retrieving stock dependency through Woocommerce Legacy API

  • Resolved mauberni

    (@mauberni)


    Hey Kevin,

    I’m making a little software to auto update the stock, and easily add new products to my woocommerce store.

    So I was trying to retrieve the _stock_dependency field through the Woocommerce API(Legacy) but the stock dependency doesn’t appear even with this filter -> wc-api/v3/products?filter[meta]=true

    I did a little research and I found this code to add to your functions.php file to add custom fields to your product request:

    function my_custom_product_api_response( $product ) {
        $id = $product['product']['id'];
    
        // Adding your custom field:
        $product['product']['custom_field_name'] = get_post_meta( $id, 'custom_field_name', true );
    
        return $product;
    }
    
    add_filter( 'woocommerce_api_product_response', 'my_custom_product_api_response' );

    But my php is shit so I’m having some trouble figuring this out. I did it like this but as you would expect, it doesn’t work…
    By the way my products types are “variable” so that’s why I tried adding it to the variations.

    function my_custom_product_api_response( $product ) {
        $id = $product['product']['id'];
    
        // Adding your custom field:
        $product['variations']['stock_dependency'] = get_post_meta( $id, '_stock_dependency', true );
    
        return $product;
    }
    
    add_filter( 'woocommerce_api_product_response', 'my_custom_product_api_response' );

    And this is the result
    "stock_dependency": false

    So if you can point me in the right direction that would help me a lot.

    Thanks in advance.

    • This topic was modified 10 months, 3 weeks ago by mauberni.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Retrieving stock dependency through Woocommerce Legacy API’ is closed to new replies.