function uc_varprice_form_alter in UC Variable Price 7
Same name and namespace in other branches
- 6 uc_varprice.module \uc_varprice_form_alter()
Implements 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 29 - 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_query("SELECT COUNT(*) FROM {uc_product_features} WHERE nid = :nid AND fid = :fid", array(
':nid' => arg(1),
':fid' => 'varprice',
))
->fetchField()) {
// 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) {
$description = array();
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 && !empty($data->amount_title) ? $data->amount_title : t('Amount'),
'#description' => implode('<br />', $description),
'#default_value' => $data ? $data->price_default : 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', '$') : '',
);
}
}
// 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 (isset($data['varprice']) && $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 = FALSE;
if (!empty($form['pcid']['#value'])) {
$class_defaults = variable_get('ucvp_class_def_' . $form['pcid']['#value'], array());
if (!empty($class_defaults)) {
$data = (object) unserialize($class_defaults);
}
}
$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['#submit'][] = 'uc_varprice_product_class_submit';
$form['submit']['#weight'] = 10;
}
}