44 replies [Last post]
opa001's picture
Offline
Joined: 12/07/2007
Juice: 55

Can I show the stock level on a catalog page. So that users know that a product is sold out or how many are in stock!

Thx

j
j's picture
Offline
Joined: 01/17/2008
Juice: 240
I'm also interested...

I'm also interested in customizing the catalog page ... It would be nice if you had an option to show stock levels and a fast add to cart button or link. Less clicks for the users viewing the store.

Good with CSS - Bad with Code. Smiling

opa001's picture
Offline
Joined: 12/07/2007
Juice: 55
Re: Stock level on catalog page

To show the "add to cart" button that's possible. I don't know where to set this option at this moment, but I thought you can do it by shop configuration and then catalog.

But there you can't set the option to show the stock level on the catalog page.

72dpi's picture
Offline
Joined: 10/07/2007
Juice: 134
Re: Re: Stock level on catalog page

Have you guys checked out Inventory API & Simple Stock levels?:
http://www.ubercart.org/contrib/132

it is really sweet. If you can print out the levels of stock on admin, then yes, you will be able to do it front-end.

U just need to add the "stocklevels" from /admin/content/types/product/fields
to show on your product pages.... (haven't tried, but sure it would be easy)..

Would be a nice addition.. Will post if I have success..

thefreefood's picture
Offline
Joined: 03/02/2008
Juice: 40
Re: Stock level on catalog page

I'm currently working on code that provides Views integration for Ubercart Attributes:

http://www.ubercart.org/contrib/3960

The code includes some Stock integration (i.e. you can choose to display only those products that are in stock). Including a stock level field as part of that code wouldn't be difficult. This would provide you the ability to make a view that showed the stock level of a product with a specific set of attribute options. The specific set of options could be chosen by an option select box in the Views edit page, and the default would be to show the stock level for a product with no attributes or attribute options.

Admittedly, this doesn't get the Stock level built into Ubercart's catalog feature, but if you're comfortable using Views, this should do exactly what you want.

I suspect I won't be able to work on this feature until sometime next week, but let me know if this is what you're looking for and I'll make sure to put it on my To-Do list.

qrios's picture
Offline
Cool profile pic award.Getting busy with the Ubercode.Internationalizationizer
Joined: 08/13/2007
Juice: 554
Theme snippet for stocklevel display on product pages

Maybe this is usefull to somebody:

It displays a small table with the current stocklevel of all stock-enabled SKU's for a product.

<?php
if ($node->type == 'product') {
 
$arr_sku = uc_stock_skus($node->nid);
 
$stock_html .='<table>';
  foreach(
$arr_sku as $sku){
   
$stocklevel = uc_stock_level($sku);
    if (
$stocklevel) {
      if (
$stocklevel < 1) {
       
$stocklevel = '<span class="nostock">'.$stocklevel.'</span>';
      }
     
$stock_html .='<tr><td>'.$sku.'</td><td>:&nbsp;&nbsp;'.$stocklevel.'</td></tr>';
    }
  }
 
$stock_html .='</table>';
}
?>

Put it in the node.tpl.php, and print the var $stock_html in a proper place.

Feel free to ask.

Regards,

Kees

Qrios Drupal Development

pokerking400's picture
Offline
Joined: 04/09/2008
Juice: 14
Re: Theme snippet for stocklevel display on product pages

How do i make stock level by stores?. How do i make store level inventory for each product?

How do i show which store the product is available?

How do i show which store is nearer to a zip code (different ranges).

qrios's picture
Offline
Cool profile pic award.Getting busy with the Ubercode.Internationalizationizer
Joined: 08/13/2007
Juice: 554
Re: Re: Theme snippet for stocklevel display on product pages
pokerking400 wrote:

How do i make stock level by stores?. How do i make store level inventory for each product?

How do i show which store the product is available?

How do i show which store is nearer to a zip code (different ranges).

Sorry, I don't know what youre talkin about. Ubercart IS a store. Just one.
Set up Ubercart twice and (you wont believe)you will end up with two stores, each one with it's own stock! Wow!

72dpi's picture
Offline
Joined: 10/07/2007
Juice: 134
Sweet keesje76, is that

Sweet keesje76,
is that using the simple stock levels?
if so, I am gonna add it. means I can then add a nice icon as well.

Cheers for the share mate!

littly_kitty's picture
Offline
Bug Finder
Joined: 01/20/2008
Juice: 155
Re: Theme snippet for stocklevel display on product pages

I'm finding this theme snippet to be a real gem as it dramatically cuts down customer questions about particular product attributes being in stock.

Placing the < ? php print $stock_html ? > in < fieldset class=" collapsible collapsed" > < / fieldset > tags, allows the table to be displayed in a collapsed field, which reduces screen clutter.

Two Questions:
1) Is there a way to use the snippet to display a message if there are no attributes in stock? (At the moment if no attributes are in stock, no table is rendered, which is confusing to the end user).

