9 replies [Last post]
setfree's picture
Offline
Uber Donor
Joined: 12/16/2007
Juice: 436
Was this information Helpful?

As discussed in this thread, I am working on a project that needs to have dynamic pricing of a product. The user can choose the amount they want to pay. I have it working great accept that I have no way for the user to change the amount they are paying after the item has been added to the cart.

The code and methods I am using to achieve this are listed in the thread.

After very much research and experimentation, the only idea that I can come up with is to remove the first item from the cart and then add the item back with the updated price. But I can't find a way to remove an item from the cart.

Is there anyway for me to programmatically remove an item from the cart?

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Function to remove an item from the cart?

Can you not just tweak the data in the uc_cart_products table based on a form submission? Then you wouldn't have to remove/re-add. You can just load the data array for that item in the cart, unserialize it, modify it, and then update the row in the table with the new serialized array.

I don't believe there actually is an API func to remove a single item from the cart unfortunately. I hate the cart API. Sad

setfree's picture
Offline
Uber Donor
Joined: 12/16/2007
Juice: 436
Re: Re: Function to remove an item from the cart?

Thanks for the help Ryan! I know how busy you are and I hate to be a pain, but this is a little beyond me. If I'm on the same page, then I believe this is the same basic idea that I originally had. However, I'm stuck at trying to figure out how to write the modified item back to the table.

If you could just point in the right direction, it would really help Smiling

This is what I have right now:

<?php

function mymodule_multi_add_form_submit($form, &$form_state) {

   if (

$form_state['values']['nid'] == 288){
     
$items = uc_cart_get_contents();
      foreach (
$items as $item){
        if (
$item->nid == 288) {
       
$item->data['pay'] = 1000000; // <-- I put a new test value in, now how to I write this back to the table?      
       
}
      }
    }
 
// Display a message and redirect to the cart.
 
$form_state['redirect'] = uc_cart_add_item($form_state['values']['nid'], $form_state['values']['qty'],  module_invoke_all('add_to_cart_data', $form_state['values']));
}
?>
Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Re: Re: Function to remove an item from the cart?

You'd have to use the database abstraction layer to write the new data value to the proper column in the uc_cart_products table. This is untested pseudo-code, but hopefully it gives you an idea and some functions to look up in the Drupal API docs.

<?php
 
foreach ($items as $item){
    if (
$item->nid == 288) {
     
$new_data = $item->data;
     
$new_data['pay'] = 1000000; // Using a copy of the array for the purpose of saving.
     
db_query("UPDATE {uc_cart_products} SET data = '%s' WHERE cart_id = '%s' AND nid = %d AND data = '%s'", serialize($new_data), uc_cart_get_id(), $item->nid);
    }
  }
?>
setfree's picture
Offline
Uber Donor
Joined: 12/16/2007
Juice: 436
Thanks!

I was finally able to get it working! Thanks for all of your help, I really appreciate it Smiling

grobot's picture
Offline
Joined: 04/12/2008
Juice: 289
Re: Function to remove an item from the cart?

I know that your real question was answered via another method, eg update the existing cart entry, but due to the thread title I figured I should post this reply to the question in your first post -

setfree wrote:

Is there anyway for me to programmatically remove an item from the cart?

YES - see http://api.ubercart.org/api/function/uc_cart_remove_item

You need to supply all three parameters, even though it kind of looks like you can get away with just supplying one.

I'd love it if this function made the $data param optional and allowed us to specify only the nid.

Giant Robot - for campaign, charity, ngo & online store solutions - www.giantrobot.co.nz

kniepers's picture
Offline
Joined: 11/19/2010
Juice: 3
xurizaemon I've been trying

xurizaemon

I've been trying to use this function in a conditional action to check for a few conditions when adding an item to a cart and to remove it if appropriate. I've confirmed my conditions are working by setting a message, but the item still adds to my cart. This is my first time using ubercart, so perhaps I missed something simple.

The predicate is 'product node added to cart' and the conditions is comparing two CCK fields associated with the product.

I chose to 'execute php' as my action and then added:

$nid = $product->nid;
uc_cart_remove_item($nid, $cid = NULL, $data = array());

The other action - set message - works fine, so I know the conditions are activated correctly. I'm not sure how to debug what is going wrong at this point, so if you've used this successfully before and can provide some guidance I would appreciate it.

allanp's picture
Offline
Joined: 05/27/2010
Juice: 74
Predicate Options

I do not see 'product node added to cart' as an option for a predicate.

Any idea what i may have missed?

noah's picture
Offline
Joined: 07/27/2009
Juice: 16
Re: Re: Function to remove an item from the cart?

Is there some clear documentation on how this $data should be formatted? I don't see how I can delete versions of the same product with different attributes.

Mickey7Ray's picture
Offline
Joined: 11/22/2010
Juice: 9
Re: Function to remove an item from the cart?

Thanks buddy! That was a nice option.... It helped me too. Thanks again!