Increasing count_on_hand for products (or variants, not sure) via API

Hi there,

I’m busy developing an app which uses the OFN API to affect product counts. Nothing seems to be working, though.

I’ve tried PUTing product[count_on_hand] to a product endpoint, variant[count_on_hand] to a variant endpoint. Neither makes any difference to the count_on_hand value, but a successful result is returned (ie, no error).

Basically, I’ve tried this:

PUT /api/products/1?product[count_on_hand]=100

And this:

PUT /api/products/1/variants/1?variant[count_on_hand]=100

And endless variations of those, nothing works.

Is there anyone out there who can assist?

Marlin Forbes

Hi Marlin,

That looks like you’re starting from the right place. The one to look for is the PUT on the variant, not the product, since the product’s count_on_hand value is a cached version of that stored on the variant(s).

There’s an additional layer of complexity here because of VariantOverrides, which allow a hub to have their own stock level of a variant, independent of the stock level at the producer or at other hubs. There’s a little documentation of variant overrides at this page: Variant Overrides - Hub can override stock level and price on a variant . (@Kirsten, do you know of any more thorough documentation?)

I doubt that’s causing your immediate problem though. If I were to investigate this problem myself, I’d start by putting a binding.pry in the spree API, in Spree::Api::VariantsController#update and investigate whether authorisation was granted, if the save succeeded and so on.

Hope this helps,

Hi Rohan,

Thanks for the pointers, I’ll start digging. Was the API also changed to support that feature? Are there any specs or tests in the codebase that would help me to learn how to use that?


Ok, I’ve managed to increase stock levels of a variant, thanks for your help.

The changes I’m making are for producers, so I’m not entirely sure that the overrides would apply to me, that seems to be a Hub management function.

I see that the main product count on hand now follows the count on hand of the variant/s.

The change that made the difference was adding an attr_accessible :count_on_hand to Spree::Variant