function uc_varprice_form_alter in UC Variable Price 6
Same name and namespace in other branches
- 7 uc_varprice.module \uc_varprice_form_alter()
Implementation of hook_form_alter().
Summary of alterations: 1) Alters the product feature add form to restrict multiple Variable Price features from being added to a single product. 2) Alters the add to cart form for variable priced products. 3) Disable the appropriate Qty. fields on the cart view form. 4) Alter the product class form to set default donations.
File
- ./
uc_varprice.module, line 36 - Defines a product feature to turn any product into a variable priced product.
Code
function uc_varprice_form_alter(&$form, &$form_state, $form_id) {
// 1) Alter the product feature add form.
if ($form_id == 'uc_product_feature_add_form') {
// If a Variable Price feature has already been added to this product...
if (db_result(db_query("SELECT COUNT(*) FROM {uc_product_features} WHERE nid = %d AND fid = '%s'", arg(1), 'varprice'))) {
// Remove Variable Price from the available list of features to add.
unset($form['feature']['#options']['varprice']);
}
}
// 2) Alter the add to cart form.
if (strpos($form_id, 'uc_product_add_to_cart_form_') === 0) {
$data = uc_varprice_product_load($form['nid']['#value']);
if ($data) {
// Determine the default value
$default_val = !empty($data->price_default) ? $data->price_default : variable_get('uc_varprice_global_default', '0');
// Are we going to add a selectable widget?
if ($data->selectable) {
$options = $data->sel_options;
// Add "other" option if users can also enter arbitrary values
if ($data->arbitrary) {
$options['other'] = t('Other...');
}
if (isset($options[$default_val])) {
// If the default value is in the options…
$sel_default = $default_val;
$default_val = FALSE;
}
elseif ($data->arbitrary) {
// If "other" is an option, select that by default and have the
// default value appear in the arbitrary value field.
$sel_default = 'other';
}
elseif (count($options)) {
// If all else fails, just select the first item.
$sel_default = array_shift(array_keys($options));
}
else {
// If we get here, then there aren't any options in the list anyway.
// What the heck?
$sel_default = '';
}
$form['varprice_sel'] = array(
'#type' => $data->sel_widget,
'#title' => !empty($data->amount_title) ? $data->amount_title : t('Amount'),
'#options' => $options,
'#default_value' => $sel_default,
'#weight' => -5.2,
);
}
if ($data->arbitrary) {
$description = array();
if ($data->selectable) {
$description[] = t('If you’ve selected “Other…” above, enter the precise amount here.');
}
if (!empty($data->price_minimum)) {
$description[] = t('Minimum: @price', array(
'@price' => uc_currency_format($data->price_minimum),
));
}
if (!empty($data->price_maximum)) {
$description[] = t('Maximum: @price', array(
'@price' => uc_currency_format($data->price_maximum),
));
}
// Add the amount textfield to the add to cart form.
$form['varprice'] = array(
'#type' => 'textfield',
'#title' => $data->selectable ? '' : (!empty($data->amount_title) ? $data->amount_title : t('Amount')),
'#description' => implode('<br />', $description),
'#default_value' => !empty($default_val) ? $default_val : ($data->selectable ? '' : variable_get('uc_varprice_global_default', '0')),
'#size' => 8,
'#weight' => -5,
'#field_prefix' => variable_get('uc_sign_after_amount', FALSE) ? '' : variable_get('uc_currency_sign', '$'),
'#field_suffix' => variable_get('uc_sign_after_amount', FALSE) ? variable_get('uc_currency_sign', '$') : '',
'#attributes' => array(
'class' => 'uc_varprice_arb',
),
);
// Relabel the "Add to cart" button
if (!empty($data->add_to_cart_title)) {
$form['submit']['#value'] = $data->add_to_cart_title;
}
}
$form['#validate'][] = 'uc_varprice_add_to_cart_form_validate';
}
}
// 3) Disable the appropriate Qty. fields on the cart view form.
if ($form_id == 'uc_cart_view_form') {
for ($i = 0, $j = count(uc_cart_get_contents()); $i < $j; $i++) {
$data = unserialize($form['items'][$i]['data']['#value']);
// If this item has a quantity restriction on it...
if ($data['varprice'] > 0) {
$form['items'][$i]['qty']['#type'] = 'value';
$form['items'][$i]['qty']['#theme'] = 'varprice_qty';
}
}
}
// 4) Alter the product class form to set default donations.
if ($form_id == 'uc_product_class_form') {
// Add some helper JS to the form.
drupal_add_js(drupal_get_path('module', 'uc_varprice') . '/uc_varprice.js');
$data = variable_get('ucvp_class_def_' . $form['pcid']['#value'], array());
if (!empty($data)) {
$data = (object) unserialize($data);
}
else {
$data = FALSE;
}
$form['varprice'] = array(
'#type' => 'fieldset',
'#title' => t('Default Variable Price product feature'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 5,
);
$form['varprice']['default_varprice'] = array(
'#type' => 'checkbox',
'#title' => t('Check this box to add a default product feature to every product of this class using these settings.'),
'#default_value' => $data === FALSE ? FALSE : TRUE,
);
$form['varprice'] += _uc_varprice_feature_form($data);
$form['#validate'][] = 'uc_varprice_feature_form_validate';
$form['#submit'][] = 'uc_varprice_product_class_submit';
$form['submit']['#weight'] = 10;
}
}