/**
* 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.
*/ 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']);
// 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');
// If the product class has a default Variable Price product feature... if($data){ // Prepare the data as if it were from a form submission. $data = unserialize($data); $data['nid'] = $node->nid; $data['pfid'] = ''; $form_state = array('values' =>$data);
// Add the feature to the product by spoofing the normal form submission.
uc_varprice_feature_form_submit(array(), $form_state); } } }
/**
* Implements hook_uc_add_to_cart_data().
*/ function uc_varprice_uc_add_to_cart_data($form_values){ // Store the customer entered price in the product's data array. if(!empty($form_values['varprice'])){ returnarray('varprice' =>$form_values['varprice'], 'uniqid' =>uniqid()); } }
// If there is Variable Price data for this product... if(isset($data['varprice'])){ $message = '';
// Load the product feature data. $vp_data = uc_varprice_product_load($nid);
// Fail if the customer failed to enter a price. if(empty($data['varprice'])||$data['varprice'] == 0){ $message = t('You must specify a price.'); } // Fail if the customer entered a price lower than the minimum. elseif(!empty($vp_data->price_minimum)&&$data['varprice']<$vp_data->price_minimum){ $message = t('You must specify an amount greater than or equal to @price.', array('@price' => uc_currency_format($vp_data->price_minimum))); } // Fail if the customer entered a price above the maximum. elseif(!empty($vp_data->price_maximum)&&$data['varprice']>$vp_data->price_maximum){ $message = t('You must specify an amount less than or equal to @price.', array('@price' => uc_currency_format($vp_data->price_maximum))); }
// If an error message was set, return the failure notification. if(!empty($message)){ returnarray(array('success' =>FALSE, 'message' =>$message, 'silent' =>FALSE)); } } }
/**
* Implements hook_uc_product_alter().
*/ function uc_varprice_uc_product_alter($item){ // If the product has a variable price set... if(!empty($item->data['varprice'])){ // Update the cart item's price to the entered price value. $item->display_price = $item->price = $item->data['varprice']; } }
/**
* Implements hook_uc_product_feature().
*/ function uc_varprice_uc_product_feature(){ $features = array();
/**
* Adds settings to the product features form for UC Variable Price.
*/ function uc_varprice_settings(){ $form = array();
$form['uc_varprice_global_default'] = array( '#title' =>t('Global default price'), '#type' =>'textfield', '#size' =>8, '#description' =>t('The global default price for variable priced products; may be overridden at the product class or product level.'), '#default_value' =>variable_get('uc_varprice_global_default', '0'), '#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', '$') : '', );
return$form; }
/**
* Settings form for individual Variable Price product features.
*/ function uc_varprice_feature_form($form, &$form_state, $node, $feature){ // Add some helper JS to the form. drupal_add_js(drupal_get_path('module', 'uc_varprice') . '/uc_varprice.js');
// Load the Variable Price data specific to this product. if(!empty($feature)){ $varprice_feature = db_query('SELECT * FROM {uc_varprice_products} WHERE pfid = :pfid', array(':pfid' =>$feature['pfid']))->fetchObject(); } else{ $varprice_feature = new stdClass(); $varprice_feature->pfid = NULL; $varprice_feature->price_default = variable_get('uc_varprice_global_default', '0'); $varprice_feature->price_minimum = ''; $varprice_feature->price_maximum = ''; $varprice_feature->add_to_cart_title = t('Add to cart'); $varprice_feature->amount_title = t('Amount'); }
function _uc_varprice_feature_form($varprice_feature = FALSE){ $form = array();
$form['prices'] = array( '#type' =>'fieldset', '#title' =>t('Price settings'), ); $form['prices']['price_default'] = array( '#type' =>'textfield', '#title' =>t('Default price'), '#size' =>8, '#description' =>t('The default price for this variable priced products.'), '#default_value' =>$varprice_feature ? $varprice_feature->price_default : variable_get('uc_varprice_global_default', '0'), '#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', '$') : '', ); $form['prices']['price_minimum'] = array( '#type' =>'textfield', '#title' =>t('Minimum price'), '#size' =>8, '#description' =>t('The minimum price required for this product to be added to the cart.<br />Leave blank for no minimum.'), '#default_value' =>$varprice_feature ? $varprice_feature->price_minimum : '', '#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', '$') : '', ); $form['prices']['price_maximum'] = array( '#type' =>'textfield', '#title' =>t('Maximum price'), '#size' =>8, '#description' =>t('The maximum price allowed for this product to be added to the cart.<br />Leave blank for no maximum.'), '#default_value' =>$varprice_feature ? $varprice_feature->price_maximum : '', '#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', '$') : '', );
$form['titles'] = array( '#type' =>'fieldset', '#title' =>t('Add to cart form element titles'), '#description' =>t('Use these settings to adjust the normal titles of add to cart form elements for variable priced products.'), ); $form['titles']['override_add_to_cart_title'] = array( '#type' =>'checkbox', '#title' =>t('Override the title of the add to cart button.'), '#description' =>t('Defaults to <em>Add to cart</em>. For multilingual sites, use <a href="!url">String Overrides</a> instead.', array('!url' =>url('http://drupal.org/project/stringoverrides', array('absolute' =>TRUE)))), '#default_value' =>$varprice_feature ? !empty($varprice_feature->add_to_cart_title) : FALSE, '#attributes' =>array('class' =>array('override-checkbox')), ); $form['titles']['add_to_cart_title'] = array( '#type' =>'textfield', '#title' =>t('Add to cart button title'), '#default_value' =>$varprice_feature&&!empty($varprice_feature->add_to_cart_title) ? $varprice_feature->add_to_cart_title : t('Add to cart'), ); $form['titles']['override_amount_title'] = array( '#type' =>'checkbox', '#title' =>t('Override the title of the amount field for the price on the add to cart form.'), '#description' =>t('Defaults to <em>Amount</em>. For multilingual sites, use <a href="!url">String Overrides</a> instead.', array('!url' =>url('http://drupal.org/project/stringoverrides', array('absolute' =>TRUE)))), '#default_value' =>$varprice_feature ? !empty($varprice_feature->amount_title) : FALSE, '#attributes' =>array('class' =>array('override-checkbox')), ); $form['titles']['amount_title'] = array( '#type' =>'textfield', '#title' =>t('Amount field title'), '#default_value' =>$varprice_feature&&!empty($varprice_feature->amount_title) ? $varprice_feature->amount_title : t('Amount'), );
return$form; }
/**
* Creates the varprice feature in the database.
*/ function uc_varprice_feature_form_submit($form, &$form_state){ // Build an array of Variable Price data from the form submission. $vp_data = array( 'pfid' =>$form_state['values']['pfid'], 'price_default' =>$form_state['values']['price_default'], 'price_minimum' =>$form_state['values']['price_minimum'], 'price_maximum' =>$form_state['values']['price_maximum'], 'add_to_cart_title' =>$form_state['values']['override_add_to_cart_title'] ? $form_state['values']['add_to_cart_title'] : '', 'amount_title' =>$form_state['values']['override_amount_title'] ? $form_state['values']['amount_title'] : '', );
// Build the product feature description. $description = array( t('Customers can specify a price for this product.'), t('<b>Default price:</b> @price', array('@price' => uc_currency_format($vp_data['price_default']))), ); if(!empty($vp_data['price_minimum'])){ $description[] = t('<b>Minimum price:</b> @price', array('@price' => uc_currency_format($vp_data['price_minimum']))); } if(!empty($vp_data['price_maximum'])){ $description[] = t('<b>Maximum price:</b> @price', array('@price' => uc_currency_format($vp_data['price_maximum']))); } if(!empty($vp_data['add_to_cart_title'])){ $description[] = t('<b>Add to cart title:</b> @title', array('@title' =>$vp_data['add_to_cart_title'])); } if(!empty($vp_data['amount_title'])){ $description[] = t('<b>Amount field title:</b> @title', array('@title' =>$vp_data['amount_title'])); }
// Insert or update the data in the Variable Price products table. $key = array(); if($vpid = _uc_varprice_get_vpid($vp_data['pfid'])){ $key = 'vpid'; $vp_data['vpid'] = $vpid; }
// Load the product feature data for a given node. /**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/ function uc_varprice_product_load($nid){ returndb_query('SELECT vp.* FROM {uc_product_features} AS pf
LEFT JOIN {uc_varprice_products} AS vp ON pf.pfid = vp.pfid
WHERE pf.fid = :pf_fid AND pf.nid = :pf_nid', array(':pf_fid' =>'varprice', ':pf_nid' =>$nid))->fetchObject(); }
/**
* Theme the Qty. field for products in the shopping cart with variable prices.
*/ function theme_varprice_qty($variables){ $element = $variables['form']; return$element['#default_value']; }
/**
* Gets a uc_varprice id from a product feature id.
*/ function _uc_varprice_get_vpid($pfid){ returndb_query('SELECT vpid FROM {uc_varprice_products} WHERE pfid = :pfid', array(':pfid' =>$pfid))->fetchField(); }
Комментарии
/**
* @file
* Defines a product feature to turn any product into a variable priced product.
*/
/**
* Implements hook_theme().
*/
function uc_varprice_theme() {
return array(
'varprice_qty' => array(
'render element' => 'form',
),
);
}
/**
* 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.
*/
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;
}
}
/**
* Submit handler for the product class form for default Variable Price features.
*/
function uc_varprice_product_class_submit($form, &$form_state) {
if ($form_state['values']['default_varprice']) {
$data = array(
'price_default' => $form_state['values']['price_default'],
'price_minimum' => $form_state['values']['price_minimum'],
'price_maximum' => $form_state['values']['price_maximum'],
'override_add_to_cart_title' => $form_state['values']['override_add_to_cart_title'],
'add_to_cart_title' => $form_state['values']['override_add_to_cart_title'] ? $form_state['values']['add_to_cart_title'] : '',
'override_amount_title' => $form_state['values']['override_amount_title'],
'amount_title' => $form_state['values']['override_amount_title'] ? $form_state['values']['amount_title'] : '',
);
variable_set('ucvp_class_def_' . $form_state['values']['pcid'], serialize($data));
}
else {
variable_del('ucvp_class_def_' . $form_state['values']['pcid']);
}
}
/**
* Implements hook_node_view().
* Summary of alterations:
* 1) Removes price displays from variable priced product nodes.
* 2) Inserts Variable Price product feature on product node creation.
*/
function uc_varprice_node_view($node, $view_mode = 'full') {
// If this node has a variable price product feature...
if (db_query("SELECT pfid FROM {uc_product_features} WHERE fid = :fid AND nid = :nid", array(':fid' => 'varprice', ':nid' => $node->nid))->fetchField()) {
// Hide all the prices from display.
$node->content['cost']['#access'] = FALSE;
$node->content['list_price']['#access'] = FALSE;
$node->content['sell_price']['#access'] = FALSE;
$node->content['display_price']['#access'] = FALSE;
}
}
/**
* Implements hook_node_insert().
*/
function uc_varprice_node_insert($node) {
if (uc_product_is_product($node)) {
$data = variable_get('ucvp_class_def_' . $node->type, array());
// If the product class has a default Variable Price product feature...
if ($data) {
// Prepare the data as if it were from a form submission.
$data = unserialize($data);
$data['nid'] = $node->nid;
$data['pfid'] = '';
$form_state = array('values' => $data);
// Add the feature to the product by spoofing the normal form submission.
uc_varprice_feature_form_submit(array(), $form_state);
}
}
}
/**
* Implements hook_uc_add_to_cart_data().
*/
function uc_varprice_uc_add_to_cart_data($form_values) {
// Store the customer entered price in the product's data array.
if (!empty($form_values['varprice'])) {
return array('varprice' => $form_values['varprice'], 'uniqid' => uniqid());
}
}
/**
* Implements hook_uc_add_to_cart().
*/
function uc_varprice_uc_add_to_cart($nid, $qty, $data) {
$result = array();
// If there is Variable Price data for this product...
if (isset($data['varprice'])) {
$message = '';
// Load the product feature data.
$vp_data = uc_varprice_product_load($nid);
// Fail if the customer failed to enter a price.
if (empty($data['varprice']) || $data['varprice'] == 0) {
$message = t('You must specify a price.');
}
// Fail if the customer entered a price lower than the minimum.
elseif (!empty($vp_data->price_minimum) && $data['varprice'] < $vp_data->price_minimum) {
$message = t('You must specify an amount greater than or equal to @price.', array('@price' => uc_currency_format($vp_data->price_minimum)));
}
// Fail if the customer entered a price above the maximum.
elseif (!empty($vp_data->price_maximum) && $data['varprice'] > $vp_data->price_maximum) {
$message = t('You must specify an amount less than or equal to @price.', array('@price' => uc_currency_format($vp_data->price_maximum)));
}
// If an error message was set, return the failure notification.
if (!empty($message)) {
return array(array('success' => FALSE, 'message' => $message, 'silent' => FALSE));
}
}
}
/**
* Implements hook_uc_product_alter().
*/
function uc_varprice_uc_product_alter($item) {
// If the product has a variable price set...
if (!empty($item->data['varprice'])) {
// Update the cart item's price to the entered price value.
$item->display_price = $item->price = $item->data['varprice'];
}
}
/**
* Implements hook_uc_product_feature().
*/
function uc_varprice_uc_product_feature() {
$features = array();
$features[] = array(
'id' => 'varprice',
'title' => t('Variable price'),
'callback' => 'uc_varprice_feature_form',
'delete' => 'uc_varprice_feature_delete',
'settings' => 'uc_varprice_settings',
'multiple' => FALSE,
);
return $features;
}
/**
* Adds settings to the product features form for UC Variable Price.
*/
function uc_varprice_settings() {
$form = array();
$form['uc_varprice_global_default'] = array(
'#title' => t('Global default price'),
'#type' => 'textfield',
'#size' => 8,
'#description' => t('The global default price for variable priced products; may be overridden at the product class or product level.'),
'#default_value' => variable_get('uc_varprice_global_default', '0'),
'#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', '$') : '',
);
return $form;
}
/**
* Settings form for individual Variable Price product features.
*/
function uc_varprice_feature_form($form, &$form_state, $node, $feature) {
// Add some helper JS to the form.
drupal_add_js(drupal_get_path('module', 'uc_varprice') . '/uc_varprice.js');
// Load the Variable Price data specific to this product.
if (!empty($feature)) {
$varprice_feature = db_query('SELECT * FROM {uc_varprice_products} WHERE pfid = :pfid', array(':pfid' => $feature['pfid']))->fetchObject();
}
else {
$varprice_feature = new stdClass();
$varprice_feature->pfid = NULL;
$varprice_feature->price_default = variable_get('uc_varprice_global_default', '0');
$varprice_feature->price_minimum = '';
$varprice_feature->price_maximum = '';
$varprice_feature->add_to_cart_title = t('Add to cart');
$varprice_feature->amount_title = t('Amount');
}
$form['nid'] = array(
'#type' => 'value',
'#value' => $node->nid,
);
$form['pfid'] = array(
'#type' => 'value',
'#value' => $varprice_feature ? $varprice_feature->pfid : '',
);
$form += _uc_varprice_feature_form($varprice_feature);
return $form;
}
function _uc_varprice_feature_form($varprice_feature = FALSE) {
$form = array();
$form['prices'] = array(
'#type' => 'fieldset',
'#title' => t('Price settings'),
);
$form['prices']['price_default'] = array(
'#type' => 'textfield',
'#title' => t('Default price'),
'#size' => 8,
'#description' => t('The default price for this variable priced products.'),
'#default_value' => $varprice_feature ? $varprice_feature->price_default : variable_get('uc_varprice_global_default', '0'),
'#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', '$') : '',
);
$form['prices']['price_minimum'] = array(
'#type' => 'textfield',
'#title' => t('Minimum price'),
'#size' => 8,
'#description' => t('The minimum price required for this product to be added to the cart.<br />Leave blank for no minimum.'),
'#default_value' => $varprice_feature ? $varprice_feature->price_minimum : '',
'#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', '$') : '',
);
$form['prices']['price_maximum'] = array(
'#type' => 'textfield',
'#title' => t('Maximum price'),
'#size' => 8,
'#description' => t('The maximum price allowed for this product to be added to the cart.<br />Leave blank for no maximum.'),
'#default_value' => $varprice_feature ? $varprice_feature->price_maximum : '',
'#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', '$') : '',
);
$form['titles'] = array(
'#type' => 'fieldset',
'#title' => t('Add to cart form element titles'),
'#description' => t('Use these settings to adjust the normal titles of add to cart form elements for variable priced products.'),
);
$form['titles']['override_add_to_cart_title'] = array(
'#type' => 'checkbox',
'#title' => t('Override the title of the add to cart button.'),
'#description' => t('Defaults to <em>Add to cart</em>. For multilingual sites, use <a href="!url">String Overrides</a> instead.', array('!url' => url('http://drupal.org/project/stringoverrides', array('absolute' => TRUE)))),
'#default_value' => $varprice_feature ? !empty($varprice_feature->add_to_cart_title) : FALSE,
'#attributes' => array('class' => array('override-checkbox')),
);
$form['titles']['add_to_cart_title'] = array(
'#type' => 'textfield',
'#title' => t('Add to cart button title'),
'#default_value' => $varprice_feature && !empty($varprice_feature->add_to_cart_title) ? $varprice_feature->add_to_cart_title : t('Add to cart'),
);
$form['titles']['override_amount_title'] = array(
'#type' => 'checkbox',
'#title' => t('Override the title of the amount field for the price on the add to cart form.'),
'#description' => t('Defaults to <em>Amount</em>. For multilingual sites, use <a href="!url">String Overrides</a> instead.', array('!url' => url('http://drupal.org/project/stringoverrides', array('absolute' => TRUE)))),
'#default_value' => $varprice_feature ? !empty($varprice_feature->amount_title) : FALSE,
'#attributes' => array('class' => array('override-checkbox')),
);
$form['titles']['amount_title'] = array(
'#type' => 'textfield',
'#title' => t('Amount field title'),
'#default_value' => $varprice_feature && !empty($varprice_feature->amount_title) ? $varprice_feature->amount_title : t('Amount'),
);
return $form;
}
/**
* Creates the varprice feature in the database.
*/
function uc_varprice_feature_form_submit($form, &$form_state) {
// Build an array of Variable Price data from the form submission.
$vp_data = array(
'pfid' => $form_state['values']['pfid'],
'price_default' => $form_state['values']['price_default'],
'price_minimum' => $form_state['values']['price_minimum'],
'price_maximum' => $form_state['values']['price_maximum'],
'add_to_cart_title' => $form_state['values']['override_add_to_cart_title'] ? $form_state['values']['add_to_cart_title'] : '',
'amount_title' => $form_state['values']['override_amount_title'] ? $form_state['values']['amount_title'] : '',
);
// Build the product feature description.
$description = array(
t('Customers can specify a price for this product.'),
t('<b>Default price:</b> @price', array('@price' => uc_currency_format($vp_data['price_default']))),
);
if (!empty($vp_data['price_minimum'])) {
$description[] = t('<b>Minimum price:</b> @price', array('@price' => uc_currency_format($vp_data['price_minimum'])));
}
if (!empty($vp_data['price_maximum'])) {
$description[] = t('<b>Maximum price:</b> @price', array('@price' => uc_currency_format($vp_data['price_maximum'])));
}
if (!empty($vp_data['add_to_cart_title'])) {
$description[] = t('<b>Add to cart title:</b> @title', array('@title' => $vp_data['add_to_cart_title']));
}
if (!empty($vp_data['amount_title'])) {
$description[] = t('<b>Amount field title:</b> @title', array('@title' => $vp_data['amount_title']));
}
// Save the basic product feature data.
$data = array(
'pfid' => $vp_data['pfid'],
'nid' => $form_state['values']['nid'],
'fid' => 'varprice',
'description' => implode('<br />', $description),
);
$form_state['redirect'] = uc_product_feature_save($data);
$vp_data['pfid'] = $data['pfid'];
// Insert or update the data in the Variable Price products table.
$key = array();
if ($vpid = _uc_varprice_get_vpid($vp_data['pfid'])) {
$key = 'vpid';
$vp_data['vpid'] = $vpid;
}
drupal_write_record('uc_varprice_products', $vp_data, $key);
}
/**
* Variable Price product feature delete function.
*/
function uc_varprice_feature_delete($feature) {
db_delete('uc_varprice_products')
->condition('pfid', $feature['pfid'])
->execute();
}
// Load the product feature data for a given node.
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function uc_varprice_product_load($nid) {
return db_query('SELECT vp.* FROM {uc_product_features} AS pf
LEFT JOIN {uc_varprice_products} AS vp ON pf.pfid = vp.pfid
WHERE pf.fid = :pf_fid AND pf.nid = :pf_nid',
array(':pf_fid' => 'varprice', ':pf_nid' => $nid))->fetchObject();
}
/**
* Theme the Qty. field for products in the shopping cart with variable prices.
*/
function theme_varprice_qty($variables) {
$element = $variables['form'];
return $element['#default_value'];
}
/**
* Gets a uc_varprice id from a product feature id.
*/
function _uc_varprice_get_vpid($pfid) {
return db_query('SELECT vpid FROM {uc_varprice_products} WHERE pfid = :pfid', array(':pfid' => $pfid))->fetchField();
}