Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Drupal 6 Ubercart determine if node is a product content type

Parent Feed: 

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.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web