2) Also, this snippet only displays a decrease in stock when an item has been purchased. Is there any way to make it display a decrease when a product has been added to a cart, but hasn't yet been purchased.

^_^

qrios's picture
Offline
Cool profile pic award.Getting busy with the Ubercode.Internationalizationizer
Joined: 08/13/2007
Juice: 554
Re: Re: Theme snippet for stocklevel display on product pages

Good suggestion, thanks for sharing.
Answers:
1) Yes there is, funny, I made a small module for it. Gonna release it since it seem I'm not the only one needing this. Hang on, I'l post here when it's ready.

2) This is the nature of Ubercart stock levels. I personally like it the way it is.

is that using the simple stock levels?
No, it's based on the core stock module. Maybe it works with simple stock too, don't know.

pariah911's picture
Offline
Joined: 08/22/2008
Juice: 2
Stock Level

Heyas

Any update on this ?

I sell a mixture of items that are "in stock" or "custom made" or "special ordered"

Being able to show current Stock Level on product page helps provide a "Lead Time" for my customer base

I am not good with coding so anything "point and shoot" is ideal for me Smiling

frost's picture
Offline
Joined: 07/23/2008
Juice: 134
one simple approach

Here's a simple way to display stock level for nodes, and also to remove the "add to cart" button if the stock is zero.

Add the following to the template.php file in your sites/all/themes/YOUR_THEME_NAME directory, changing the function name according to your theme's name:

function YOUR_THEME_NAME_uc_product_add_to_cart( $node ) {
  $stocklevel = uc_stock_level($node->model);
  if (is_numeric($stocklevel)) {
    // Stock tracking is active
    if ($stocklevel == 0) {
      return( '<div class="out_of_stock">' . t('Sorry, this item is out of stock') . '</div>' );
    } else {
      return( '<div class="stock_level">' . t('Number available: ') . $stocklevel . '</div>' ) . theme_uc_product_add_to_cart($node);
    }
  } else {
    // Stock tracking is not being used for this product, just show the add to cart button as normal
    return theme_uc_product_add_to_cart($node);
  }
}

Note that there are some obvious limitations to this:
1. It doesn't stop someone adding a quantity to their cart that is greater than the stock available
2. It doesn't update in real time so you may see the "add to cart" button but there may be no stock available by the time you click it
3. Stock is normally updated when an item is purchased, but this code tests at "add to cart" time
4. It is unlikely to work with product attributes. I don't use attributes, so didn't test it, but from what I understand about them, I think this approach is too simplistic.

keyone's picture
Offline
Joined: 10/01/2008
Juice: 95
Re: one simple approach

I modified below code to allow for stock levels on product attributes. Not elegant, but it works Smiling

// ######### BEGIN CUSTOM CODING RELATED TO STOCK DISPLAY ###############

