Hi, we are making a store with products created via Feeds from an XML from our dealer. So in product creation, the stock field is filled in and updated once a day via Feeds, too.
The problem is that we want to have a threshold margin from our dealer. To explain better, say we have a product with 6 units in stock (which is our dealer's stock) but we want to be safe and only sell 4 units so we have a threshold of 2. This number is fixed, so a product with 12 units in our dealer should only sell 10 units in our store, and so on.
We have managed to achieve this in our full node add to cart form. We have altered the form to include a new validation function with our calculations (we are using the threshold as a constant in our module). In order to avoid duplicated messages we have removed the commerce_stock_add_to_cart_validate function from $form['#validate'].
This way we end up with our custom validation function, which is ok. Problem is that we have to do this for all forms validated in Commerce Stock (all places where commerce_stock_product_check_out_of_stock() function is called) so here we are asking for a solution to handle this globally. Maybe a simple field in the module configuration where we can insert an integer (or leave it blank or 0). If this field is filled in, then it should count in the calculation.
Thanks in advance.
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | commerce_stock-threshold_field-1272648-13.patch | 4.22 KB | andyf |
Comments
Comment #1
rfayPlease take a look at the strategy in http://www.commerceguys.com/resources/articles/216 and see if it meets your needs.
Comment #2
EndEd commentedThis is what I came up with (sorry I don't know how to roll a patch yet):
In commerce_stock/includes/commerce_stock.admin.inc we can add the following to commerce_stock_admin_form() function...
To validate the textfield as a number we modify also commerce_stock_admin_form_validate() function...
Then we can modify commerce_stock_product_check_out_of_stock() function in commerce_stock/commerce_stock.module...
This is not fully tested (for starts we can't seem to submit the configuration form correctly. After submit it always sets the value of the threshold textfield to 0), but it should work and is rather easy to implement.
Comment #3
EndEd commentedSorry, didn't read your answer before my previous comment. I will take a look, thanks :)
EDIT: After viewing the video... well, it's not the same functionality I was looking for, but gave me some ideas about Rules I will try tomorrow.
What we needed is a global parameter that stops the user from buying more products than ($stock - $threshold). That includes, validation on various forms, rules integration (the commerce_stock_product_check_out_of_stock() function is called by commerce_stock_order_has_out_of_stock() which is called in commerce_stock.rules.inc) and making the add to cart button disabled if it does not meet the requierements. Will try some other ideas. Thanks again!
Comment #4
rfay@EndEd Please do learn how to roll a patch, as it will make you more effective as a contributor. People don't tend to read long code posts, but patches can be applied and experimented with (and possibly committed). Making a Drupal patch with git.
Comment #5
rfayI should note that one way to go forward here is to provide a (set of) stock threshold rule, perhaps disabled, that could ship with this module, perhaps with a minimal UI. In general, I like keeping the functionality in rules even when the UI is not the rules UI.
Comment #6
guy_schneerson commented@EndEd, Our client had a similar requirement and we ended up agreeing to keep the commerce stock level x number under their external stock system.
The functionality you want is likely to be easier to develop as a sub module for the planed version 2, see roadmap http://drupal.org/node/1269168 however that’s not likely to be ready for sum time.
The problem in implementing this is that the business logic may be different for each client i.e. is the threshold is global by product type or for each product?, and as more people start using the stock module they will come up with new requirements.
I think that this should be possible to do this using rules, you can probably create a rule to set stock to zero if it goes under the threshold this will make the stock validation kick in,
Optionally (if you can get away with it) remoe the threshold on import so a stock level of 6 will be imported as 4.
Comment #7
EndEd commentedThanks for the tips!
I think the easiest way for now is doing this in the product import/update using feed tamper (php plugin) and removing the threshold in the process. As I said our thrshold is a constant in a module so it should be no problem doing this :)
Comment #8
guy_schneerson commentedmarking it as fixed but added the requirement to the raodmap http://drupal.org/node/1269168
Comment #9
carn1x commentedMight be a bit late and probably too broad, but I wrote a small plugin for Feeds Tamper which provides mathematical calculations for imported values: http://drupal.org/node/1271700#comment-5006800
Comment #10
guy_schneerson commentedthanks @carn1x,
never to late to contribute, if you turn it into a sandbox project would be easier to find & use.
Comment #11
rfay@carn1x +1 for a sandbox!
Comment #12
carn1x commentedI was hoping the maintainer of Feeds Tamper might incorporate it! I spend every waking hour working, but I'd love to contribute more if I could find the time :)
Comment #14
andyf commentedIn case anyone's interested, here's a rough and ready patch for 1.1 that allows you to have a threshold field attached to the product (though it'd be easy to modify it for a global threshold as well). There's no UI - you need to set the variable commerce_stock_threshold_field to the name of the threshold field.
Comment #15
deggertsen commented@AndyF. Is there a reason you have it set to be out of stock at the threshold? It seems to me that it shouldn't be out of stock until it hits 0 in all cases. Otherwise looks like a very helpful patch. Thanks.
Comment #15.0
deggertsen commentedbetter explanation