Custom add to cart redirect

Posts: 44
Joined: 11/21/2007
Bug Finder

What I'm trying to do is change the redirect link for a single product while keeping the rest of my products with no redirect where they simply refresh the page while adding it to there cart.

Something like this

  • "Special redirect product" - redirect=/cart/checkout
  • "Everything else - no redirect

Cheers,

Posts: 44
Joined: 11/21/2007
Bug Finder

They are two different Content types (product types) if that makes a difference

Posts: 4117
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Hey Tom, you're going to have to use a custom module w/ hook_form_alter() to accomplish this right now. Might seem like a bit of overkill, but it should be a quick job.

Posts: 44
Joined: 11/21/2007
Bug Finder

Thanks Ryan, I finally managed to create a custom module with my very limited php experience (it's getting better Smiling

<?php
function uc_subscribe_form_alter($form_id, &$form) {
      if (
$form_id == "uc_product_add_to_cart_form_495") {
       
$form['#action'] = url($_GET['q'], "destination=cart/checkout");
      return;
        }
}
?>

Basically this allows my form to redirect directly to checkout, which when it did I jumped for joy of course LOL!
What I need it to do now is it to empty the cart so they only have this one product in their cart, but for the life of me I can't work out how to do it!

any suggestions would be most appreciated

Posts: 4117
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

hehe Well, you might try a query... first, a big picture of how the shopping cart works. There's a database table called uc_cart_products where for every unique product in a cart, we store the qty and data array using the user ID or session ID of the customer as a key. The function uc_cart_get_items(), for example, loads up the items from this table and does stuff with them.

Your function can simply delete anything from that table that doesn't match this product's nid (which I'm assuming is 495?).

<?php
  db_query
("DELETE FROM {uc_cart_products} WHERE cart_id = '%s' AND nid != 495", uc_cart_get_id());
?>

You can do other queries to adjust the qty if you need to, too.

You should check out the Drupal API for db_query() for more info.