| 1 |
<?php |
<?php |
| 2 |
// $Id: uc_restrict_qty.module,v 1.1.4.3 2008/12/04 22:46:39 mrfelton Exp $ |
// $Id$ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 6 |
* Restrict the quantity on specified products so that only one may be purchased |
* Restrict the quantity on specified products so that only one may be purchased |
| 7 |
* at a time. |
* at a time. |
|
* |
|
|
* Sponsored by Flat World Knowledge - http://www.flatworldknowledge.com |
|
|
* And Kirkdesigns - http://www.kirkdesigns.co.uk |
|
| 8 |
*/ |
*/ |
| 9 |
|
|
|
/** |
|
|
* Implementation of hook_help(). |
|
|
*/ |
|
|
function uc_restrict_qty_help($path, $arg) { |
|
|
switch ($path) { |
|
|
case 'admin/store/settings/restrict_qty': |
|
|
return t('You may use this page to set a global limit on the number of items that a shopping cart may contain.'); |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* Implementation of hook_menu(). |
|
|
*/ |
|
|
function uc_restrict_qty_menu() { |
|
|
$items['admin/store/settings/restrict_qty'] = array( |
|
|
'title' => 'Restrict Qty', |
|
|
'description' => 'Adjust settings for Ubercart Restrict Qty feature.', |
|
|
'page callback' => 'drupal_get_form', |
|
|
'page arguments' => array('uc_restrict_qty_admin_form'), |
|
|
'access arguments' => array('administer products'), |
|
|
'type' => MENU_NORMAL_ITEM, |
|
|
'weight' => 1, |
|
|
); |
|
|
return $items; |
|
|
} |
|
| 10 |
|
|
| 11 |
/** |
/** |
| 12 |
* Implementation of hook_theme(). |
* Implementation of hook_theme(). |
| 27 |
'id' => 'restrict_qty', |
'id' => 'restrict_qty', |
| 28 |
'title' => t('Restrict Qty'), |
'title' => t('Restrict Qty'), |
| 29 |
'callback' => 'uc_restrict_qty_feature_form', |
'callback' => 'uc_restrict_qty_feature_form', |
| 30 |
|
'settings' => 'uc_restrict_qty_settings', |
| 31 |
); |
); |
| 32 |
return $features; |
return $features; |
| 33 |
} |
} |
| 34 |
|
|
| 35 |
|
// Adds settings to the product features form for UC Restrict Qty. |
| 36 |
|
function uc_restrict_qty_settings() { |
| 37 |
|
$form['#description'] = t('This feature is limited in scope to preventing a user from adding different products to the cart. This does not restrict the quantity of products in the cart if updated after being added, so this feature is best used on sites where all products have a restrict quantity feature on them.'); |
| 38 |
|
|
| 39 |
|
$form['uc_restrict_qty_global'] = array( |
| 40 |
|
'#title' => t('Global limit'), |
| 41 |
|
'#type' => 'textfield', |
| 42 |
|
'#size' => 5, |
| 43 |
|
'#maxlength' => 5, |
| 44 |
|
'#description' => t('The number of different products that can be added to a cart. Set to 0 for unlimited.'), |
| 45 |
|
'#default_value' => variable_get('uc_restrict_qty_global', '0'), |
| 46 |
|
); |
| 47 |
|
|
| 48 |
|
return $form; |
| 49 |
|
} |
| 50 |
|
|
| 51 |
|
// Validates the textfield on the product features settings form. |
| 52 |
|
function uc_restrict_qty_settings_validate($form, &$form_state) { |
| 53 |
|
if (!is_numeric($form_state['values']['uc_restrict_qty_global']) || $form_state['values']['uc_restrict_qty_global'] < 0) { |
| 54 |
|
form_set_error('uc_restrict_qty_global', t('You must enter 0 or a positive number value.')); |
| 55 |
|
} |
| 56 |
|
} |
| 57 |
|
|
| 58 |
// Builds the form to display for adding or editing a the restricted quantity feature. |
// Builds the form to display for adding or editing a the restricted quantity feature. |
| 59 |
function uc_restrict_qty_feature_form($form_state, $node, $feature) { |
function uc_restrict_qty_feature_form($form_state, $node, $feature) { |
| 60 |
if (empty($feature)) { |
if (empty($feature)) { |
| 82 |
* Implementation of hook_add_to_cart(). |
* Implementation of hook_add_to_cart(). |
| 83 |
*/ |
*/ |
| 84 |
function uc_restrict_qty_add_to_cart($nid, $qty, $data) { |
function uc_restrict_qty_add_to_cart($nid, $qty, $data) { |
| 85 |
if (variable_get('uc_restrict_qty_global', 0)) { |
$limit = variable_get('uc_restrict_qty_global', 0); |
| 86 |
$cart_items = uc_cart_get_contents(uc_cart_get_id()); |
|
| 87 |
if (sizeof($cart_items) >= variable_get('uc_restrict_qty_global', 0)) { |
// If a global restriction on the number of items has been made. |
| 88 |
|
if ($limit > 0) { |
| 89 |
|
if (count(uc_cart_get_contents()) >= $limit) { |
| 90 |
|
$message = format_plural($limit, 'Sorry, you may only have 1 item in your cart. You must checkout or remove the item in your cart before adding a different item.', |
| 91 |
|
'Sorry, you may only have a total of @count items in your cart. You must checkout or remove items from your cart before adding others.'); |
| 92 |
|
|
| 93 |
$result[] = array( |
$result[] = array( |
| 94 |
'success' => FALSE, |
'success' => FALSE, |
| 95 |
'message' => t('Sorry, you may only have @qty in your cart. Please remove something from your cart first.', |
'message' => $message, |
|
array( |
|
|
'@qty' => format_plural(variable_get('uc_restrict_qty_global', 0), '1 item', '@count items'), |
|
|
)), |
|
| 96 |
); |
); |
| 97 |
} |
} |
| 98 |
} |
} |
| 113 |
if ($op == 'load') { |
if ($op == 'load') { |
| 114 |
// If this item has a quantity restriction on it... |
// If this item has a quantity restriction on it... |
| 115 |
if ($item->data['restrict_qty'] > 0 && $item->qty > 1) { |
if ($item->data['restrict_qty'] > 0 && $item->qty > 1) { |
| 116 |
db_query("UPDATE {uc_cart_products} SET qty = 1, changed = %d WHERE nid = %d AND cart_id = '%s' AND data = '%s'", time(), $item->nid, uc_cart_get_id(), serialize($item->data)); |
// Reduce the quantity to 1 if necessary. |
| 117 |
|
db_query("UPDATE {uc_cart_products} SET qty = 1, changed = %d WHERE cart_id = '%s' AND nid = %d AND data = '%s'", time(), uc_cart_get_id(), $item->nid, serialize($item->data)); |
| 118 |
$item->qty = 1; |
$item->qty = 1; |
| 119 |
} |
} |
|
// If this item quantity has exceeded the global restriction... |
|
|
elseif (variable_get('uc_restrict_qty_global', 0) && $item->qty >= variable_get('uc_restrict_qty_global', 0)) { |
|
|
db_query("UPDATE {uc_cart_products} SET qty = %d, changed = %d WHERE nid = %d AND cart_id = '%s' AND data = '%s'", variable_get('uc_restrict_qty_global', 0), time(), $item->nid, uc_cart_get_id(), serialize($item->data)); |
|
|
$item->qty = variable_get('uc_restrict_qty_global', 0); |
|
|
} |
|
| 120 |
} |
} |
| 121 |
} |
} |
| 122 |
|
|
| 126 |
function uc_restrict_qty_form_alter(&$form, &$form_state, $form_id) { |
function uc_restrict_qty_form_alter(&$form, &$form_state, $form_id) { |
| 127 |
// Disable the appropriate Qty. fields on the cart view form. |
// Disable the appropriate Qty. fields on the cart view form. |
| 128 |
if ($form_id == 'uc_cart_view_form') { |
if ($form_id == 'uc_cart_view_form') { |
| 129 |
foreach ($form['#parameters'][2] as $key => $item) { |
for ($i = 0, $j = count(uc_cart_get_contents()); $i < $j; $i++) { |
| 130 |
// If this item has a quantity restriction feature on it... |
$data = unserialize($form['items'][$i]['data']['#value']); |
| 131 |
if ($item->data['restrict_qty'] > 0) { |
|
| 132 |
$form['items'][$key]['qty']['#attributes']['readonly'] = 'readonly'; |
// If this item has a quantity restriction on it... |
| 133 |
//$form['items'][$key]['qty']['#theme'] = 'restrict_qty_field'; |
if ($data['restrict_qty'] > 0) { |
| 134 |
|
$form['items'][$i]['qty']['#type'] = 'value'; |
| 135 |
|
$form['items'][$i]['qty']['#theme'] = 'restrict_qty_field'; |
| 136 |
} |
} |
| 137 |
} |
} |
| 138 |
} |
} |
|
} |
|
| 139 |
|
|
| 140 |
// builds the admin settings form. |
if ($form_id == 'uc_product_feature_settings_form') { |
| 141 |
function uc_restrict_qty_admin_form() { |
$form['#validate'][] = 'uc_restrict_qty_settings_validate'; |
|
$form['uc_restrict_qty_global'] = array( |
|
|
'#title' => t('Global limit'), |
|
|
'#type' => 'textfield', |
|
|
'#size' => 5, |
|
|
'#maxlength' => 5, |
|
|
'#description' => t('The total number of products that can be added to a cart. Set to 0 for unlimited.'), |
|
|
'#default_value' => variable_get('uc_restrict_qty_global', 0), |
|
|
); |
|
|
$form['#validate'][] = 'uc_restrict_qty_admin_form_validate'; |
|
|
return system_settings_form($form); |
|
|
} |
|
|
|
|
|
// Ensure user has entered a sensible value. |
|
|
function uc_restrict_qty_admin_form_validate(&$form, $form_state) { |
|
|
if (!is_numeric($form_state['values']['uc_restrict_qty_global'])) { |
|
|
form_set_error('uc_restrict_qty_global', t('Please enter a numeric value.')); |
|
|
} |
|
|
if ($form_state['values']['uc_restrict_qty_global'] < 0) { |
|
|
form_set_error('uc_restrict_qty_global', t('Please enter a positive integer.')); |
|
| 142 |
} |
} |
| 143 |
} |
} |
| 144 |
|
|