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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

guy_schneerson’s picture

Status: Active » Needs review

Thanks @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.

guy_schneerson’s picture

Assigned: Unassigned » guy_schneerson

assigning to myself so I don't forget to test and commit this awesome patch

gillisig’s picture

Does 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?

guy_schneerson’s picture

Assigned: guy_schneerson » Unassigned
Status: Needs review » Needs work
FileSize
5.05 KB

You got a few issues in your patch that stop it from working:

  1. white space at end of line
  2. you are not working from a git repo and as a result your file paths are reletive to your machine:
    +++ C:/applications/web/wamp/www/commerce/sites/all/modules/commerce_stock/commerce_stock.module

Also all hooks should have the following comment style above them:
so

<?php
// hook for displaying format for our field
// hook_field_formatter_info
?>

should be:

<?php
/**
 * Implaments hook_field_formatter_info().
 */

?>

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.

MarcElbichon’s picture

A studpid question : Have you cleared your caches ?

guy_schneerson’s picture

Good question, sure did

MarcElbichon’s picture

Can you have a look in Product Type settings (not in Display product settings) ?

guy_schneerson’s picture

@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

      'field types' => array('commerce_price'),

and indeed the formatter is available to the price field, I asume that suposed to be

      'field types' => array('commerce_stock'),

Can you check this and update the patch if you got time

MarcElbichon’s picture

Sorry 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

    'commerce_stock_formatter_2' => array(
      'label' => t('Formatted Stock level'),
      'field types' => array('commerce_price'),
      'settings' => array(
        'calculation' => FALSE,
      ),

and this should be good.

guy_schneerson’s picture

Status: Needs work » Needs review
FileSize
5.36 KB

Updated 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

MarcElbichon’s picture

Looks fine for me

drcelus’s picture

I 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 :

  • <= 0 Out of stock
  • <= 50 Low stock, this is the classname that it is being applied to all 0 stock products
  • <= 999999 In stock

The out of stock logic works, so "Out of stock " appears on the default add to cart button.

guy_schneerson’s picture

thanks @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.

drcelus’s picture

EDITED ! My first code was absolutely wrong.

The offending line for me is 664 :

    if ($seuil != "") {
     //....
    }

Removing the condition works for me.

Great addition for a great module, BTW.

guy_schneerson’s picture

thanks @rcelus and thanks @MarcElbichon for this feature a great addition indeed

drcelus’s picture

TBH I deleted it because I can't reproduce it again, and thought it was a cache-clear issue after patching the file.

Albert Volkman’s picture

Formatting and grammar clean-up.

Albert Volkman’s picture

Found a few more.

guy_schneerson’s picture

Status: Needs review » Needs work

Thanks Albert Volkman for the updated patch and the interdiff (makes it easier to review).
notes to self:

  • Still need to finish testing see #12 & #13
  • and look at #14 for a fix
guy_schneerson’s picture

Hi 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

guy_schneerson’s picture

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

Albert Volkman’s picture

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

guy_schneerson’s picture

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

MarcElbichon’s picture

I'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.

guy_schneerson’s picture

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

jamestombs’s picture

I'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:

'field types' => array('number_decimal'),

to:

'field types' => array('number_decimal', 'number_integer'),

gets around this problem.

flerkmisk’s picture

Hi!

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.

guy_schneerson’s picture

also not working in views

MarcElbichon’s picture

I've tried using formatter in views and it is working for me. What's the problem for you ?

guy_schneerson’s picture

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

guy_schneerson’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
5.39 KB

fixed #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.

guy_schneerson’s picture

Status: Reviewed & tested by the community » Fixed
guy_schneerson’s picture

also create a follow up issue http://drupal.org/node/1958174 for @Albert Volkman comment in #22

MarcElbichon’s picture

Can you add commerce_stock as dependency ?
Many thanks

Marc

Albert Volkman’s picture

Status: Fixed » Needs review
FileSize
498 bytes

Dependency added.

guy_schneerson’s picture

Status: Needs review » Fixed

Hi @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.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.