function YOURTHEMENAME_uc_product_add_to_cart( $node ) {

// look up all sku's for this product
$skus=uc_attribute_uc_product_models($node);

  if(!empty($skus)){  // if there are multiple sku's, then list stock availability of options.
  $avail = array();
  foreach($skus as $val) { 
    $sl = uc_stock_level($val);
    if (is_numeric($sl)) {
      $name=lookup_option_name($val);  // get the pretty name of option sku
      $avail[] =  " " .$name." (".$sl.")";     
    }
  }
    if(!empty($avail)) { $avail_string = "<br><div class=\"stock_level\">Number Available: ". implode(",",$avail) ."</div>"; }
    return( $avail_string ). theme_uc_product_add_to_cart($node);
}
else {  // else, we do the normal thing.
  $stocklevel = uc_stock_level($node->model);
  if (is_numeric($stocklevel)) {
    // Stock tracking is active
    if ($stocklevel == 0) {
      return( '<div class="out_of_stock">' . t('Sorry, this item is out of stock') . '</div>' );
    } else {
      return( '<div class="stock_level">' . t('Number available: ') . $stocklevel . '</div>' ) . theme_uc_product_add_to_cart($node);
    }
  } else {
    // Stock tracking is not being used for this product, just show the add to cart button as normal
    return theme_uc_product_add_to_cart($node);
  }
}
}

function uc_attribute_uc_product_models($node) {
  $models = array();
 
  // Get all the SKUs for all the attriibutes.
  $adjustments = db_query("SELECT model FROM {uc_product_adjustments} WHERE nid = %d", $node->nid);
  while ($adjustment = db_fetch_object($adjustments)) {
    if (!in_array($adjustment->model, $models)) {
      $models[$adjustment->model] = $adjustment->model;
    }
  }
 
  return $models;
 
  }
 
 
function  lookup_option_name($sku){
//uc_attribute_options: name,oid
//uc_product_adjustments: combination (number in "quotes" equals oid above),model

$query = "SELECT combination FROM {uc_product_adjustments} WHERE model =  '$sku'";
$result = db_query($query);
  while ($row = db_fetch_array($result)) {
    $combo = $row[combination];
    list($stuff, $oid, $junk) = split('"', $combo);
    $query2="SELECT name FROM {uc_attribute_options} WHERE oid = '$oid'";
    $result2=db_query($query2);
    while($row2=db_fetch_array($result2)){$name=$row2[name];}
    return $name;
  }

}

frost wrote:

Here's a simple way to display stock level for nodes, and also to remove the "add to cart" button if the stock is zero.

Add the following to the template.php file in your sites/all/themes/YOUR_THEME_NAME directory, changing the function name according to your theme's name:

function YOUR_THEME_NAME_uc_product_add_to_cart( $node ) {
  $stocklevel = uc_stock_level($node->model);
  if (is_numeric($stocklevel)) {
    // Stock tracking is active
    if ($stocklevel == 0) {
      return( '<div class="out_of_stock">' . t('Sorry, this item is out of stock') . '</div>' );
    } else {
      return( '<div class="stock_level">' . t('Number available: ') . $stocklevel . '</div>' ) . theme_uc_product_add_to_cart($node);
    }
  } else {
    // Stock tracking is not being used for this product, just show the add to cart button as normal
    return theme_uc_product_add_to_cart($node);
  }
}

Note that there are some obvious limitations to this:
1. It doesn't stop someone adding a quantity to their cart that is greater than the stock available
2. It doesn't update in real time so you may see the "add to cart" button but there may be no stock available by the time you click it
3. Stock is normally updated when an item is purchased, but this code tests at "add to cart" time
4. It is unlikely to work with product attributes. I don't use attributes, so didn't test it, but from what I understand about them, I think this approach is too simplistic.

icymetal's picture
Offline
Joined: 06/19/2009
Juice: 24
It works nice except... When

It works nice except...
When you enable quantity, you still can add higher quantity than the current stock level, thus getting a negative value of stock. With this code you'll get something like "Number available: -11".
Can this be fixed easily? How?
Thanks for the help.

clintthayer's picture
Offline
Joined: 07/26/2009
Juice: 22
Added, not working

Hey guys,

I added the code from keyone to my template.php file and am now getting this error:

Fatal error: Cannot redeclare uc_attribute_uc_product_models() (previously declared in /sites/default/modules/ubercart/uc_attribute/uc_attribute.module:347) in /sites/default/themes/twilight/template.php on line 124

I'm assuming I can re-declare the uc_attribute_uc_product_models() function???

