|
|
|
@@ -2,24 +2,24 @@
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* Restrict the quantity on specified products so that only specified quantity may be purchased
|
|
|
|
|
* at a time.
|
|
|
|
|
* Restrict the quantity on specified products so that only specified quantity
|
|
|
|
|
* may be purchased at a time.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implementation of hook_theme().
|
|
|
|
|
* Implements hook_theme().
|
|
|
|
|
*/
|
|
|
|
|
function uc_restrict_qty_theme() {
|
|
|
|
|
return array(
|
|
|
|
|
'restrict_qty_field' => array(
|
|
|
|
|
'render element' => 'form',
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
return array(
|
|
|
|
|
'restrict_qty_field' => array(
|
|
|
|
|
'render element' => 'form',
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implementation of hook_uc_product_feature().
|
|
|
|
|
* Implements hook_uc_product_feature().
|
|
|
|
|
*/
|
|
|
|
|
function uc_restrict_qty_uc_product_feature() {
|
|
|
|
|
$features[] = array(
|
|
|
|
@@ -32,8 +32,10 @@ function uc_restrict_qty_uc_product_feature() {
|
|
|
|
|
return $features;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Adds settings to the product features form for UC Restrict Qty.
|
|
|
|
|
function uc_restrict_qty_settings() {
|
|
|
|
|
/**
|
|
|
|
|
* Adds settings to the product features form for UC Restrict Qty.
|
|
|
|
|
*/
|
|
|
|
|
function uc_restrict_qty_settings($form, &$form_state) {
|
|
|
|
|
$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.');
|
|
|
|
|
|
|
|
|
|
$form['uc_restrict_qty_global'] = array(
|
|
|
|
@@ -46,10 +48,10 @@ function uc_restrict_qty_settings() {
|
|
|
|
|
);
|
|
|
|
|
$form['uc_restrict_qty_global_replace'] = array(
|
|
|
|
|
'#title' => t('Replace Contents'),
|
|
|
|
|
'#type' => 'checkbox',
|
|
|
|
|
'#type' => 'checkbox',
|
|
|
|
|
'#description' => t('Enabling this feature will cause the users cart to be emptied if they add more items than the Global Limit set above. This is best used when you offer mutually exclusive content (such as subscription services) where purchasing more then one product is not a valid option.'),
|
|
|
|
|
'#default_value' => variable_get('uc_restrict_qty_global_replace', FALSE),
|
|
|
|
|
);
|
|
|
|
|
);
|
|
|
|
|
$form['defaults'] = array(
|
|
|
|
|
'#title' => t('Defaults'),
|
|
|
|
|
'#type' => 'fieldset',
|
|
|
|
@@ -73,7 +75,9 @@ function uc_restrict_qty_settings() {
|
|
|
|
|
return $form;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validates the textfield on the product features settings form.
|
|
|
|
|
/**
|
|
|
|
|
* Validates the textfield on the product features settings form.
|
|
|
|
|
*/
|
|
|
|
|
function uc_restrict_qty_settings_validate($form, &$form_state) {
|
|
|
|
|
if (!is_numeric($form_state['values']['uc_restrict_qty_global']) || $form_state['values']['uc_restrict_qty_global'] < 0) {
|
|
|
|
|
form_set_error('uc_restrict_qty_global', t('You must enter 0 or a positive number value.'));
|
|
|
|
@@ -83,9 +87,11 @@ function uc_restrict_qty_settings_validate($form, &$form_state) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
*/
|
|
|
|
|
function uc_restrict_qty_feature_form($form, &$form_state, $node, $feature) {
|
|
|
|
|
$models = uc_product_get_models($node->nid);
|
|
|
|
|
$models = uc_product_get_models($node->nid, FALSE);
|
|
|
|
|
if (!empty($feature)) {
|
|
|
|
|
$product_qty = db_query("SELECT * FROM {uc_restrict_qty_products} WHERE pfid = :pfid", array(':pfid' => $feature['pfid']))->fetchObject();
|
|
|
|
|
|
|
|
|
@@ -102,15 +108,6 @@ function uc_restrict_qty_feature_form($form, &$form_state, $node, $feature) {
|
|
|
|
|
'#value' => $product_qty->rqpid,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
/* if ($result = db_result(db_query("SELECT COUNT(*) FROM {uc_product_features} WHERE nid = %d AND fid = '%s'", $node->nid, 'restrict_qty'))) {
|
|
|
|
|
drupal_set_message(t('Adding more than one Restrict Qty. does nothing for now.', 'error'));
|
|
|
|
|
$form['no_roles'] = array(
|
|
|
|
|
'#value' => t('You need to <a href="!url">create new roles</a> before any can be added as product features.', array('!url' => url('admin/user/roles', array('query' => 'destination=admin/store/settings/products/edit/features')))),
|
|
|
|
|
'#prefix' => '<p>',
|
|
|
|
|
'#suffix' => '</p>',
|
|
|
|
|
);
|
|
|
|
|
return $form;
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
$form['nid'] = array(
|
|
|
|
|
'#type' => 'value',
|
|
|
|
@@ -140,14 +137,16 @@ function uc_restrict_qty_feature_form($form, &$form_state, $node, $feature) {
|
|
|
|
|
return uc_product_feature_form($form, $form_state, $node, $feature);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validates the textfield on the product features settings form.
|
|
|
|
|
/**
|
|
|
|
|
* Validates the textfield on the product node feature settings form.
|
|
|
|
|
*/
|
|
|
|
|
function uc_restrict_qty_feature_form_validate($form, &$form_state) {
|
|
|
|
|
// Invalid quantity?
|
|
|
|
|
// Check for invalid quantity.
|
|
|
|
|
if (!is_numeric($form_state['values']['quantity']) || $form_state['values']['quantity'] < 0) {
|
|
|
|
|
form_set_error('uc_restrict_qty_product_qty', t('You must enter 0 or a positive integer value.'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This feature is already set on this SKU?
|
|
|
|
|
// Check if this feature is already set on this SKU.
|
|
|
|
|
$product_roles = db_query("SELECT * FROM {uc_restrict_qty_products} WHERE nid = :nid AND model = :model", array(
|
|
|
|
|
':nid' => $form_state['values']['nid'],
|
|
|
|
|
':model' => $form_state['values']['model'],
|
|
|
|
@@ -161,7 +160,7 @@ function uc_restrict_qty_feature_form_validate($form, &$form_state) {
|
|
|
|
|
/**
|
|
|
|
|
* Form submit handler for the roles feature form.
|
|
|
|
|
*/
|
|
|
|
|
function uc_restrict_qty_feature_form_submit($form, &$form_state) {dpm($form_state);
|
|
|
|
|
function uc_restrict_qty_feature_form_submit($form, &$form_state) {
|
|
|
|
|
$product_qty = array(
|
|
|
|
|
'rqpid' => isset($form_state['values']['rqpid']) ? $form_state['values']['rqpid'] : NULL,
|
|
|
|
|
'pfid' => isset($form_state['values']['pfid']) ? $form_state['values']['pfid'] : NULL,
|
|
|
|
@@ -171,9 +170,9 @@ function uc_restrict_qty_feature_form_submit($form, &$form_state) {dpm($form_sta
|
|
|
|
|
'lifetime' => $form_state['values']['lifetime'],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$description = '<strong>'. t('SKU') .':</strong> '. (empty($product_qty['model']) ? t('Any') : $product_qty['model']) .'<br/>';
|
|
|
|
|
$description .= '<strong>'. t('Quantity restriction') .':</strong> '. $product_qty['qty'] .'<br/>';
|
|
|
|
|
$description .= '<strong>'. t('Type') .':</strong> '. ($product_qty['lifetime'] ? t('Lifetime') : t('Cart max.')) .'<br/>';
|
|
|
|
|
$description = '<strong>' . t('SKU') . ':</strong>' . (empty($product_qty['model']) ? t('Any') : $product_qty['model']) . '<br/>';
|
|
|
|
|
$description .= '<strong>' . t('Quantity restriction') . ':</strong>' . $product_qty['qty'] . '<br/>';
|
|
|
|
|
$description .= '<strong>' . t('Type') . ':</strong>' . ($product_qty['lifetime'] ? t('Lifetime') : t('Cart max.')) . '<br/>';
|
|
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
|
'nid' => $product_qty['nid'],
|
|
|
|
@@ -187,10 +186,10 @@ function uc_restrict_qty_feature_form_submit($form, &$form_state) {dpm($form_sta
|
|
|
|
|
|
|
|
|
|
$form_state['redirect'] = uc_product_feature_save($data);
|
|
|
|
|
|
|
|
|
|
$key = array();
|
|
|
|
|
if ($product_qty['rqpid']) {
|
|
|
|
|
$key[] = 'rqpid';
|
|
|
|
|
}
|
|
|
|
|
$key = array();
|
|
|
|
|
if ($product_qty['rqpid']) {
|
|
|
|
|
$key[] = 'rqpid';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert or update uc_file_product table
|
|
|
|
|
if (empty($product_qty['pfid'])) {
|
|
|
|
@@ -201,22 +200,23 @@ function uc_restrict_qty_feature_form_submit($form, &$form_state) {dpm($form_sta
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implementation of hook_add_to_cart().
|
|
|
|
|
* Implements hook_uc_add_to_cart().
|
|
|
|
|
*/
|
|
|
|
|
function uc_restrict_qty_add_to_cart($nid, $qty, $data) {
|
|
|
|
|
function uc_restrict_qty_uc_add_to_cart($nid, $qty, $data) {
|
|
|
|
|
$limit = variable_get('uc_restrict_qty_global', 0);
|
|
|
|
|
$replaceCart = variable_get('uc_restrict_qty_global_replace', FALSE);
|
|
|
|
|
$replacecart = variable_get('uc_restrict_qty_global_replace', FALSE);
|
|
|
|
|
$result = NULL; // added by Jeff
|
|
|
|
|
|
|
|
|
|
// If a global restriction on the number of items has been made.
|
|
|
|
|
if ($limit > 0) {
|
|
|
|
|
if (count(uc_cart_get_contents()) >= $limit) {
|
|
|
|
|
if($replaceCart) {
|
|
|
|
|
db_query("DELETE FROM {uc_cart_products} WHERE cart_id = '%s'", uc_cart_get_id());
|
|
|
|
|
if ($replacecart) {
|
|
|
|
|
db_query("DELETE FROM {uc_cart_products} WHERE cart_id = :cart", array(':cart' => uc_cart_get_id()));
|
|
|
|
|
$result[] = array('success' => TRUE);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$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.',
|
|
|
|
|
'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.');
|
|
|
|
|
'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.');
|
|
|
|
|
|
|
|
|
|
$result[] = array(
|
|
|
|
|
'success' => FALSE,
|
|
|
|
@@ -232,8 +232,8 @@ function uc_restrict_qty_add_to_cart($nid, $qty, $data) {
|
|
|
|
|
$message = t('Sorry, you have reached the quantity limit for this product. You can not order more items of this product.');
|
|
|
|
|
|
|
|
|
|
$result[] = array(
|
|
|
|
|
'success' => FALSE,
|
|
|
|
|
'message' => $message,
|
|
|
|
|
'success' => FALSE,
|
|
|
|
|
'message' => $message,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -241,33 +241,41 @@ function uc_restrict_qty_add_to_cart($nid, $qty, $data) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implementation of hook_add_to_cart_data().
|
|
|
|
|
* Implements hook_add_to_cart_data().
|
|
|
|
|
* @param $form_values
|
|
|
|
|
* The values submitted to the Add to Cart form.
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
* An array of data to be merged into the item added to the cart.
|
|
|
|
|
*/
|
|
|
|
|
function uc_restrict_qty_uc_add_to_cart_data($form_values) {
|
|
|
|
|
return array('restrict_qty' => uc_restrict_qty_count($form_values));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implementation of hook_cart_item().
|
|
|
|
|
* Implements of hook_uc_cart_item_update().
|
|
|
|
|
*/
|
|
|
|
|
function uc_restrict_qty_uc_cart_item($op, &$item) {
|
|
|
|
|
if ($op == 'load') {
|
|
|
|
|
// If this item has a quantity restriction on it...
|
|
|
|
|
if ($item->data['restrict_qty']['qty'] > 0) {
|
|
|
|
|
$is_lifetime = isset($item->data['restrict_qty']['lifetime']) && $item->data['restrict_qty']['lifetime'];
|
|
|
|
|
$restrict_qty = $is_lifetime ? $item->data['restrict_qty']['rest'] : $item->data['restrict_qty']['qty'];
|
|
|
|
|
if ($item->qty > $restrict_qty) {
|
|
|
|
|
// Reduce the quantity to 1 if necessary.
|
|
|
|
|
$item->qty = $restrict_qty;
|
|
|
|
|
db_query("UPDATE {uc_cart_products} SET qty = %d, changed = %d WHERE cart_id = '%s' AND nid = %d AND data = '%s'", $restrict_qty, time(), uc_cart_get_id(), $item->nid, serialize($item->data));
|
|
|
|
|
drupal_set_message(format_plural($restrict_qty, 'You may only add 1 !item to your cart. Quantity has been restricted.', 'You may only add @count !item to your cart. Quantity has been restricted.', array('!item' => $item->title)), 'warning');
|
|
|
|
|
}
|
|
|
|
|
function uc_restrict_qty_uc_cart_item_update($item) {
|
|
|
|
|
// Check if this item has a quantity restriction on it.
|
|
|
|
|
if (isset($item->data['restrict_qty']['qty']) && $item->data['restrict_qty']['qty'] > 0) {
|
|
|
|
|
$is_lifetime = isset($item->data['restrict_qty']['lifetime']) && $item->data['restrict_qty']['lifetime'];
|
|
|
|
|
$restrict_qty = $is_lifetime ? $item->data['restrict_qty']['rest'] : $item->data['restrict_qty']['qty'];
|
|
|
|
|
if ($item->qty > $restrict_qty) {
|
|
|
|
|
// Reduce the quantity to 1 if necessary.
|
|
|
|
|
$item->qty = $restrict_qty;
|
|
|
|
|
db_update('uc_cart_products')
|
|
|
|
|
->fields(array('qty' => $restrict_qty, 'changed' => time()))
|
|
|
|
|
->condition('cart_id', uc_cart_get_id())
|
|
|
|
|
->condition('nid', $item->nid)
|
|
|
|
|
->condition('data', serialize($item->data))
|
|
|
|
|
->execute();
|
|
|
|
|
drupal_set_message(format_plural($restrict_qty, 'You may only add 1 !item to your cart. Quantity has been restricted.', 'You may only add @count !item to your cart. Quantity has been restricted.', array('!item' => $item->title)), 'warning');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implementation of hook_form_alter().
|
|
|
|
|
* Implements hook_form_alter().
|
|
|
|
|
*/
|
|
|
|
|
function uc_restrict_qty_form_alter(&$form, &$form_state, $form_id) {
|
|
|
|
|
// Disable the appropriate Qty. fields on the cart view form.
|
|
|
|
@@ -275,12 +283,14 @@ function uc_restrict_qty_form_alter(&$form, &$form_state, $form_id) {
|
|
|
|
|
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...
|
|
|
|
|
$is_lifetime = isset($data['restrict_qty']['lifetime']) && $data['restrict_qty']['lifetime'];
|
|
|
|
|
$restrict_qty = $is_lifetime ? $data['restrict_qty']['rest'] : $data['restrict_qty']['qty'];
|
|
|
|
|
if ($restrict_qty == 1) {
|
|
|
|
|
$form['items'][$i]['qty']['#type'] = 'value';
|
|
|
|
|
$form['items'][$i]['qty']['#theme'] = 'restrict_qty_field';
|
|
|
|
|
// If this item has a quantity restriction on it.
|
|
|
|
|
if (isset($data['restrict_qty'])) {
|
|
|
|
|
$is_lifetime = isset($data['restrict_qty']['lifetime']) && $data['restrict_qty']['lifetime'];
|
|
|
|
|
$restrict_qty = $is_lifetime ? $data['restrict_qty']['rest'] : $data['restrict_qty']['qty'];
|
|
|
|
|
if ($restrict_qty == 1) {
|
|
|
|
|
$form['items'][$i]['qty']['#type'] = 'value';
|
|
|
|
|
$form['items'][$i]['qty']['#theme'] = 'restrict_qty_field';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -290,18 +300,26 @@ function uc_restrict_qty_form_alter(&$form, &$form_state, $form_id) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Themes cart Qty. boxes so they can't be changed. (currently not in use)
|
|
|
|
|
/**
|
|
|
|
|
* Themes cart Qty. boxes so they can't be changed. (currently not in use)
|
|
|
|
|
*/
|
|
|
|
|
function theme_restrict_qty_field($variables) {
|
|
|
|
|
return check_plain($variables['form']['#value']);
|
|
|
|
|
return check_plain($variables['form']['#value']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the number of restrict_qty features on a product node.
|
|
|
|
|
/**
|
|
|
|
|
* Returns the number of restrict_qty features on a product node.
|
|
|
|
|
*/
|
|
|
|
|
function uc_restrict_qty_count($form_values) {
|
|
|
|
|
global $user;
|
|
|
|
|
|
|
|
|
|
$data = db_query("SELECT qty, lifetime FROM {uc_restrict_qty_products} WHERE nid = :nid", array(':nid' => $form_values['nid']))->fetchAssoc();
|
|
|
|
|
if ($data['lifetime']) {
|
|
|
|
|
$bought_qty = db_query('SELECT SUM(uop.qty) FROM {uc_orders} uo LEFT JOIN {uc_order_products} uop ON uo.order_id = uop.order_id WHERE uo.order_status = "completed" AND uo.uid = :uid AND uop.nid = :nid ORDER BY uop.nid', array(':uid' => $user->uid , ':nid' => $form_values['nid']))->fetchField();
|
|
|
|
|
$bought_qty = db_query('SELECT SUM(uop.qty) FROM {uc_orders} uo LEFT JOIN {uc_order_products} uop ON uo.order_id = uop.order_id WHERE uo.order_status = :completed AND uo.uid = :uid AND uop.nid = :nid ORDER BY uop.nid', array(
|
|
|
|
|
':completed' => 'completed',
|
|
|
|
|
':uid' => $user->uid,
|
|
|
|
|
':nid' => $form_values['nid']
|
|
|
|
|
))->fetchField();
|
|
|
|
|
$data['rest'] = $data['qty'] - $bought_qty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -315,11 +333,13 @@ function uc_restrict_qty_count($form_values) {
|
|
|
|
|
/**
|
|
|
|
|
* Delete all data associated with a given node.
|
|
|
|
|
*
|
|
|
|
|
* @param $nid
|
|
|
|
|
* A Drupal node ID.
|
|
|
|
|
* @param obejct $node
|
|
|
|
|
* A Drupal node object.
|
|
|
|
|
*/
|
|
|
|
|
function uc_restrict_qty_node_delete($nid) {
|
|
|
|
|
db_query("DELETE FROM {uc_restrict_qty_products} WHERE nid = :nid", array(':nid' => $node->nid));
|
|
|
|
|
function uc_restrict_qty_node_delete($node) {
|
|
|
|
|
db_delete('uc_restrict_qty_products')
|
|
|
|
|
->condition('nid', $node->nid)
|
|
|
|
|
->execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -329,5 +349,7 @@ function uc_restrict_qty_node_delete($nid) {
|
|
|
|
|
* An Ubercart product feature array.
|
|
|
|
|
*/
|
|
|
|
|
function uc_restrict_qty_feature_delete($feature) {
|
|
|
|
|
db_query("DELETE FROM {uc_restrict_qty_products} WHERE pfid = :pfid", array(':pfid' => $feature['pfid']));
|
|
|
|
|
db_delete('uc_restrict_qty_products')
|
|
|
|
|
->condition('pfid', $feature['pfid'])
|
|
|
|
|
->execute();
|
|
|
|
|
}
|
|
|
|
|