5 replies [Last post]
PaulW's picture
Offline
Joined: 05/23/2008
Juice: 148
Was this information Helpful?

I am creating a module in which I've customized a product. When the user adds the item to the shopping cart, I want to override the nid and qty for the $form_values with different values. This I can do successfully in:

function buy_memberships_form_submit($form_id, $form_values) {
  global $prod1nid, $prod1qty, $prod2nid, $pro2qty;

  $form_values['nid'] = $prod1nid;
  $form_values['qty'] = $prod1qty;
}

However, I want to add a second item with a different nid and qty to the cart at the same time. Obviously, I can't re-declare the function, so how do I send two products to the shopping cart within one submit function?

Your help is appreciated!

PaulW's picture
Offline
Joined: 05/23/2008
Juice: 148
Re: Add multiple items to cart within one submit function

Duh, that was easy! Just in case anyone else doesn't see the obvious, here it is:

function buy_memberships_form_submit($form_id, $form_values) {
  global $prod1nid, $prod1qty, $prod2nid, $pro2qty;

  $form_values['nid'] = $prod1nid;
  $form_values['qty'] = $prod1qty;

  uc_cart_add_item($prod2nid,$prod2qty);
}

PaulW's picture
Offline
Joined: 05/23/2008
Juice: 148
Related Issue... If the

Related Issue...

If the $form_value['qty'] is overridden to zero, then the item still gets added to the cart with both a quantity and total of zero.

If the quantity is zero, I don't want to add any record to the cart.
How can I keep from adding a zero quantity item to the cart?

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Related Issue... If the

Hmm... well, you can check those global variables and see if the quantity is 0... you might also write a validate handler that could trap it and throw up an error.

PaulW's picture
Offline
Joined: 05/23/2008
Juice: 148
Re: Re: Related Issue... If the

When the cart is displayed, clicking the "Update Cart" button results in the item with zero quantity to be deleted. Would it not be possible to invoke this button programatically at the end of the _form_submit function? If so, what command would accomplish it. Seems like uc_cart_update_item_object() should do it, but I don't know what object should be placed in the ().

sbanawan's picture
Offline
Bug Finder
Joined: 09/25/2008
Juice: 66
PaulW wrote:Duh, that was
PaulW wrote:

Duh, that was easy! Just in case anyone else doesn't see the obvious, here it is:

function buy_memberships_form_submit($form_id, $form_values) {
  global $prod1nid, $prod1qty, $prod2nid, $pro2qty;

  $form_values['nid'] = $prod1nid;
  $form_values['qty'] = $prod1qty;

  uc_cart_add_item($prod2nid,$prod2qty);
}

I'm looking to do a similar thing. Could you share the details of your solution? I'm very new to ubercart and I'm rusty at my PHP (though I could follow your function). I'd like to do as little programmatically to get this working because the more I do, the more likely it'll break something!