[ic] [SOLUTION] different shipping cost schedule for each item in cart

Guido Sohne caught_one@yahoo.com
Thu, 15 Mar 2001 11:24:47 -0800 (PST)


I've posted earlier on trying to find a way to charge shipping on a per
item basis and got some leads that gave birth to the code I wrote below
(thank you, cfm@maine.com, for responding to my plea for help!). 

Here's how it works ...

Basically, one extends the products table with a ship field. Each
product can now have a shipping class. For example, household
electricals can have a shipping class of '1' and building materials can
have a shipping class of '2'. A shipping class is basically an ordinary
Interchange shipping method.

Next one defines shipping methods via the usual means (editing
shipping.asc or using the admin interface to create shipping methods
and associated data tables). For example, if I am shipping to Ghana and
I have products of shipping class '1' and '2' then I define two
shipping methods, one called Ghana1, the other called Ghana2. 

Finally, you set mv_shipmode to 'Ghana' (notice that this is without
the trailing '1' or '2' that are part of the actual ship mode).

In your HTML, just use a [delivery-charge] tag anywhere you want to see
the actual shipping charges for all products in all shipping classes.

The UserTag below will have to be put into your catalog.cfg ... It's
not perfect code but is a good and importantly, working, first cut.

UserTag delivery-charge Routine <<EOR
sub {
	my $mv_shipmode = $::Values->{mv_shipmode};
	my $items = $Vend::Items;
	my $delivery_charges = 0;
	my $opt = { convert => 1, noformat => 1 };
	my %carts;

	# iterate over the cart, watch the sun rise :)

	foreach my $i (0 .. $#$Vend::Items) {

		# decode cart item into components

		my $code = $Vend::Items->[$i]{code};
		my $mv_ib = $Vend::Items->[$i]{mv_ib};

		# get shipping class of product
		my $db = Vend::Data::database_exists_ref($mv_ib);
		$db = $db->ref();

		return "no such sku $code" unless $db->record_exists($code);
		my $ship = $db->field($code, "ship");

		# create cart for each shipping class

		push @{$carts{$ship}}, $Vend::Items->[$i];

	# calculate shipping for each cart ...
	foreach my $c (keys %carts) {
		$Vend::Items = $carts{$c}; # set shopping cart to this subset of
		my $delivery = Vend::Interpolate::tag_shipping($mv_shipmode . $c,
		$delivery_charges += $delivery;

        # restore original shipping cart
	$Vend::Items = $items;

	return $delivery_charges;

Do You Yahoo!?
Yahoo! Auctions - Buy the things you want at great prices.