3

I added a product and say it has count of ten(of that product), If one is bought, then I want to show in the product view page out of 10/1 sold like that.

Is there any contributed modules available, or custom way to do it? I can add the quantity as field in the product content type, but what do I do after that?

1

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.

  • Actually there is a bug in this code, i found out. The issue was this is only chekcing row count of the nid and no quantity check. So for a record if i order 3 of that products , still row count wil be only one not three... I am updating the answer. – Ram Nov 28 '13 at 11:14
  • I have posted the answer. You can update your answer too. i am not going to un-check your answer, so better you update your answer – Ram Nov 28 '13 at 11:18
1

This is not an answer, but you can find the stock level using the below code, just put in the node.tpl.php, and print the var $stock_html in a proper place, probably in node-product.tpl.php <?php print $stock_html ?> .

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

or you can try this http://www.ubercart.org/comment/60577

  • uc_stock_skus is a drupal5 function. – Ram Nov 15 '13 at 8:17
  • @Ram try to convert this function for drupal 7, since no other way to find the proper one. – Bala Nov 15 '13 at 8:20
  • This is just printing the stock level, but i want to print also how many has been already purchased. – Ram Nov 15 '13 at 9:25
  • I just have a doubt. Why is tat fetaure of stock is coming only while editing a product, not while adding a product? – Ram Nov 27 '13 at 6:19
0
         <?php
                $array_sku = uc_product_get_models($node->nid, FALSE);          
                foreach ($array_sku as $sku) {
                    $stock_level = uc_stock_level($sku);
                    //$stock_count = db_select('uc_order_products', 'up')->fields('up', array('nid'))->condition('up.model', $sku, '=')->execute()->rowCount();

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

This will output exact stock level and sold level of the product.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.