Code snippets to display total and sold item from stock:
$output = '';
$array_sku = uc_product_get_models($node->nid, FALSE);
foreach ($array_sku as $sku) {
$stock_level = uc_stock_level($sku);
$stock_counts = db_select('uc_order_products', 'up')
->fields('up', array('nid','qty'))
->condition('up.model', $sku, '=')
->execute()
->fetchAll();
$quantity_count = 0;
if(isset($stock_counts)) {
foreach($stock_counts as $stock_count ) {
$quantity_count += $stock_count->qty;
}
}
$total = $stock_level + $quantity_count;
if($total == 0){
$total = 1;
}
$output .= $total . " / " . $quantity_count . " Sold out";
}
print $output;
Kindly see that above code will be check stock level for each different SKU
of the product. If there is attribute of the product and each attribute has different SKU
, then it will display the stock
for each sKU
of the product.
It will work perfect in fresh "Ubercart" installation with "stock" configuration.
You can add this code in node--product.tpl.php
file in your theme directory.