Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I've add a patch which adds a display formatter to display stock availability as message.
For a product type, select manage display and choose Show stock display as message.
In the settings form, add a text and a classname for any threshold.
See screenshot for example
Hope this patch will be commited to next release
Comment | File | Size | Author |
---|---|---|---|
#35 | stock_formatter-1859152-35.patch | 498 bytes | Albert Volkman |
#31 | stock_formater-1859152-22.patch | 5.39 KB | guy_schneerson |
#21 | stock_formater-1859152-21.patch | 5.04 KB | guy_schneerson |
#18 | stock_formater-1859152-18.patch | 5.26 KB | Albert Volkman |
#18 | interdiff.txt | 1.58 KB | Albert Volkman |
Comments
Comment #1
guy_schneerson CreditAttribution: guy_schneerson commentedThanks @MarcElbichon
Looks cool, will review and see how it fits with the other functionality available by stock but don't see any reason why it should not make it, just be patient with me as I am trying to finish of all my work in time for my end of year break.
Comment #2
guy_schneerson CreditAttribution: guy_schneerson commentedassigning to myself so I don't forget to test and commit this awesome patch
Comment #3
gillisig CreditAttribution: gillisig commentedDoes this take into account if a product has multiple variants?
So for example a product has 1 piece of Medium and 2 pieces of Large, does it count all variants or does it only work when the product has 1 variant?
Comment #4
guy_schneerson CreditAttribution: guy_schneerson commentedYou got a few issues in your patch that stop it from working:
Also all hooks should have the following comment style above them:
so
should be:
Attaching updated patch, however it is not working for me, don't get the new formater on my product display
@MarcElbichon, would be great if you can have a look at this as this would make a great additon to the module.
Comment #5
MarcElbichon CreditAttribution: MarcElbichon commentedA studpid question : Have you cleared your caches ?
Comment #6
guy_schneerson CreditAttribution: guy_schneerson commentedGood question, sure did
Comment #7
MarcElbichon CreditAttribution: MarcElbichon commentedCan you have a look in Product Type settings (not in Display product settings) ?
Comment #8
guy_schneerson CreditAttribution: guy_schneerson commented@MarcElbichon sorry its taken me so long to get back to you, yest I was looking at the product type not the product display.
I had another look and found the following line in your code
and indeed the formatter is available to the price field, I asume that suposed to be
Can you check this and update the patch if you got time
Comment #9
MarcElbichon CreditAttribution: MarcElbichon commentedSorry too for the delay !
I don't know why there is a commerce_stock_formatter_2 formatter in the patch. It wasn't in the first one !
Just remove
and this should be good.
Comment #10
guy_schneerson CreditAttribution: guy_schneerson commentedUpdated patch: cleaned up the code as I was getting some errors.
Also updated the defaults and moved them into the hook_field_formatter_info()
Thanks @MarcElbichon this will be a great addition.
Needs review testing on a clean copy
Comment #11
MarcElbichon CreditAttribution: MarcElbichon commentedLooks fine for me
Comment #12
drcelus CreditAttribution: drcelus commentedI have tested on a clean install of Commerce Kickstart with the example store.
Patched against current dev version.
Activated stock tracking for the "Bags & Cases" product type and modified the product display to show the stock using the new formatter.
I have one issue , all the products out of stock products (0 stock) have "Low stock " if defining the ranges as follows :
The out of stock logic works, so "Out of stock " appears on the default add to cart button.
Comment #13
guy_schneerson CreditAttribution: guy_schneerson commentedthanks @drcelus
I noticed you drooped the "Notice: Undefined index: classname" I was getting that on a kickstart site I tested it. Did this issue go away? how?
Will try and recreate your second issue next time I work on ti.
Comment #14
drcelus CreditAttribution: drcelus commentedEDITED ! My first code was absolutely wrong.
The offending line for me is 664 :
Removing the condition works for me.
Great addition for a great module, BTW.
Comment #15
guy_schneerson CreditAttribution: guy_schneerson commentedthanks @rcelus and thanks @MarcElbichon for this feature a great addition indeed
Comment #16
drcelus CreditAttribution: drcelus commentedTBH I deleted it because I can't reproduce it again, and thought it was a cache-clear issue after patching the file.
Comment #17
Albert Volkman CreditAttribution: Albert Volkman commentedFormatting and grammar clean-up.
Comment #18
Albert Volkman CreditAttribution: Albert Volkman commentedFound a few more.
Comment #19
guy_schneerson CreditAttribution: guy_schneerson commentedThanks Albert Volkman for the updated patch and the interdiff (makes it easier to review).
notes to self:
Comment #20
guy_schneerson CreditAttribution: guy_schneerson commentedHi drcelus thanks for finding the offending line the safest way is to replace it with an is_int() that way both 0 and numbers are allowed and empty strings are skipped.
Also fixed the empty class by adding an if else and allowing for both cases.
Will post a patch shortly
Comment #21
guy_schneerson CreditAttribution: guy_schneerson commentedHad to rebuild the #18 patch as it was broken by Code clean-up. I had a feeling this will happen but was worth it, so thanks again Albert.
Also fixed both issues 12 & 13.
I have a feeling this patch is good to go in but didn't really have time to fully test it.
Comment #22
Albert Volkman CreditAttribution: Albert Volkman commentedIs there possibly a dynamic way instead of a fixed set of 5 options? Also, the table should utilize theme_table() instead of manually creating the markup.
Comment #23
guy_schneerson CreditAttribution: guy_schneerson commentedMarc worked in the original patch so if you are still following let us know what you think.
Both are good ideas but not show stoppers in my opinien as they are back end.
However theme_table sounds more importent and easier to implement.
Albert if you are up for any of those it would be great.
Comment #24
MarcElbichon CreditAttribution: MarcElbichon commentedI've tried to add dynamically rows but it didn't work (values were not saved).
I do not remember why i didn't use theme_table but i think i had some trouble with this.
If someone knows how to do this, should be better than fixed values.
Comment #25
guy_schneerson CreditAttribution: guy_schneerson commentedthanks MarcElbichon, Albert Volkman
i am happy for the patch to go in with those two issues we can always raise two separate issues for this one its committed.
Comment #26
jamestombs CreditAttribution: jamestombs commentedI've applied patch in #21 to latest dev and don't get the new formatter.
Realised that the formatter is set for number_decimal whereas my set up (original using 1.x branch) was using number_integer.
Changing line #536 from:
to:
gets around this problem.
Comment #27
flerkmisk CreditAttribution: flerkmisk commentedHi!
I've applied patch in #21 to latest dev too, and modified the line that jamestamr mentioned.
In my case the "Out of stock" messages shown correctly, but the "In stock" messages are invisible.
Comment #28
guy_schneerson CreditAttribution: guy_schneerson commentedalso not working in views
Comment #29
MarcElbichon CreditAttribution: MarcElbichon commentedI've tried using formatter in views and it is working for me. What's the problem for you ?
Comment #30
guy_schneerson CreditAttribution: guy_schneerson commentedHi MarcElbichon didn't have quality time to test it jet and I just randomly ran into an issue and #28 is more of a note to self to look at it more, happy that its working for you, hopefully its not an issue at all.
Comment #31
guy_schneerson CreditAttribution: guy_schneerson commentedfixed #27 & #28 "In stock" not showing
The problem was with using is_int(), updated to use is_numeric()
I also moved the code to its own sub module commerce_sdf (Commerce Stock Decimal formatter).
I am going to go with this, if we find other issues its not to bad as the module can be disabled.
Comment #32
guy_schneerson CreditAttribution: guy_schneerson commentedCommitted http://drupalcode.org/project/commerce_stock.git/commit/9271489
Comment #33
guy_schneerson CreditAttribution: guy_schneerson commentedalso create a follow up issue http://drupal.org/node/1958174 for @Albert Volkman comment in #22
Comment #34
MarcElbichon CreditAttribution: MarcElbichon commentedCan you add commerce_stock as dependency ?
Many thanks
Marc
Comment #35
Albert Volkman CreditAttribution: Albert Volkman commentedDependency added.
Comment #36
guy_schneerson CreditAttribution: guy_schneerson commentedHi @Albert Volkman & @MarcElbichon
If any dependencies are needed they should probably include the commerce_ss module as it is the one creating the field to be formatted.
however while the formatter was written for formatting stock levels it is actually a decimal formatter and does not depend on any of the other commerce modules. Although unlikely this may have some use cases and no harm in not including the dependency.