change $stocklevel to $stockactive

imrubio's picture
Offline
Joined: 01/28/2011
Juice: 44
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;
  }
}
?>
Stock level on catalog page By: opa001 (44 replies) Mon, 03/31/2008 - 16:11