5 replies [Last post]
Axel_Pressbutton's picture
Offline
Joined: 11/30/2008
Juice: 123
Was this information Helpful?

Hi All,

I was wondering if anyone has any tips on the best way I could create a 'limited edition' product that would display the current stock level on the product detail page?

I've tried code that I noticed on another post by Marillion@drupal.org (dropped into template.php)

<?php
/**
* Display the stock level of products
*/
function phptemplate_uc_product_add_to_cart( $node ) {
$stocklevel = uc_stock_level($node->model);
return(
'' . t('Only ') . $stocklevel . t(' left in stock') . '' ) . theme_uc_product_add_to_cart($node);
}
?>

This certainly returns the correct information but I only want to associate/display it with a particular type of product. As the Product content type is generic I'm guessing I somehow need to modify this code so that it includes some form of category restriction (I have a taxonomy term for Limited Edition)...OR am I better off taking a completely different approach?

Any pointers would be greatly appreciated. Many thanks Laughing out loud

justageek's picture
Offline
Bug Finder
Joined: 10/29/2008
Juice: 189
you can get taxonomy terms and loop through them.
<?php
$limited
= false;
$terms = taxonomy_node_get_terms($node->nid);
if (!empty(
$terms))
{
  foreach (
$terms as $term)
  {
    if (
$term->tid == the_term_id_for_your_desired_term)
    {
     
$limited = true;
      break;
    }
  }
}
if (
$limited)
{
 
$stocklevel = uc_stock_level($node->model);
  return(
'' . t('Only ') . $stocklevel . t(' left in stock') . '' ) . theme_uc_product_add_to_cart($node);
}
?>
Axel_Pressbutton's picture
Offline
Joined: 11/30/2008
Juice: 123
Re: you can get taxonomy terms and loop through them.

WOW justageek, Many thanks for the super fast response to this!!! I owe you a beer (if you happen to be around Bristol/Bath UK at any time)

Your solution worked like a dream.

Sorry, I'm still quite new to Drupal/Ubercart and I'm still getting used to how things hang together.

Axel_Pressbutton's picture
Offline
Joined: 11/30/2008
Juice: 123
Re: Re: you can get taxonomy terms and loop through them.

In my haste, and possible lack of clearing the cache...I've just noticed that on other products (not associated with the term id) the Add to Cart button is missing - where would I need to specify the displaying of 'just' the add to cart button without the number left in stock counter?

Should I use a different override or does it need to be associated with theme_uc_product_add_to_cart or is this just a positional thing inherited from the code I previously found?

Thanks again

Axel_Pressbutton's picture
Offline
Joined: 11/30/2008
Juice: 123
Re: you can get taxonomy terms and loop through them.

Sorry, looks like I had a flash of inspiration and solved it with a cup of coffee and an ELSE statement - DOH!

(Providing this is OK to do of course Smiling )

<?php
/**
* Display the stock level of products
*/
function phptemplate_uc_product_add_to_cart( $node ) {
 
$limited = false;
 
$terms = taxonomy_node_get_terms($node->nid);
  if (!empty(
$terms))
  {
    foreach (
$terms as $term)
    {
      if (
$term->tid == the_term_id_for_your_desired_term)
      {
       
$limited = true;
        break;
      }
    }
  }
  if (
$limited)
  {
   
$stocklevel = uc_stock_level($node->model);
   
$output .= '<div class="number-in-stock">' . t('Only ') . $stocklevel . t(' left in stock') . '</div>' theme_uc_product_add_to_cart($node);
    return
$output;
  }
  else
  {
   
$stocklevel = uc_stock_level($node->model);
   
$output .= theme_uc_product_add_to_cart($node);
    return
$output;
  }
}
?>
justageek's picture
Offline
Bug Finder
Joined: 10/29/2008
Juice: 189
oops...

I was hastly in my response, so glad you caught that, I should have known there had to be a return either way Smiling