9 replies [Last post]
PaulW's picture
Offline
Joined: 05/23/2008
Juice: 148

I am writing a custom module that deals with the shopping cart. I need to remove the "Update cart" button and add an "Empty cart" button. I can easily hide the "Update cart" button via CSS. My question is:

How can I add an "Empty cart" button and write code to delete all the items from the cart?

- or -

How can I override the functionality of the "Update cart" button to delete all the items from the cart?

I will appreciate being pointed in the right direction!

Thanks.

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15422
Re: Add "Empty cart" Button

I'm curious if you can add a submit handler to that button using hook_form_alter() that would get called instead of the normal form submit handler... that would probably do what you need if it works. Puzzled

PaulW's picture
Offline
Joined: 05/23/2008
Juice: 148
Thanks, Ryan. I appreciate

Thanks, Ryan.

I appreciate your suggestionn. I only had to modify uc_cart.module in two places:

1. function uc_cart_view_form($items = NULL) {
. . . Added an "Empty cart" button.
2. function uc_cart_view_form_submit($form_id, $form_values) {
. . . Added case for the "Empty cart" button.

I attempted to override these functions in my module; however, they were so intertwined with other functions that I would have had to override many functions that I wasn't modifying. That seemed to make a mess of my module and removed a big chunk of uc_cart.module from future updates. The most direct approach was to hack the core uc_cart.module with these two short additions. I don't like doing it that way, but I could see no other clear approach. Since the two additions are small and isolated, it was not a problem to re-edit ver 1.2 of uc_cart.module this morning.

Do you think you folks will add an "Empty cart" button to the cart in the future?

-Paul

TR
TR's picture
Online
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3369
Re: Thanks, Ryan.

Ryan's suggestion was the right one. It's easy to use hook_form_alter() to remove the update button, add an empty cart button, then handle the empty cart button press in your own submission function - all without modifying core.

In your own module "example_module", add this code:

function example_module_form_alter($form_id, &$form) {
  if ($form_id == 'uc_cart_view_form') {
    // Remove "Update button" from form
    unset($form['update']);

    // Add "Empty cart" button to form
    $form['empty'] = array(
      '#type' => 'submit',
      '#value' => t('Empty cart'),
    );

    // Add new submit handler for the "Empty cart" button,
    // then call the default submit handler to deal with the
    // rest of the form
    $form['#submit'] = array(
      'example_module_cart_view_form_submit' => NULL,
      'uc_cart_view_form_submit' => NULL,
    );
  }
}

function example_module_cart_view_form_submit($form_id, $form_values) {
  switch ($form_values['op']) {
    case t('Empty cart'):
      // Take action to empty the cart
      uc_cart_empty(uc_cart_get_id());

      drupal_set_message(t('Cart is now empty...'));
      return 'cart';   // set redirect
  }
}

<tr>.
PaulW's picture
Offline
Joined: 05/23/2008
Juice: 148
TR, Super! That worked

TR,

Super! That worked great and, obviously, is much cleaner.

This is my first dig into a custom module for Ubercart. I guess, in my own naivete, I presumed that I had to put the entire contents of the affected functions in my module, rather than add bits that conditionally only affected parts of the existing uc modules. I've got a lot to learn!

I'm a bit puzzled by the purpose of the two statements in:

    $form['#submit'] = array(
      'example_module_cart_view_form_submit' => NULL,
      'uc_cart_view_form_submit' => NULL,
    );

Perhaps you could clue me in.

Thanks for your help!

-Paul

TR
TR's picture
Online
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3369
Re: TR,

That code specifies the function(s) to be run when the form is submitted. By default, when the uc_cart_view_form is submitted, Drupal will look for and execute uc_cart_view_form_submit() (append _submit to the form name). What I'm doing is to declare that two functions should be executed upon form submit: the first is example_module_cart_view_form_submit() which I use to handle the "Empty cart" button press, the second is uc_cart_view_form_submit(), which is the default submit handler and takes care of any other actions on that form, like handling the "Checkout" button. The NULL is an array() of extra arguments to pass to these functions - nothing extra is needed in this case, which is why it's set to NULL.

<tr>.
PaulW's picture
Offline
Joined: 05/23/2008
Juice: 148
Re: Re: TR,

Thanks. Your explanation is very helpful. At first blush I thought the '=> NULL' part was in some way turning the calls 'off', i.e. setting them to 'nothing'.

Is there a significance to the sequence in which the two calls are declared?

TR
TR's picture
Online
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3369
Re: Re: Re: TR,

First one is called first, second one is called second. I would point you to a good reference on the Forms API if I knew of one. Unfortunately, drupal.org has poor documentation on this topic (found at http://drupal.org/node/37775 FWIW).

<tr>.
dinorastoder's picture
Offline
Joined: 03/12/2010
Juice: 3
Re: Add "Empty cart" Button

I rewrite this code for Drupal 6

<?php
function uc_empty_cart_form_alter(&$form, $form_state, $form_id) {
  if (
$form_id == 'uc_cart_view_form') {
   
// Remove "Update button" from form
    // unset($form['update']);

    // Add "Empty cart" button to form
   

$form['empty'] = array(
     
'#type' => 'submit',
     
'#value' => t('Empty cart'),
    );

   

// Add new submit handler for the "Empty cart" button,
    // then call the default submit handler to deal with the
    // rest of the form
   
$form['#submit'][] = 'uc_empty_cart_cart_view_form_submit';
  }
}

function

uc_empty_cart_cart_view_form_submit($form, &$form_state) {
  switch (
$form_state['values']['op']) {
    case
t('Empty cart'):
     
// Take action to empty the cart
     
uc_cart_empty(uc_cart_get_id());

     

drupal_set_message(t('Cart is now empty...'));
     
// set redirect
     
$form_state['redirect'] = 'cart';  
  }
}
?>

Also I create module UC_EMPTY_CART

AttachmentSize
uc_empty_cart.zip 6.79 KB
marcushenningsen's picture
Offline
Joined: 05/05/2010
Juice: 9
Re: Re: Add "Empty cart" Button

Brilliant, thanks for this simple but useful module.

Marcus