Update:
Just commented out the function uc_attribute_uc_product_models() and made the change to the first one in the UC file. Regardless, can't seem to get anything to display!?!? Thoughts? I flushed the cache and hit the save button on the theme conf page... Nothing.

Any help would be great, thanks...
clint

qrios's picture
Offline
Cool profile pic award.Getting busy with the Ubercode.Internationalizationizer
Joined: 08/13/2007
Juice: 554
Re: Added, not working

You must have a copy/paste error somewhere. I dont see it redeclared in the posted snippet.
Beside this it probably needs modification for current versions.

grrrlshapedyarns's picture
Offline
Joined: 11/03/2009
Juice: 2
Re: Re: one simple approach

My theme doesn't have a template.php file

Kaktus's picture
Offline
Joined: 12/24/2009
Juice: 8
Does it work for 6.14?

It could be very useful function, but it doesnt work for me on Drupal 6.14. I am not coder. Have I missed something?

andygauk's picture
Offline
Joined: 05/21/2010
Juice: 3
Re: Re: one simple approach

I've been looking around for a fix like this for ages - great work, thanks. There was one change I made, and some other people seem to be having the same issue. A redeclare error came up because the code is trying to build a function when uc_attribute.module already has one by the same name.

i just changed the function name to uc_stock_attribute_uc_product_models.

cookiesunshinex's picture
Offline
Joined: 10/14/2009
Juice: 127
Re: one simple approach

Thanks so much for this code snippet. I was using http://drupal.org/project/uc_out_of_stock, but the ajax made my site run slow on the grid product pages.

This seems like a much more efficient and easy way to do what I wanted, and the bonus is that I'm able show a status of "In Stock" on my product page.

ryank76's picture
Offline
Joined: 01/16/2008
Juice: 61
frost wrote: Here's a simple
frost wrote:

Here's a simple way to display stock level for nodes, and also to remove the "add to cart" button if the stock is zero.

Add the following to the template.php file in your sites/all/themes/YOUR_THEME_NAME directory, changing the function name according to your theme's name:

function YOUR_THEME_NAME_uc_product_add_to_cart( $node ) {
  $stocklevel = uc_stock_level($node->model);
  if (is_numeric($stocklevel)) {
    // Stock tracking is active
    if ($stocklevel == 0) {
      return( '<div class="out_of_stock">' . t('Sorry, this item is out of stock') . '</div>' );
    } else {
      return( '<div class="stock_level">' . t('Number available: ') . $stocklevel . '</div>' ) . theme_uc_product_add_to_cart($node);
    }
  } else {
    // Stock tracking is not being used for this product, just show the add to cart button as normal
    return theme_uc_product_add_to_cart($node);
  }
}

Note that there are some obvious limitations to this:
1. It doesn't stop someone adding a quantity to their cart that is greater than the stock available
2. It doesn't update in real time so you may see the "add to cart" button but there may be no stock available by the time you click it
3. Stock is normally updated when an item is purchased, but this code tests at "add to cart" time
4. It is unlikely to work with product attributes. I don't use attributes, so didn't test it, but from what I understand about them, I think this approach is too simplistic.

I tried this one as well, even putting

<?php
print $stocklevel
?>

in the product page. It doesn't do anything.

What am I doing wrong? Everyone else seems to have it working.

george@dynapres.nl's picture
Offline
Joined: 01/02/2008
Juice: 202
Out of stock script

I tried:

function phptemplate_uc_product_add_to_cart( $node ) {
  $stocklevel = uc_stock_level($node->model);
  if (is_numeric($stocklevel)) {
    // Stock tracking is active
    if ($stocklevel == 0) {
      return( '<div class="out_of_stock">' . t('Out of stock') . '</div>' );
    } else {
      return( '<div class="stock_level">' . t('Number available: ') .
        $stocklevel . '</div>' ) . theme_uc_product_add_to_cart($node);
    }
  } else {
    // Stock tracking is not being used for this product,
    // just show the add to cart button as normal
    return theme_uc_product_add_to_cart($node);
  }
}

which works just fine with D6 / U2.

---
George on Drupal

qrios's picture
Offline
Cool profile pic award.Getting busy with the Ubercode.Internationalizationizer
Joined: 08/13/2007
Juice: 554
Display order

