Search

Friends

Atomspheric CO2 (PPM)

Archives

Blather

Uptime verified by Wormly.com

14 December 2011

Preventing ordering out of stock items in Ubercart 3

In template.php or some such...

function site_uc_product_add_to_cart( $variables ) {
  $sku = $variables['form']['node']['#value']->model;
  $stocklevel = uc_stock_level($sku);
  if (is_numeric($stocklevel)) {
    // Stock tracking is active
    if ($stocklevel <= 0) {
      return '<div class="add-to-cart out-of-stock"><button disabled>' . t('Sold') . '</button></div>';
    }
    else {
      return theme_uc_product_add_to_cart($variables);
    }
  }
  else {
    // Stock tracking is not being used for this product, just show the add to cart button as normal
    return theme_uc_product_add_to_cart($variables);
  }
}
  • 7:57pm
  •  
  • PHP

Comments

  1. I tried this with Drupal 7 and UC 3 to no avail. Thoughts on why it didn’t work?
    Thanks for the snippet!

    Derek M / 8:21am / 9 January 2012

  2. Did you get the function name right? You’ll have to change “site_” in the function name to the name of your theme.

    Ryan / 11:54am / 10 January 2012

Leave a comment

Markdown

0.096 seconds