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;
}
}
?>