function uc_product_kit_form in Ubercart 7.3
Same name and namespace in other branches
- 5 uc_product_kit/uc_product_kit.module \uc_product_kit_form()
- 6.2 uc_product_kit/uc_product_kit.module \uc_product_kit_form()
Implements hook_form().
File
- uc_product_kit/
uc_product_kit.module, line 490 - The product kit module for Ubercart.
Code
function uc_product_kit_form(&$node, $form_state) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#required' => TRUE,
'#weight' => -5,
'#default_value' => $node->title,
'#description' => t('Name of the product kit'),
);
// Create an array of products on the site for use in the product selector.
$product_types = uc_product_types();
$products = array();
// Disregard other product kits.
unset($product_types[array_search('product_kit', $product_types)]);
// Query the database and loop through the results.
$products = db_query("SELECT nid, title FROM {node} WHERE type IN (:types) ORDER BY title, nid", array(
':types' => $product_types,
))
->fetchAllKeyed();
$form['base'] = array(
'#type' => 'fieldset',
'#title' => t('Product kit information'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => -10,
'#group' => 'additional_settings',
);
$form['base']['mutable'] = array(
'#type' => 'radios',
'#title' => t('How is this product kit handled by the cart?'),
'#options' => array(
UC_PRODUCT_KIT_UNMUTABLE_NO_LIST => t('As a unit. Customers may only change how many kits they are buying. Do not list component products.'),
UC_PRODUCT_KIT_UNMUTABLE_WITH_LIST => t('As a unit. Customers may only change how many kits they are buying. List component products.'),
UC_PRODUCT_KIT_MUTABLE => t('As individual products. Customers may add or remove kit components at will. Discounts entered below are not applied to the kit price'),
),
'#default_value' => $node->mutable,
);
$form['base']['products'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#required' => TRUE,
'#title' => t('Products'),
'#options' => $products,
'#default_value' => array_keys($node->products),
);
$total = 0;
$base_total = 0;
$form['base']['items'] = array(
'#tree' => TRUE,
'#theme' => 'uc_product_kit_items_form',
'#weight' => 1,
'#description' => t('Enter a positive or negative discount to raise or lower the item price by that amount. The change is applied to each item in the kit.'),
);
if (!empty($node->products)) {
foreach ($node->products as $i => $product) {
$form['base']['items'][$i] = array(
'#type' => 'fieldset',
);
$form['base']['items'][$i]['link'] = array(
'#type' => 'item',
'#markup' => l($product->title, 'node/' . $i),
);
$form['base']['items'][$i]['qty'] = array(
'#type' => 'uc_quantity',
'#title' => t('Quantity'),
'#title_display' => 'invisible',
'#default_value' => $product->qty,
);
$form['base']['items'][$i]['ordering'] = array(
'#type' => 'weight',
'#title' => t('List position'),
'#title_display' => 'invisible',
'#default_value' => isset($product->ordering) ? $product->ordering : 0,
'#attributes' => array(
'class' => array(
'uc-product-kit-item-ordering',
),
),
);
$form['base']['items'][$i]['discount'] = array(
'#type' => 'textfield',
'#title' => t('Discount'),
'#title_display' => 'invisible',
'#field_prefix' => uc_currency_format($product->sell_price) . ' + ',
'#default_value' => isset($product->discount) ? number_format($product->discount, 3, '.', '') : 0,
'#size' => 5,
);
$total += $product->sell_price * $product->qty;
$base_total += $product->sell_price * $product->qty;
if (isset($product->discount)) {
$total += $product->discount * $product->qty;
}
}
if (!$node->synchronized && $node->sell_price != $total) {
// Component products have changed their prices. Recalculate discounts
// to keep the same total.
$total = $base_total;
foreach ($node->products as $i => $product) {
$discount = ($node->sell_price - $base_total) * $product->sell_price / $base_total;
$total += $discount * $product->qty;
$form['base']['items'][$i]['discount']['#default_value'] = number_format($discount, 3, '.', '');
}
}
$form['base']['kit_total'] = array(
'#type' => 'uc_price',
'#title' => t('Total price'),
'#default_value' => $node->synchronized ? '' : $total,
'#description' => t('If this field is set, the discounts of the individual products will be recalculated to equal this value. Currently, the total sell price is %price.', array(
'%price' => uc_currency_format($total),
)),
'#empty_zero' => FALSE,
);
}
if (variable_get('uc_product_add_to_cart_qty', FALSE)) {
$form['base']['default_qty'] = array(
'#type' => 'uc_quantity',
'#title' => t('Default quantity to add to cart'),
'#default_value' => $node->default_qty,
'#description' => t('Use 0 to disable the quantity field next to the add to cart button.'),
'#weight' => 27,
'#allow_zero' => TRUE,
);
}
else {
$form['base']['default_qty'] = array(
'#type' => 'value',
'#value' => $node->default_qty,
);
}
$form['base']['ordering'] = array(
'#type' => 'weight',
'#title' => t('List position'),
'#description' => t("Specify a value to set this product's position in product lists.<br />Products in the same position will be sorted alphabetically."),
'#delta' => 25,
'#default_value' => $node->ordering,
'#weight' => 30,
);
// Disable all shipping related functionality.
$form['shipping']['#access'] = FALSE;
return $form;
}