RSS

Newsletter

The CodeKarate Newsletter is the best way for technical ninjas to keep their swords sharpened. Don't worry, we won't flood your inbox and your email address will always remain private.
Back to Top

Drupal 6 Ubercart determine if node is a product content type

Note: Turns out there is a much easier way to do this as there is an Ubercart function that does essentially the same thing (thanks to the comment below that pointed this out). Just use:

uc_product_is_product($node);

I left the original post below (although it is no longer really needed).

I have seemed to run into the issue of trying to determine if a specific Drupal 6 node is an Ubercart product content type a few too many times. There are many ways to verify this from checking the Drupal database in the uc_products table, to looking at the Drupal node object for a sell_price attribute (or other Ubercart attribute), etc. Here is the way that seems to work the best for me and that I will be using from here on out.

I have put this in a Drupal 6 hook_nodeapi implementation to try to demonstrate a practical Drupal example:

/**
 * Implements hook_nodeapi().
 */
function MYMODULE_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  //print('<pre>'.print_r($node,1).'</pre>');
  if ($op == 'view') {
    // Check if this is a product node.
    $node_types = uc_product_types();
    if (in_array($node->type, $node_types)) {
      // Do something with the product.
    }
  }
}

Overall it is pretty easy, but just wanted to get this put up somewhere to not only help others, but to also help myself the next time I run into this situation. Any questions or comments? If so, let me know if the comments below.

Discussions

1
Ubercart master (not verified)

There's a function to do this.

uc_product_is_product($node)

1-1
shane

Thanks!

I knew I had seen a function like that before, but when I did my quick search I couldn't seem to find it. Thanks for pointing this out (I updated my post accordingly).

shane's picture

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <blockquote> <img>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <css>, <html>, <php>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <python>, <ruby>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.