[ic] New UserTag: delete-checked

Jeff Carnahan jcarnahan@networq.com
Sun, 15 Apr 2001 17:22:37 -0700


While working on a new project, I needed a way to allow users to use a
checkbox to indicate what items they would like to remove from their
shopping cart.

This was a problem in my implementation because we were also providing users
with a text-box allowing them to change the quantity of items in their
basket.

Usually both of these features can be provided in one page, but ONLY if the
checkbox is before the text quantity box. This is because both form elements
share the same name.

To overcome this problem, I took a small snippit of Ed LaFrance's del-item
usertag and integrated it into a tag of my own, which would remove any items
marked for deletion (in this case, through a checkbox) from the user's cart.

Perhaps someone else will find this useful. Included is an example of how
you could use it. =)

--
Jeff Carnahan - jcarnahan@networq.com

--SNIP------------------------------------------------------------------
#
# $Id: delete-checked,v 1.1 2001/04/16 00:21:12 jcarnahan Exp $
#
# UserTag delete-checked - see documentation for more information
#
# Copyright 2001 by Jeff Carnahan <jcarnahan@networq.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
# USA.

UserTag delete-checked Documentation <<EOD

NAME:       delete-checked

FUNCTION:   Removes items from a users shopping cart if special form
            fields identifying products to delete are present

TESTED:     Tested on Interchange 4.6.4 -- should work on all future
            versions without any changes.

USAGE:      Named:
                [delete-checked cart="cartname"*]

            Positional:
                [delete-checked cartname*]

            * = optional parameter

PARAMETERS: cart =  if present, the name of the cart to remove items
                    from. The default is 'main'.

DISCUSSION:

[delete-checked] removes items from a shopping cart. It determines what
products to remove by looking for fields named "^^DELETE_code" that were
submitted from a form. The word "code" should be the product code of the
item to remove from the users shopping cart. If the field is set to a
non-zero, non-empty value, the item will be removed from the specified
cart.

This makes it easy to have a checkbox integrated into the display of items
in a users cart, that when checked and the form submitted, removes the
selected items.

For example, you could have a basket page that looks like:

  
  
  

Basket

[item-list] Quantity: [item-quantity]
Item: [item-code]
Price: [item-price]
Delete it?:

[/item-list]

Here, if the page is loaded, and someone had already selected items to delete, the [delete-checked] tag at the top would remove them. If the user loads this page for the first time, no items will be selected for deletion, and it would be up to the user to select items for removal and submit the form. Note that the tag will return a message indicating how many items were removed. This may be helpful when debugging. I also must acknowledge Ed LaFrance <edl@newmediaems.com> for his wonderful [del-item] usertag. Part of this code is directly based on his. If you find this tag useful, I highly suggest that you look into his at: http://developer.akopia.com/archive/interchange-users/2001/msg00129.html Thanks Ed! EOD UserTag delete-checked Order cart UserTag delete-checked PosNumber 1 UserTag delete-checked addAttr UserTag delete-checked Routine <<EOF sub { my $cart = shift || 'main'; my $opt = shift; my $key; my $counter = 0; my $removed = 0; foreach $key (keys %{ $CGI }) { next unless $key =~ s/^\^\^DELETE_//; do { if ($Carts->{$cart}[$counter]{code} eq $key) { $removed++; splice @{$Carts->{$cart}}, $counter--, 1; } ++$counter; } until $counter > $#{$Carts->{$cart}}; } # ------------------------------------------------------------------ return "$removed products removed"; } EOF