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
;$Id$ name = KP OP Extra description = Function that allows an extra field in attribute options core = 6.x
kp_op_extra.module
/** * 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
2 Comments
Thank you for this snippet
Submitted by Bronislovas (not verified) on
Thank you for this snippet
Hello. This snippet-module really great! Maybe can you told or suggest how to print industry id (pharmacode) of selected option in node? Thank you!
Printing out fields
Submitted by kevin on
Generally I always create a node-product.tpl.php into my theme directory, I’ve never really liked the default layout so once you’ve created your node-product.tpl.php (don’t forget you need a copy of node.tpl.php inside your theme directory) you can control the layout easily. Devel and Devel-themer are your best friends.
Most things broken in our house was done by "Nobody" the just was my fault anyway!
Add new comment