I'm working on a module that will enable a customer to customize their product before they add it to their cart. I'm implementing the hook_form_alter on the product view to add additional form fields including: a text area and a file upload. (I've included an attachment of what that currently looks like.)
I'd like to capture the data from the altered form and do something useful with it. So far I'm able to upload a file to the server, from that point on I'm a little stuck. Ideally the file path and additional form info would be posted someplace convenient for the store owner to retrieve and associate with the order.
One idea would be to use the uc_order_comment_save function to append the data to the comment field in the admin order view, but I'm not sure when the function gets called or which hook to use.
Here's the code I'm working with, any ideas or suggestions would be extremely helpful.
(Please excuse me if some of my comments are wrong, I'm a PHP noob.)
<?php
/*
implementation of hook_form_alter
*/
function uc_upload_logo_form_alter($form_id, &$form) {
if ('uc_product_add_to_cart_form_' . $form['nid']['#value'] == $form_id) {
$node = node_load($form['nid']['#value']);
// This checks whether the product is a "personalized_cards" class
// The form won't show up unless the product is a class named with the above!!!
if($node->type == 'personalized_cards'){
$form['personal_message'] = array(
'#title' => t('Personalized message'),
'#type' => 'textarea',
'#description' => t('Please enter your personalized message here'),
'#cols' => 40,
'#rows' => 3,
'#resizable' => FALSE,
'#weight' => -1
);
$form['logo'] = array(
'#type' => 'file',
'#title' => t('Upload a personalized image'),
'#size' => 40,
'#weight' => 0,
'#description' => t('Please upload the image you would like to appear in your card.
Image resolution of at least 300dpi is required. Please be patient while your file uploads.')
);
// Required by Drupal form API
$form['#attributes']['enctype'] = 'multipart/form-data';
// Appends the form data to the form submit function
//($form_id = uc_product_add_to_cart_form, $form_values =
$form['#submit'] += array('uc_upload_logo_form_submit' => array($form_id, &$form_values));
}
}
}
// Call this function after the form submit has passed.
// Writes the submitted file to the server. Make sure a directory exists or it will fail silently!
function uc_upload_logo_form_submit($form_id, $form_values) {
$dir = variable_get('file_directory_path', NULL);
// Commented out for testing
//$is_writable = file_check_directory($dir, 1);
// if($is_writable) {
// Save the file to the below path and append the actual file name
$dir .= 'imagecache/logos/' . $dir;
$source = file_check_upload('logo');
// Security measure to prevent exploit of file.php.png
$source->filename = upload_munge_filename($source->filename);
if ($file = file_save_upload($source,$dir )) {
if (image_get_info($file->filepath)) {
drupal_set_message(t('New image saved. '.$file->filepath.''));
} else {
file_delete($file->filepath);
drupal_set_message('Uploaded file does not appear to be a valid image file. Please try again.');
}
}
}
?>Thanks in advance for anyone willing to take the time to help me learn. I would definitely be interested in submitting the module as a contribution when it's finished, I think there's definitely a need for it.
| Preview | Attachment | Size |
|---|---|---|
![]() | product_view.jpg | 36.71 KB |