Did anyone had succes displaying the stocklevels of options in the right order (same order as the options itself are ordered)?

I'm struggling with this, I believe it's imposible.
See also: http://www.ubercart.org/issue/6541/uc_stock_skus_does_not_use_option_wei...

Any help greatly appreciated!

qrios's picture
Offline
Cool profile pic award.Getting busy with the Ubercode.Internationalizationizer
Joined: 08/13/2007
Juice: 554
Re: Display order
ryank76's picture
Offline
Joined: 01/16/2008
Juice: 61
doesn't like line 3
<?php
if ($node->type == 'product') {
 
$arr_sku = uc_stock_skus($node->nid);
 
$stock_html .='<table>';
  foreach(
$arr_sku as $sku){
   
$stocklevel = uc_stock_level($sku);
    if (
$stocklevel) {
      if (
$stocklevel < 1) {
       
$stocklevel = '<span class="nostock">'.$stocklevel.'</span>';
      }
     
$stock_html .='<tr><td>'.$sku.'</td><td>:&nbsp;&nbsp;'.$stocklevel.'</td></tr>';
    }
  }
 
$stock_html .='</table>';
}
?>

If I put this in the end of my node.tpl.php, I get:

Fatal error: Call to undefined function uc_stock_skus() in /var/www/drupal-6.14/themes/garland/node.tpl.php on line 36

Where line 36 is:
$arr_sku = uc_stock_skus($node->nid);

It doesn't seem to matter which theme I use. My setup is Drupal 6.14, Ubercart 6.x-2.0.

mikeejt's picture
Offline
Joined: 05/07/2011
Juice: 3
The call

Change your code:

$arr_sku = uc_stock_skus($node->nid);

To:

$arr_sku = uc_product_get_models($node, false);

You don't need to put the code in the post below in your template.php file. This was updated in UC 2.x.

epop's picture
Offline
Joined: 05/02/2008
Juice: 17
Re: Theme snippet for stocklevel display on product pages

#ryank76
Im also getting th same "undefined function error" with my install Drupal 6.14, Ubercart 6.x-2.0

epop's picture
Offline
Joined: 05/02/2008
Juice: 17
Re: Re: Theme snippet for stocklevel display on product pages

Can anyone tell us what we are doing wrong?

zeezhao's picture
Offline
Joined: 04/23/2008
Juice: 956
Re: Stock level on catalog page

subscribing.

fehin's picture
Offline
Joined: 12/17/2008
Juice: 151
Re: Re: Stock level on catalog page

subscribing

notmike's picture
Offline
Joined: 01/27/2010
Juice: 5
Re: Stock level on catalog page

The function uc_stock_skus() to have vanished from the module, just add the function to your template.php

http://drupalcontrib.org/api/function/uc_stock_skus/5

qrios's picture
Offline
Cool profile pic award.Getting busy with the Ubercode.Internationalizationizer
Joined: 08/13/2007
Juice: 554
Re: Re: Stock level on catalog page

Thanks for pointing out!

-----------
Drupal Webshop

Gynekolog's picture
Offline
Joined: 11/03/2010
Juice: 14
in template.php: function

in template.php:

function uc_stock_skus($nid) {
  $node = node_load($nid);

  if (is_null($node->model)) {
    return FALSE;
  }

  $skus = array($node->model);

  if (module_exists('uc_attribute')) {
    $models = db_query("SELECT model FROM {uc_product_adjustments} WHERE nid = %d", $node->nid);
    while ($model = db_fetch_object($models)) {
      if (!in_array($model->model, $skus)) {
        $skus[] = $model->model;
      }
    }
  }

  return $skus;
}

and in node-product.tpl.php

      <?php
if ($node->type == 'product') {
 
$arr_sku = uc_stock_skus($node->nid);
 
$stock_html .='<table>';
  foreach(
$arr_sku as $sku){
   
$stocklevel = uc_stock_level($sku);
    if (
$stocklevel) {
      if (
$stocklevel < 1) {
       
$stocklevel = '<span class="nostock">'.$stocklevel.'</span>';
      }
     
$stock_html .='<tr><td>'.$sku.'</td><td>:&nbsp;&nbsp;'.$stocklevel.'</td></tr>';
    }
  }
 
$stock_html .='</table>';
}
?>

