Posted by marthinal on March 24, 2011 at 11:00am
2 followers
Jump to:
| Project: | Ubercart Restrict Qty |
| Version: | 6.x-2.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Issue Summary
If you want to create a membership process with ubercart mis very important i think to put qty to 1 but also maybe is a good idea to reduce to only one selection the items in the cart.
For example if someone wants to pay a suscription for 1 month... then he goes back and select 1 year product because he was thinking better about ... you have 2 products.In my case for asuscription i dont need the cart what i need is that the user select the suscription and then checkout only the last selected option.
So i implemented a function using hook_cart_item() that put always to 1 qty and only retain the last item.
<?php
function my_module_cart_item($op, &$item) {
// Restrict all quantities to 1.
if ($item->qty > 1) {
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));
}
$products = db_query('SELECT cart_id,cart_item_id FROM {uc_cart_products} WHERE cart_id = %d ORDER BY cart_item_id ASC', $item->cart_id);
$products_min = array();
if(db_affected_rows($products) > 1){
while ($row = db_fetch_array($products)) {
$products_min[] = $row['cart_item_id'];
}
db_query('DELETE FROM {uc_cart_products} WHERE cart_item_id = %d', $products_min[0]);
}
?>I think maybe is possible to add something like this to this module.
Thanks.
Comments
#1
Thank you for your code. You have missed one closing curly bracket. Correct code for One product per cart in Ubercart:
<?phpfunction my_module_cart_item($op, &$item) {
// Restrict all quantities to 1.
if ($item->qty > 1) {
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));
}
$products = db_query('SELECT cart_id,cart_item_id FROM {uc_cart_products} WHERE cart_id = %d ORDER BY cart_item_id ASC', $item->cart_id);
$products_min = array();
if(db_affected_rows($products) > 1){
while ($row = db_fetch_array($products)) {
$products_min[] = $row['cart_item_id'];
}
}
db_query('DELETE FROM {uc_cart_products} WHERE cart_item_id = %d', $products_min[0]);
}
?>