[ic] New Usertags, Y'all

Ed LaFrance [email protected]
Thu, 04 Jan 2001 13:21:21 -0800


Hello all Interchange listees -

Below please find a pair of Interchange UserTags to add to the pot: 
[add-item] and [del-item].  Together these tags allow you to manipulate the 
cart contents without direct user interaction.  Given the number of 
questions I have seen on the list about ways to automatic stuff with the 
order contents, I think that these will be of some use to the community at 
large, and I hope that Akopia will add them to the Usertag library.

Best Regards,

Ed LaFrance

----------------

#
# UserTag add-item - see documentation for more information
#
# Copyright 2001 by Ed LaFrance <[email protected]>
#
# 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 add-item Documentation <<EOD

NAME:		add-item
FUNCTION: 	programatically places item(s) in Intechange shopping carts
		without direct user interaction

IC VERSION:	4.6.x; should work with MV4 (not tested), may require changes 
for MV3.1x
		(not tested)

USAGE:		Named:
		[add-item code="sku" qty="quantity"* cart="cartname"* inc="increment"*]
		
		Positional:
		[add-item sku quantity* cartname* increment*]

		* = optional parameters

PARAMETERS:	code = 	the sku of the item to place in the cart. This is the only
			required parameter
		qty = 	the qty of the item to be placed in the cart.  Default is 1.
			This parameter works is coordinated with inc (see below).
		cart =	the name of the shopping cart in which to place the item.
			Default is 'main'.
		inc =	Provide a non-blank value to increment (by amount of qty)
			the quantity of an existing item in the cart, if there is one.
			See DISCUSSION for more info.

DISCUSSION:

[add-item] can be used to place items in the shopping cart without 
requiring the user
to explicitly click an 'Order' link, submit a form, etc.  Example:

	[set items]widgetA widgetB widgetC widgetD[/set]

	[loop arg="[scratch items]"]
		[add-item [loop-code]]
	[/loop]

Placing the above snippet on a page and displaying that page in the browser 
will cause
one each of the four items in the scratch variable to be placed in the 
default cart -
provided that they are valid items in a defined products file.

This tag works in coordination with the ProductFiles catalog.cfg directive: 
if mutiple
products files are used, each will be scanned in turn until an entry which 
matches "code"
is found.  If no match is found, nothing will be added to the cart

This tag also coordinates with the SeparateItems catalog.cfg directive.  If 
SeparateItems
is disabled (not set to 'Yes'), [add-item] will look for an ALL EXISTING 
INSTANCES of "code"
in the cart, and increase the ordered quantity by "qty" for each.  If 
SeparateItems is set
to "Yes" (enabled), [add-item] will place a new instance of "qty" amount of 
"code" in the cart.
You can over-ride this behavior by setting "inc" to any non-blank value; 
doing so will cause
[add-item] to behave as if SeparateItems is disabled.

EOD

UserTag add-item Order code qty cart inc
UserTag add-item PosNumber 4
UserTag add-item  Routine <<EOR
sub {
	my ($code, $qty, $cart, $inc) = @_;
	return unless $code;
	my $file;
	my $notfound = 1;

	foreach $file (@{$Config->{ProductFiles}}) {
		if (tag_data($file, 'sku', $code)) {
			undef $notfound;
			last;
		}
	}
	return if $notfound;

	$qty = 1 unless defined $qty;
	$cart = 'main' unless $cart;
	$inc = 1 unless $Config->{SeparateItems};
	$notfound = 1;

	if (defined $inc) {
		foreach $item (@{$Carts->{$cart}}) {
			if ($item->{'code'} eq $code) {
				$item->{'quantity'} += $qty;
				undef $notfound;
			}
		}
	}
	push (@{$Carts->{$cart}}, { code => $code, quantity => $qty }) if $notfound;
	return;
}
EOR

#
# UserTag del-item - see documentation for more information
#
# Copyright 2001 by Ed LaFrance <[email protected]>
#
# 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 del-item Documentation <<EOD

NAME:		del-item
FUNCTION: 	programatically removes, or reduces the quantity of, item(s)
		in Intechange shopping carts without direct user interaction

IC VERSION:	4.6.x; should work with MV4 (not tested), may require changes 
for MV3.1x
		(not tested)

USAGE:		Named:
		[del-item code="sku" qty="quantity"* cart="cartname"*]
		
		Positional:
		[del-item sku quantity* cartname*]

		* = optional parameters

PARAMETERS:	code = 	the sku of the item to remove or reduce.  This is the only
			required parameter
		qty = 	the qty of the item to be removed from the cart. The default
			is to completely remove the item.
		cart =	the name of the shopping cart in which to place the item.
			Default is 'main'.

DISCUSSION:

[del-item] removes some or all of an item from a shopping cart, without 
requiring direct user
interaction.  The tag can be used on any valid Interchange page prior to 
order submission. If
a value for the "qty" parameter is not supplied, ALL INSTANCES of the 
specified item will be
completely removed from the cart if it is found.  If "qty" is supplied, 
[del-item] will
attempt to reduce ALL INSTANCES of the specified item by the amount of 
"qty".  If this would
result in a remainder of 0 or less in any one instance, then that instance 
of the item will
be completely removed.

EOD

UserTag del-item Order code qty cart
UserTag del-item PosNumber 3
UserTag del-item Routine <<EOR
sub {
	my ($code, $qty, $cart) = @_;

	return unless $code;
	$cart = 'main' unless $cart;
	my $counter = 0;

	do {
		if ($Carts->{$cart}[$counter]{code} eq $code) {
			if ((defined $qty) && ($Carts->{$cart}[$counter]{quantity} > $qty)) {
				$Carts->{$cart}[$counter]{quantity} -= $qty;
			}
			else {
				splice @{$Carts->{$cart}}, $counter--, 1;
			}
		}
	++$counter;
	} until $counter > $#{$Carts->{$cart}};

	return '';
}
EOR
 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
New Media E.M.S.               Software Solutions for Business
463 Main St., Suite D          [email protected]
Placerville, CA  95667         http://www.newmediaems.com
(530) 622-9421                 (530) 622-9426 Fax
(866) 519-4680 Toll-Free
 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>