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.
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
Comment #1
gumol commentedThank you for your code. You have missed one closing curly bracket. Correct code for One product per cart in Ubercart:
Comment #2
tkasis commentedI am new to Drupal module development and have a question. Is this code snippet added to the Restrict Qty module or placed in a module of its own?
Thanks
Comment #3
daveX99 commented@tkasis:
As a good, general rule, you should not edit core or contributed modules directly. This is because the usual process for updating Drupal modules will wipe out any custom code you have written -- the new code file will replace the old.
This should go in a separate module that you maintain.
-dave.
Comment #4
kapilsurolia commentedsir this code where update because i created a custom module but this code doesnot work so help me
Comment #5
raulmuroc commentedClosing due to lack of activity. If you are still working on this application, you should fix all known problems and then set the status to "Needs review". (See also the project application workflow).