6 replies [Last post]
epg
epg's picture
Offline
Joined: 09/26/2009
Juice: 56
Was this information Helpful?

Is there a way to change where the "cancel" button on the checkout page redirects to?

Currently it takes you to the shopping cart. Since I'm bypassing the shopping cart (it is an unnecessary step since I only sell one file per customer), I want it to redirect to my homepage or any other page on my site. How can I do this?

Even better: is there a way to get rid of the "cancel" button altogether? I'm on drupal 6.13, using Ubercart 2.x

Thanks!

Mark B's picture
Offline
Joined: 10/27/2008
Juice: 47
Workaround

I have exactly the same problem, and struggled to solve it. I tried overriding the form submit function and the cancel button submit function, but my new submit function never got fired.

The best workaround I've found is to simply remove the cancel button from the checkout page altogether. The user can use their "Back" button or follow the normal site navigation cues (like menus).

If you don't already have a custom module where you can implement your own hook_form_alter, there's a contributed module which will do this for you: http://www.ubercart.org/contrib/13630

timos's picture
Offline
Joined: 02/13/2010
Juice: 30
Hi, The redirect process is

Hi,
The redirect process is wrote there :
ubercart/uc_cart/uc_cart.pages.inc, near line 125 :

function uc_cart_checkout_form() {
  global $user;

  // Cancel an order when a customer clicks the 'Cancel' button.
  if ($_POST['op'] == t('Cancel')) {
    if (intval($_SESSION['cart_order']) > 0) {
      uc_order_comment_save($_SESSION['cart_order'], 0, t('Customer cancelled this order from the checkout form.'));
      unset($_SESSION['cart_order']);
    }
    drupal_goto('cart');
  }

So we have to find out how override it ! As it's not in a submit function i don't know how to do ?
Any idea ?

Thanks

timos's picture
Offline
Joined: 02/13/2010
Juice: 30
Tricky style

Ok,
I think i found out a trick to do it but it's not very clean, so i write it :

if ($_POST['tod'] == t('Cancel')) {
     if (intval($_SESSION['cart_order']) > 0) {
      uc_order_comment_save($_SESSION['cart_order'], 0, t('Customer cancelled this order from the checkout form.'));
      unset($_SESSION['cart_order']);
    }
    // here you can do that you
    drupal_goto('node/2');
    }
   
   // change the the name of your $_POST variable like you want
   $form['cancel']['#name'] = 'tod';
}

As i change the name of my $_POST variable, the code on the original function doesn't match and mine match only if i cancel

Well, now i have to do some testes to see if there is not conflict but it seems ok

timos's picture
Offline
Joined: 02/13/2010
Juice: 30
The trick seems to be fine. I

The trick seems to be fine.
I test it on my site and it's ok.
I just add the $form['continue']['#name'] override as all buttons on a form have to get same ['#name'] property.

So the code likes that

<?php
function checkout_cancel_form_uc_cart_checkout_form_alter(&$form, &$form_state) {
  // if user cancel is checkout the cart is dropped
  if ($_POST['custom'] == t('Cancel')) {
    global $base_url;
    global $user;
    // get the cart_id (you can do it with the  uc_cart_get_id function defined in uc_cart.module at line 1310
    if($user->uid) {
      $cart_id = $user->uid;
      }
    else {
      $cart_id = $_SESSION['uc_cart_id'];
      }
      // drop the cart
      db_query('DELETE FROM {uc_cart_products} WHERE cart_id = %d', $cart_id);
      // if order has been prepare, unset the variable
      if (intval($_SESSION['cart_order']) > 0) {
        uc_order_comment_save($_SESSION['cart_order'], 0, t('Customer cancelled this order from the checkout form.'));
        unset($_SESSION['cart_order']);
      }
    // if user is logged in he's redirected on user page
    if($user -> uid != 0) {
      drupal_goto('user/');
    }
    // else on the home page
    else {
      drupal_goto($base_url);
    }
   }
   // here we define new $form['continue']['#name'] and $form['cancel']['#name'] to override the condition define in the uc_cart_checkout_form (uc_cart.pages.inc line 125)
   // the $form['continue']['#name'] is defined too as all buttons in a form have to get the same id (FORM API)
   $form['continue']['#name'] = 'custom';
   $form['cancel']['#name'] = 'custom';
}

If you don't have custom module i let a tar.gz here and you can implement it.
This code drop the cart if is canceled and redirect on the user page or on the home page if anonymous user. You can rewrite your own redirection and your own custom function

Thanks to report any bug here and don't use it like it's a bug fixed code. It's just a trick !!!

Note you can override the cancel button's ['#value'] property too. If you do that the condition would be

if($_POST['op'] == t('NewValue')) {
// New Function
}

and the redefinition of element have just to be :

$form['cancel']['#value'] = t('NewValue');
AttachmentSize
checkout_cancel.tar.gz 1.1 KB
timos's picture
Offline
Joined: 02/13/2010
Juice: 30
Re: redirecting "cancel" button on checkout page

I add just a comment in the mini module :

// if order has been prepare, unset the variable
      // Don't remove this part !!!
      if (intval($_SESSION['cart_order']) > 0) {
        uc_order_comment_save($_SESSION['cart_order'], 0, t('Customer cancelled this order from the checkout form.'));
        unset($_SESSION['cart_order']);
      }

This code is a copy of the original function, don't remove it !!!

AttachmentSize
checkout_cancel-1.1.tar.gz 1.11 KB
Tanjerine's picture
Offline
Bug FinderInternationalizationizerNot KulvikThe other woman.
Joined: 08/31/2007
Juice: 235
Unset Cancel Button

Just to document this for new users, if you want to remove the cancel button in the cart/checkout page, you can create a custom module with the following OR if you have one already, simply add the following:

<?php

function alter_modules_page_form_alter(&$form, $form_state, $form_id) {
    global $user;
 
    switch ($form_id) {
       /* OTHER FORMS TO HANDLE if any*/
       /* .... */
      
       case 'uc_cart_checkout_form' :
          // unset cancel button in checkout
          unset($form['cancel']);
          break;
    }
}