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

gumol’s picture

Thank you for your code. You have missed one closing curly bracket. Correct code for One product per cart in Ubercart:

<?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]);
}
?>
tkasis’s picture

I 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

daveX99’s picture

@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.

kapilsurolia’s picture

Issue summary: View changes

sir this code where update because i created a custom module but this code doesnot work so help me

raulmuroc’s picture

Status: Active » Closed (fixed)

Closing 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).