home  ECOMMERCE NETSHOP  : VIII Cart Addons 

netShop
VIII . Cart Add-ons

Now that we've covered the basic cart set-up and usage in the previous sections, it's time now to discuss the greatest feature of netShop: flexibilty. You see, even though the netShop does quite a good job of providing a viable solution for selling products over the web, it's the amount of customization that the netShop allows that truly makes this product valuable.

When it's first installed, the netShop is really nothing more than a template for you to build upon. Each of the pages can be customized with your site's look and feel, however, the flexibilty this document discusses goes beyond HTML formatting. This document covers the use of external scripts to enhance the netShop.

To demonstrate the use of external scripts with netShop, the developers have created two example scripts.

Example 1 - testcart.pl

The first example script, testcart.pl, is very simple and is meant to be prevent your customers from accessing your order form if they don't have any items in their cart.

#!/usr/bin/perl
# To use this script, give it 755 permissions
# and replace the submit button on checkout.shtml
# with this tag: <!--#exec cgi="testcart.pl" -->
# (obviously, if you're using links, you'll change
# the code below to reflect this.
require 'cart.pl';
print "Content-Type: text/html\n\n";
if (netShop::count_in_cart('mycart') > 0) {
print "or <input type=\"submit\" value=\"Proceed to Checkout\">\n";
}

To incorporate this script you must first download it, using the link below. Next, upload the script to your server and grant it 755 permissions. Finally, replace lines 34 and 35 of the checkout.shtml:

34
35
If you would like to order the above item(s), click <a href="order.shtml">here</a>.<br>
Go <a href="main.shtml">back</a>
With this line:

<input type="button" onClick="history.go(-1);" value="Continue Shopping"> <!--#exec cgi="testcart.pl" -->

This way, if a customer arrives to your checkout.shtml page with no items in their cart, they won't be given the option to order as only a "Continue Shopping" button will be displayed.

Download testcart.pl

Example 2 - cartsearch.cgi

The second example script, cartsearch.cgi, is far from simple and truly shows how extensively the netShop can be modified.

As you can see from the code below, cartsearch.cgi is a bit complicated. In fact, if you're not familiar with Perl, you might not be able to get an idea of what this script does.

#!/usr/bin/perl
my %q = ();
require 'cart.pl';
my $path = lc($ENV{PATH_INFO} || '/');

foreach my $i (split /\&/, $ENV{QUERY_STRING}) {
	my ($k,$v)=split /=/, $i;
	$k =~ y/+/ /; $v =~ y/+/ /;
	$k =~ s/%([A-Fa-f0-9]{2})/pack('C',hex($1))/seg;
	$v =~ s/%([A-Fa-f0-9]{2})/pack('C',hex($1))/seg;
	$q{$k}=$v;
}

my $year = $q{year};
my $l = netShop::product_list(cart => "mycart");
my @sres = ();
my %seen = ();

foreach my $i (keys(%{ $l })) {
	my $jr = $l->{$i};
	my $yr = $jr->{yr};
	$jr->{id} = $i;
	next if ($year && $yr ne $year);

	my $base = $jr->{id};
	$base =~ s/[^0-9]//g;
	my $findthis = $base;
	my $fprice = $jr->{price};
	$fprice =~ s/([0-9]{2})$/\.$1/;

# Builds Add/Remove area for each item.
	my $item;
	$item = join('',
	'<tr><td>', $jr->{tags}, ' - $', $fprice, '</td><td>&nbsp;</td>',
	'<td><input name="shoppe_', $i, '" type="text" size="2" value="', $jr->{qty}, '"
	 onChange="set_count(\'mycart\', \'', $i, '\', this.value);"> ',
	'<a href="cart.cgi?put-another:mycart=', $i, '" onClick="put_another(\'mycart\', \'' , $i, '\');
	 document.forms.mycart[\'shoppe_', $i, '\'].value = count_in_cart(\'mycart\', \'', $i, '\');
	 return false;">Add</a> | ',
	'<a href="cart.cgi?remove-one:mycart=', $i, '" onClick="remove_one(\'mycart\', \'', $i, '\');
	 document.forms.mycart[\'shoppe_', $i, '\'].value = count_in_cart(\'mycart\', \'', $i, '\');
	 return false;">Remove</a></td></tr>');
	
	if ($seen{$findthis}) {
		push(@{ $seen{$findthis}{__avail} }, $item);
		if ($path eq '/product') {
			$seen{$findthis}{avail} = join("\n", sort @{ $seen{$findthis}{__avail} });
		} else {
			$seen{$findthis}{avail} = join("", sort @{ $seen{$findthis}{__avail} });
		}
	} else {
		$jr->{findthis} = $findthis;
		$jr->{__avail} = [ $item ];
		$jr->{avail} = $item;
		$jr->{base} = $base;
		$seen{$findthis} = $jr;
		push(@sres, { $i => $jr });
	}
}

my (@res) = sort {
	my $a_val = ((values %$a)[0]);
	my $b_val = ((values %$b)[0]);
	return $a_val->{'id'} cmp $b_val->{'id'};
} @sres;

my $skip = ($q{skip} && $q{skip} > 0) ? $q{skip}+0 : 0;
my $limit = ($q{limit} && $q{limit} > 0) ? $q{limit} : 10;
$limit = 10 if ($limit < 1);
my $end = $skip + $limit;
my $more;
if ($end > $#res) {
	$end = scalar(@res);
} else {
	$more = ($#res - $end) + 1;
	$more = $limit if ($more > $limit);
}
(@res) = ((@res)[$skip..$end-1]);

print "Content-Type: text/html\n\n";

# Error message text.
if (scalar(@res) == 0) {
	print "<tr><td align=\"center\" colspan=\"4\">";
	my @b = (

q{There are no products matching your search - Please Try Again!},
q{No products found matching your search - Please Try Again!},
q{If you cannot find what you are looking for - Please Contact Us.},
q{Nothing matching that combination was found.},
	);
	print $b[int rand scalar @b];
	print "</div></td></tr>\n";
	exit 0;
}

# Splits products into different pages.
if ($path eq '/product') {
	print netShop::parse_template('product-list.html', @res);
	print "\n"; $|=1;
	exit 0;
} else {
	print netShop::parse_template('list.html', @res);
	print "\n"; $|=1;
	my $qs = $ENV{QUERY_STRING};
	$qs =~ s/\&?skip=\d+\&?//;
	print "<tr><td align=\"center\" colspan=\"2\"><table border=\"0\" width=\"100\%\"><tr><td align=\"left\">";
	my $os = $skip - 10;
	my $ns = $skip + 10;
	print "<a href=\"main.shtml?$qs\&skip=$os\">Previous $limit &lt; &lt;</a></span>" if ($skip > 0);
	print "</td><td align=\"right\">";
	print "<a href=\"main.shtml?$qs\&skip=$ns\">Next $more &gt; &gt;</a>" if ($more);
	print "</td></tr></table></td></tr>\n";
	exit 0;
}

This script modifies the way the catalog pages behave and actually replaces the default means for displaying your product tables on the netShop's catalog pages.

Specifically, this script adds the following functionality to the netShop:

Again, for a better idea of what cartsearch.cgi does, compare the two Kung Fu Movies sites mentioned in the IC Tech. Ref. Document: netShop VII. Example Carts. Version 1 does not use any extra scripts and version 2 uses both testcart.pl (mentioned above) and cartsearch.cgi.

Download cartsearch.cgi

Related Items

Email and Web Hosting by Internet Connection 2004-2009 ©
Contact Us