<?php print $stock_html ?> 
qrios's picture
Offline
Cool profile pic award.Getting busy with the Ubercode.Internationalizationizer
Joined: 08/13/2007
Juice: 554
Re: Stock level on catalog page

Slightly off-topic: I want to be able to sort on the stock value. That is, if a stock is below 1 (<=0) than I want these products to be last in the row.
Anyone experience with this?

Thanks in advance.

----------
Mooi beddengoed

imrubio's picture
Offline
Joined: 01/28/2011
Juice: 41
Modified for those products that do not have stock active

I modified Keyone's code for those products that do not have stock tracking active. I was also getting WSOD if the stock tracking was not active so I use the drupal_get_form function instead to call up the normal add to cart form. I hope it helps someone as much as Keyone's code helped me.

// ######### BEGIN CUSTOM CODING RELATED TO STOCK DISPLAY ###############

function yourtheme_uc_product_add_to_cart($node) {
//First check if stock tracking is active
$stockactive = uc_stock_level($node->model);

if (is_numeric($stockactive)) {
// look up all sku's for this product
$skus=uc_stock_attribute_uc_product_models($node);

  if(!empty($skus)){  // if there are multiple sku's, then list stock availability of options.
  $avail = array();
  foreach($skus as $val) {
    $sl = uc_stock_level($val);
    if (is_numeric($sl)) {
      $name=lookup_option_name($val);  // get the pretty name of option sku
      $avail[] =  " " .$name." (".$sl.")";    
    }
  }
    if(!empty($avail)) { $avail_string = "<br><div class=\"stock_level\">Number Available: ". implode(",",$avail) ."</div>"; }
    return( $avail_string ). drupal_get_form('uc_product_add_to_cart_form', $node);
}
else {  // else, we do the normal thing.
    // Stock tracking is active
    if ($stocklevel == 0) {
      return( '<div class="out_of_stock">' . t('Sorry, this item is out of stock') . '</div>' );
    } else {
      return( '<div class="stock_level">' . t('Number available: ') . $stocklevel . '</div>' ) . drupal_get_form('uc_product_add_to_cart_form', $node);
    }

}
} else {
return drupal_get_form('uc_product_add_to_cart_form', $node);
}//end if stockactive
}

function uc_stock_attribute_uc_product_models($node) {
  $models = array();

  // Get all the SKUs for all the attriibutes.
  $adjustments = db_query("SELECT model FROM {uc_product_adjustments} WHERE nid = %d", $node->nid);
  while ($adjustment = db_fetch_object($adjustments)) {
    if (!in_array($adjustment->model, $models)) {
      $models[$adjustment->model] = $adjustment->model;
    }
  }

  return $models;

  }

function lookup_option_name($sku){
//uc_attribute_options: name,oid
//uc_product_adjustments: combination (number in "quotes" equals oid above),model

$query = "SELECT combination FROM {uc_product_adjustments} WHERE model =  '$sku'";
$result = db_query($query);
  while ($row = db_fetch_array($result)) {
    $combo = $row[combination];
    list($stuff, $oid, $junk) = split('"', $combo);
    $query2="SELECT name FROM {uc_attribute_options} WHERE oid = '$oid'";
    $result2=db_query($query2);
    while($row2=db_fetch_array($result2)){$name=$row2[name];}
    return $name;
  }

}

lindenlion's picture
Offline
Joined: 11/12/2008
Juice: 39
Nice work Imrubio,

it works beautifully!
I have a conceptual problem with it though:
Usually when you have attributes on a product, you cannot sell the item without an attribute selected. Like if you have different colours of paints for sale, nobody is going to buy 'paint', but people will buy 'white paint' or 'blue paint' or 'red paint', so 'paint' will not be stockactive, whereas all or some of the other attributes would be active. So I changed some of the conditions around.

