Kristian Lunde

Getting the default option of a ubercart product attribute

with 2 comments

In Orange Bus we are currently busy building a new web shop for a clothing company. We are building this web shop on Drupal 6 and Ubercart 2. While I was doing some tuning of the product page (built as a node template) on this site I suddenly realized that even though you can get most of the information needed in from the $node object, you are unable to get the default options of each attribute.

In my case this attribute was the sizes of the products (small, medium, large and so on), the node object contained all the attributes but not the default options. It is not at all complicated to get this information but you do need to add some custom code to get a hold of the default options. I would argue that this should be included in the default node object, which really should not be a big deal adding. I guess I should add a patch for this, instead of going around the problem which is what I do and describe here.

To get a hold of this I had to call a ubercart specific function called uc_product_get_attributes function. This function takes a node id as parameter and return all an array of all the attributes related to the node. The array contain a set of attributes objects and these object contain all the information available on each attribute.

My solution was to call the uc_product_get_attributes function and get the default_option variable from the attribute object, see code example below.

  1. //get all attributes related to the node
  2. $attributes = uc_product_get_attributes($node->nid);
  4. //get the id of default size of the product
  5. $default_size = $attributes[1]->default_option;

It is simple, but it took me about an 30 minutes to determine the problem and adding a solution. Hopefully this will save someone the job of solving the same problem.

Be Sociable, Share!

Written by Kristian Lunde

February 20th, 2009 at 10:02 pm

Posted in Drupal,PHP,Programming,web

Tagged with ,

2 Responses to 'Getting the default option of a ubercart product attribute'

Subscribe to comments with RSS or TrackBack to 'Getting the default option of a ubercart product attribute'.

  1. This problem bugged the hell out of me too.
    I found that the default failed to appear if it had the same SKU as the option.

    A non-coding way around is to use a Base SKU for the product and then make new codes for all options.

    The default appears in the stock but you turn it inactive.
    Then all the options become available.

    A non-programming work-around.

    btw… you haven’t told anywhere where to put your code?


    9 Apr 09 at 23:54:13

  2. Hi Ben

    Yes that is also a soltution to it, but it is definitely not the optimal way to go about it. I am pretty sure this is a bug in the Ubercart module.

    No you are right, I did not, I added this piece of code in my node-product.tpl.php template (which is the template file of my products). I added this before printing out the size selector and cart button, as you can see in my example below.

    //get all attributes related to the node
    $attributes = uc_product_get_attributes($node->nid);
    //get the id of default size of the product
    $default_size = $attributes[1]->default_option;
    // Print the chart button
    echo $node->content['add_to_cart']['#value'];

    The echo $node->content['add_to_cart']['#value'] is where the actual size selector and cart button is generated.

    Kristian Lunde

    11 Apr 09 at 09:04:42

Leave a Reply

Get Adobe Flash player