| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Restrict the quantity on specified products so that only one may be purchased
|
| 6 |
* at a time.
|
| 7 |
*
|
| 8 |
* Sponsored by Flat World Knowledge - http://www.flatworldknowledge.com
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_product_feature().
|
| 13 |
*/
|
| 14 |
function uc_restrict_qty_product_feature() {
|
| 15 |
$features[] = array(
|
| 16 |
'id' => 'restrict_qty',
|
| 17 |
'title' => t('Restrict Qty.'),
|
| 18 |
'callback' => 'uc_restrict_qty_feature_form',
|
| 19 |
);
|
| 20 |
|
| 21 |
return $features;
|
| 22 |
}
|
| 23 |
|
| 24 |
// Display the settings form for the node access product feature.
|
| 25 |
function uc_restrict_qty_feature_form($node, $feature) {
|
| 26 |
if (empty($feature)) {
|
| 27 |
// It's not necessary to add more than one of these features at the moment.
|
| 28 |
if ($result = db_result(db_query("SELECT COUNT(*) FROM {uc_product_features} WHERE nid = %d AND fid = '%s'", $node->nid, 'restrict_qty'))) {
|
| 29 |
drupal_set_message(t('Adding more than one Restrict Qty. does nothing for now.'));
|
| 30 |
}
|
| 31 |
else {
|
| 32 |
$data = array(
|
| 33 |
'pfid' => db_next_id('{uc_product_features}_pfid'),
|
| 34 |
'nid' => $node->nid,
|
| 35 |
'fid' => 'restrict_qty',
|
| 36 |
'description' => t('Users may only purchase one of this product at a time.'),
|
| 37 |
);
|
| 38 |
drupal_goto(uc_product_feature_save($data));
|
| 39 |
}
|
| 40 |
}
|
| 41 |
else {
|
| 42 |
drupal_set_message(t('There are no settings to edit for Restrict Qty. features.'));
|
| 43 |
}
|
| 44 |
|
| 45 |
drupal_goto('node/'. $node->nid .'/edit/features');
|
| 46 |
}
|
| 47 |
|
| 48 |
/**
|
| 49 |
* Implementation of hook_add_to_cart_data().
|
| 50 |
*/
|
| 51 |
function uc_restrict_qty_add_to_cart_data($form_values) {
|
| 52 |
return array('restrict_qty' => uc_restrict_qty_count($form_values['nid']));
|
| 53 |
}
|
| 54 |
|
| 55 |
/**
|
| 56 |
* Implementation of hook_cart_item().
|
| 57 |
*/
|
| 58 |
function uc_restrict_qty_cart_item($op, &$item) {
|
| 59 |
if ($op == 'load') {
|
| 60 |
// If this item has a quantity restriction on it...
|
| 61 |
if ($item->data['restrict_qty'] > 0 && $item->qty > 1) {
|
| 62 |
// Reduce the quantity to 1 if necessary.
|
| 63 |
db_query("UPDATE {uc_cart_products} SET qty = 1 WHERE cart_id = '%s' AND nid = %d AND data = '%s'", uc_cart_get_id(), $item->nid, serialize($item->data));
|
| 64 |
$item->qty = 1;
|
| 65 |
}
|
| 66 |
}
|
| 67 |
}
|
| 68 |
|
| 69 |
/**
|
| 70 |
* Implementation of hook_form_alter().
|
| 71 |
*/
|
| 72 |
function uc_restrict_qty_form_alter($form_id, &$form) {
|
| 73 |
// Disable the appropriate Qty. fields on the cart view form.
|
| 74 |
if ($form_id == 'uc_cart_view_form') {
|
| 75 |
foreach ($form['items'] as $key => $value) {
|
| 76 |
$data = unserialize($value['data']['#value']);
|
| 77 |
|
| 78 |
// If this item has a quantity restriction on it...
|
| 79 |
if ($data['restrict_qty'] > 0) {
|
| 80 |
$form['items'][$key]['qty']['#theme'] = 'restrict_qty_field';
|
| 81 |
}
|
| 82 |
}
|
| 83 |
}
|
| 84 |
}
|
| 85 |
|
| 86 |
// Themes cart Qty. boxes so they can't be changed.
|
| 87 |
function theme_restrict_qty_field($element) {
|
| 88 |
return check_plain($element['#value']);
|
| 89 |
}
|
| 90 |
|
| 91 |
// Returns the number of restrict_qty features on a product node.
|
| 92 |
function uc_restrict_qty_count($nid) {
|
| 93 |
return db_result(db_query("SELECT COUNT(*) FROM {uc_product_features} WHERE nid = %d AND fid = '%s'", $nid, 'restrict_qty'));
|
| 94 |
}
|