<?php
function yourtheme_uc_product_add_to_cart($node) {

 

// look up all sku's for this product
 
$skus=uc_stock_attribute_uc_product_models($node);

  if(!empty(

$skus)){  // if there are multiple sku's, then list stock availability of options.
   
$avail = array();
    foreach(
$skus as $val) {
     
$sl = uc_stock_level($val);
      if (
is_numeric($sl)) {
       
$name=lookup_option_name($val);  // get the pretty name of option sku
       
$avail[] =  " " .$name." (".$sl.")";   
      }
    }
    if(!empty(
$avail)) { $avail_string = "<br><div class=\"stock_level\">Number Available: ". implode(",",$avail) ."</div>"; }
    return(
$avail_string ). drupal_get_form('uc_product_add_to_cart_form', $node);
  }
  else { 
// no options on this product

    // Check if stock tracking is active
   

$stockactive = uc_stock_level($node->model);
    if (
is_numeric($stockactive)) { //stock active
     
if ($stocklevel == 0) {
        return(
'<div class="out_of_stock">' . t('Sold Out') . '</div>' );
      } else {
        return(
'<div class="stock_level">' . t('Number available: ') . $stocklevel . '</div>' ) . drupal_get_form('uc_product_add_to_cart_form', $node);
      }
    }
    else {
// stock not active
     
return drupal_get_form('uc_product_add_to_cart_form', $node);
    }
  }
}

function

uc_stock_attribute_uc_product_models($node) {
 
$models = array();

 

// Get all the SKUs for all the attriibutes.
 
$adjustments = db_query("SELECT model FROM {uc_product_adjustments} WHERE nid = %d", $node->nid);
  while (
$adjustment = db_fetch_object($adjustments)) {
    if (!
in_array($adjustment->model, $models)) {
     
$models[$adjustment->model] = $adjustment->model;
    }
  }
  return
$models;
}

function

lookup_option_name($sku){
 
//uc_attribute_options: name,oid
  //uc_product_adjustments: combination (number in "quotes" equals oid above),model

 

$query = "SELECT combination FROM {uc_product_adjustments} WHERE model =  '$sku'";
 
$result = db_query($query);
  while (
$row = db_fetch_array($result)) {
   
$combo = $row[combination];
    list(
$stuff, $oid, $junk) = split('"', $combo);
   
$query2="SELECT name FROM {uc_attribute_options} WHERE oid = '$oid'";
   
$result2=db_query($query2);
    while(
$row2=db_fetch_array($result2)){$name=$row2[name];}
    return
$name;
  }
}
?>
Gynekolog's picture
Offline
Joined: 11/03/2010
Juice: 14
Re: Nice work Imrubio,

