Can't remove donation from cart
kenjil - September 22, 2009 - 17:18
| Project: | Ubercart Donation Products |
| Version: | 6.x-2.0-beta3 |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
Description
Hello,
I'm programmatically adding donation the following way (inspired from code of uc_donation).
if($amount > 0){
$form_values = array(
'nid' => $nid, //nid of my donation product
'qty' => 1,
'donate_amount' => $amount, // amount collected in some way
);
uc_cart_add_item($nid, 1, module_invoke_all('add_to_cart_data', $form_values));
}it works fine but the donation i produce that way can't be removed from the cart when we check its checkbox and click 'update cart'.
Thanks for your help.

#1
The problem is that the serialized data string in the uc_cart_products table thinks that the "donate_amount" is a string.
But in "uc_donation_update_cart_item" its a float. why? I dont know, ask the maintainer.
I have created a patch that works.
#2
Anon: thank you for the patch. It allowed me to figure out what was going on. That being said, I think I may have some better (or at least simpler) solutions.
But in "uc_donation_update_cart_item" its a float. why? I dont know, ask the maintainer.
After a few hours of analyzing the code, I think I can answer that: in "uc_donation_add_to_cart_form_validate()", the donate_amount gets converted to a float. If you properly use the existing forms, the validate function will automatically get called. If you create your own form and manually call "uc_cart_add_item()", it doesn't get called.
Solution #1: in your code, when you're creating the form_state[] (assuming you do it manually), change $form_state['donate_amount'] to be a float. This fixes the problem for me; and I believe addresses the underlying issue.
$product_fs = array();
$product_fs['nid'] = $form_state['values']['projectdisplay-donation-node-id'];
$product_fs['qty'] = $form_state['values']['projectdisplay-donation-qty'];
$donate_amount = floatval(ereg_replace('[^0-9\.]*', '', $form_state['values']['projectdisplay-donation-amount']));
$product_fs['donate_amount'] = $donate_amount;
//add item to cart
$cart_redirect = uc_cart_add_item($product_fs['nid'], $product_fs['qty'], module_invoke_all('add_to_cart_data', $product_fs));
(I swiped that "... floatval()" line from "uc_donation.module", line 199.)
Solution #2: find a way to call "uc_donation_add_to_cart_form_validate()" properly from your code, just before the "uc_cart_add_item()" call. This looked like more trouble than it was worth, considering I'd have to build a form to go along with the form_state[].
Solution #3: when creating your form, use the standard product $form_state[] keys, perhaps by calling uc_product_form(), and make sure all the proper handlers/callbacks are set. This will automatically call "uc_donation_add_to_cart_form_validate()", which will take care of the floatval() problem. This is my preferred solution, but it's not one I can use in all cases, especially when I don't know at form-build-time what the node will be.