Ubercart 2 Attributes

I've been wrestling with Ubercart (Drupal's shopping cart) which is not unusual - everything I do with Ubercart seems to be a wrestle. I even get to win sometimes ! In Ubercart you can add attributes to products ie. Product = Tee-shirt and the tee-shirt has some attributes like different sizes. Normally that's not any problem. You can even give each attribute a price and SKU. Still no problem !

Well we had a client that needed to add an extra field to each attribute that would contain an industry id - each product attribute also has an individual SKU. I had a good search around the forums and it seems that no such method exists so I've created a small module that allows us to do this create your module sites/all/modules/custom/kp_op_extra note that I use by initials just to make sure I don't get a module clash kp_op_extra.info

name = KP OP Extra
description = Function that allows an extra field in attribute options
core = 6.x


 * Implements hook schema
function kp_op_extra_schema_alter(&$schema) {
  // Add field to existing schema.
  $schema['uc_product_options']['fields']['pharmacode'] = array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
    'description' => 'Pharmacode.',

Note that the extra field that is being added is Pharmacode This is the schema that we need (I manually added the field to the table)

 * Function that adds a pharma code in the options
function kp_op_extra_form_alter(&$form, &$form_state, $form_id) {
    if($form_id === 'uc_attribute_option_form') {

        $form['pharmacode'] = array(
            '#type' => 'textfield',
            '#title' => t('Pharmacode'),
            '#description' => t('Add Pharmacode'),
            '#default_value' => kp_op_extra_get_pharmacode($form['oid']['#value']),
        $form['#submit'][]= 'kp_op_extra_submit';

The main form alter that calls a function to fill the field

 * Function to get the pharmacode
function kp_op_extra_get_pharmacode($option_id = 0) {
    if($option_id > 0) {
        $result = db_query("SELECT pharmacode FROM {uc_attribute_options} WHERE oid = %d", $option_id);
        return db_result($result);
    } else {
        return '';

Now the function that inserts or updates the table

function kp_op_extra_submit($form, &$form_state) {
  if (!isset($form_state['values']['oid'])) {
    db_query("INSERT INTO {uc_attribute_options} (aid, name, pharmacode, cost, price, weight, ordering) VALUES (%d, '%s','%s', %f, %f, %f, %d)",
      $form_state['values']['aid'], $form_state['values']['name'],$form_state['values']['pharmacode'], $form_state['values']['cost'], $form_state['values']['price'], $form_state['values']['weight'], $form_state['values']['ordering']);
    drupal_set_message(t('Created new option %option.', array('%option' => $form_state['values']['name'])));
    watchdog('uc_attribute', 'Created new option %option.', array('%option' => $form_state['values']['name']), WATCHDOG_NOTICE, 'admin/store/attributes/'. $form_state['values']['aid'] .'/options/add');
    $form_state['redirect'] = 'admin/store/attributes/'. $form_state['values']['aid'] .'/options/add';
  else {
    db_query("UPDATE {uc_attribute_options} SET name = '%s', pharmacode = '%s', cost = %f, price = %f, weight = %f, ordering = %d WHERE aid = %d AND oid = %d",
      $form_state['values']['name'], $form_state['values']['pharmacode'], $form_state['values']['cost'], $form_state['values']['price'], $form_state['values']['weight'], $form_state['values']['ordering'], $form_state['values']['aid'], $form_state['values']['oid']);
    drupal_set_message(t('Updated option %option.', array('%option' => $form_state['values']['name'])));
    watchdog('uc_attribute', 'Updated option %option.', array('%option' => $form_state['values']['name']), WATCHDOG_NOTICE, 'admin/store/attributes/'. $form_state['values']['aid'] .'/options/'. $form_state['values']['oid']);
    $form_state['redirect'] = 'admin/store/attributes/'. $form_state['values']['aid'] .'/options';

Install the module and you're ready to go. I've not created any admin section or stuff because I know that I won't be using this module very often and if I do it will need different fields. If anyone knows a better way please leave a comment

Post your comment


  • James Law 03/01/2016 4:45pm (2 years ago) Gravatar for James Law

    Adding an extra field to UC Attributes - UC3 D7
    Hey Kevin, Hope you don't mind me asking you something about this article of yours 2 years since you posted it.. Although I'm desperate for a little help! I've searched all over for the ability to add and display an extra field with my product attribute options - basically just a 'description' text that explains the options a little further. I'm not much of a developer, but the code above looks like it might be doing what I need.. Could you possible confirm that it might - and how different would it need to be for UC3 on D7? Thank you, and any response would be massively appreciated! James

    • Kevin 28/07/2013 6:25pm (4 years ago) Gravatar for Kevin

      Hi James - I'm not sure about UC3 as I've moved over to Drupal Commerce, which makes it much easier to add custom fields.

RSS feed for comments on this page | RSS feed for all comments

Copywrite © 2018 Kevin Phillips All rights are reserved.

Proudly supporting opensource software