It works but after use does not work module "attribute stock filter" (http://drupal.org/project/uc_attribute_stock_filter)

imrubio's picture
Offline
Joined: 01/28/2011
Juice: 41
change $stocklevel to $stockactive

Hi Lindenlion,

I was still getting the 'Sold Out' message when stock levels were activated and set to a positive number. I found that the variable $stocklevel was never set in the version I posted so changing that to $stockactive does the trick. Here's the new code.

<?php
// ######### BEGIN CUSTOM CODING RELATED TO STOCK DISPLAY ###############

function yourtheme_uc_product_add_to_cart($node) {

// look up all sku's for this product
 
$skus=uc_stock_attribute_uc_product_models($node);

  if(!empty(

$skus)){  // if there are multiple sku's, then list stock availability of options.
   
$avail = array();
    foreach(
$skus as $val) {
     
$sl = uc_stock_level($val);
      if (
is_numeric($sl)) {
       
$name=lookup_option_name($val);  // get the pretty name of option sku
       
$avail[] =  " " .$name." (".$sl.")";  
      }
    }
    if(!empty(
$avail)) { $avail_string = "<br><div class=\"stock_level\">Number Available: ". implode(",",$avail) ."</div>"; }
    return(
$avail_string ). drupal_get_form('uc_product_add_to_cart_form', $node);
  }
  else { 
// no options on this product

    // Check if stock tracking is active
  

$stockactive = uc_stock_level($node->model);
    if (
is_numeric($stockactive)) { //stock active
     
if ($stockactive == 0) {
        return(
'<div class="out_of_stock">' . t('Sold Out') . '</div>' );
      } else {
        return(
'<div class="stock_level">' . t('Number available: ') . $stockactive . '</div>' ) . drupal_get_form('uc_product_add_to_cart_form', $node);
      }
    }
    else {
// stock not active
     
return drupal_get_form('uc_product_add_to_cart_form', $node);
    }
  }
}

function

uc_stock_attribute_uc_product_models($node) {
 
$models = array();

// Get all the SKUs for all the attriibutes.
 
$adjustments = db_query("SELECT model FROM {uc_product_adjustments} WHERE nid = %d", $node->nid);
  while (
$adjustment = db_fetch_object($adjustments)) {
    if (!
in_array($adjustment->model, $models)) {
     
$models[$adjustment->model] = $adjustment->model;
    }
  }
  return
$models;
}

function

lookup_option_name($sku){
 
//uc_attribute_options: name,oid
  //uc_product_adjustments: combination (number in "quotes" equals oid above),model

$query = "SELECT combination FROM {uc_product_adjustments} WHERE model =  '$sku'";
 
$result = db_query($query);
  while (
$row = db_fetch_array($result)) {
   
$combo = $row[combination];
    list(
$stuff, $oid, $junk) = split('"', $combo);
   
$query2="SELECT name FROM {uc_attribute_options} WHERE oid = '$oid'";
   
$result2=db_query($query2);
    while(
$row2=db_fetch_array($result2)){$name=$row2[name];}
    return
$name;
  }
}
?>
sean_buckingham's picture
Offline
Joined: 04/27/2011
Juice: 7
Re: Stock level on catalog page

Very interesting reading here..

Has anyone had any experience with using any of these solutions at the same time as Ubercart Out of Stock module?
Any issues?
http://drupal.org/project/uc_out_of_stock

At the moment I'm still wading through all the various choices for stock level display and stock control.

thanks,
Sean

sean_buckingham's picture
Offline
Joined: 04/27/2011
Juice: 7
Re: Re: Stock level on catalog page

Just to update that I've added imrubio's script above, as well as the "uc out of stock" module on my test site.

So far all is working well, with a mixture of products with and without stock tracking enabled.

persianninja's picture
Offline
Joined: 06/10/2011
Juice: 38
I am using the Stock Notify

I am using the Stock Notify module so people can sign up when item is back in stock.... when i added imrubio's show stock levels' script above it doesn't work anymore i think since they both messing with uc_product_add_to_cart_form?

Has anyone got both working together?

EDIT:
I am not sure but i think when i replace all instances of :
drupal_get_form('uc_product_add_to_cart_form', $node)
with
theme_uc_product_add_to_cart($node)
the weird stuff stops happening...

When using drupal_get_form, it completely avoided the Stock Notify page, it went to the page where it says if a product is out of stock... but it would say the wrong product in that message for some reason...
theme_uc_product_add_to_cart seems to fix it. I am not sure if thats correct to use, i just took a guess, heh.

webmayin's picture
Offline
Joined: 01/12/2010
Juice: 7
Re: Stock level on catalog page

Has anyone solved the issue of people being able to buy more products than are in stock?

2faroffroad's picture
Offline
Joined: 11/04/2011
Juice: 12
The topic has not even been touched..

Topic is - "Stock level on catalog page"....... not product pages....

I would like to show stock levels on the catalog page for each item under that taxonomy vocabulary.

You have provided solutions for product pages and I have used them.. Thank you..
I would like to see some solutions for catalog page.

Any idea's.
I have tried... But I am unsure and it required hacking the uc_product.module code to insert.
also, I can not get stock level to pull into module.

Thanks.

2faroffroad's picture
Offline
Joined: 11/04/2011
Juice: 12
Hacked it... but it works!!!

I now have stock counts displaying below the Add to Cart button in the catalog page...

in - uc_product.module - around line 1419-1435
I added this to - function uc_product_table

$stock = db_fetch_object(db_query("SELECT active, stock FROM {uc_product_stock} WHERE nid= '$node->nid'", $sku));

I added this to print the stock levels where I wanted them.

"In Stock: $stock->stock"