Re: Thanks, Ryan.

TR
TR's picture
Offline
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3453
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>.
Add "Empty cart" Button By: PaulW (9 replies) Thu, 07/24/2008 - 13:50