[ic] Custom shipping

Kevin Walsh interchange-users@icdevgroup.org
Fri Jul 12 08:28:00 2002


> 
> Can anyone help me out with this? I guess the 4th of July was a bad time to 
> post to the list.
> 
Why?  What happened on the 4th? :-)

> >
> > Thanks for your fast and helpful reply last month. I have put something
> > together, but cannot get it to work. I get the following message on the
> > checkout page:
> >
> > "Shipping mode 'DEFAULT': bad formula. Returning 0.No match found for mode
> > 'DEFAULT', quantity '1', returning 0."
> >
> > Probably a syntax error somewhere. Could you (or someone else) please help
> > me get this working? Here are the details:
> >
> > In catalog.cfg:
> >
> > UserTag calc_shipping Order weight zone
> > UserTag calc_shipping Routine <<EOF
> > sub {
> >     my ($weight, $zone) = @_;
> >     $zone = 'zone'.$zone;
> >     my $qty_packtype_a = 0;
> >     my $qty_packtype_b = 0;
> >     my $qty_packtype_c = 0;
> >     my $qty_packtype_d = 0;
> >
> >     my $shipping_charge = $Tag->query(sql="SELECT $zone FROM shipping WHERE
> > weight = $weight");
> >
> >     for (@$Items) {
> >       if ($Tag->query(sql="SELECT packaging_type FROM products WHERE sku =
> > '$_->{sku}'") == 'A')
> >         $qty_packtype_a ++;
> >       else if ($Tag->query(sql="SELECT packaging_type FROM products WHERE
> > sku = '$_->{sku}'") == 'B')
> >         $qty_packtype_b ++;
> >       else if ($Tag->query(sql="SELECT packaging_type FROM products WHERE
> > sku = '$_->{sku}'") == 'C')
> >         $qty_packtype_c ++;
> >       else if ($Tag->query(sql="SELECT packaging_type FROM products WHERE
> > sku = '$_->{sku}'") == 'D')
> >         $qty_packtype_d ++;
> >     }
> >
> >     my $packaging_charge = 0;
> >
> >     if ($qty_packtype_a)
> >       $packaging_charge += $Tag->query(sql="SELECT packtype_a FROM
> > packaging WHERE quantity = $qty_packtype_a");
> >     if ($qty_packtype_b)
> >       $packaging_charge += $Tag->query(sql="SELECT packtype_b FROM
> > packaging WHERE quantity = $qty_packtype_b");
> >     if ($qty_packtype_c)
> >       $packaging_charge += $Tag->query(sql="SELECT packtype_c FROM
> > packaging WHERE quantity = $qty_packtype_c");
> >     if ($qty_packtype_d)
> >       $packaging_charge += $Tag->query(sql="SELECT packtype_d FROM
> > packaging WHERE quantity = $qty_packtype_d");
> >
> >     return $shipping_charge + $packaging_charge;
> >     }
> > EOF
> >
OK, first things first.  All of your $Tag->query() calls are wrong.
See the Interchange Tags Reference manual:

    $Tag->query({ sql => 'some sql here' });

Do you see the difference?

Secondly, you can't expect $Tag->query() to work from a usertag unless
you're calling it from [perl], or a similar Perl block, with the table
pre-opened ("table" parameter).  Try this sort of thing instead:

    my $db = ::database_exists_ref('packaging');
    my $resultset = $db->query('some sql here');

Lastly, the $resultset will be an arrayref with each element pointing
to an arrayref (giving you rows and columns).  Your queries seem to
return one row with one column, so you'll need to use $resultset->[0]->[0]
to get at your single value result.

You could create yourself a subroutine in your UserTag (or even a new
UserTag) that expects two args (table name and sql query), checks that
data was returned by the query and then returns the $resultset->[0]->[0]
value, as expected by your code.

I hope this helps.

-- 
   _/   _/  _/_/_/_/  _/    _/  _/_/_/  _/    _/
  _/_/_/   _/_/      _/    _/    _/    _/_/  _/   K e v i n   W a l s h
 _/ _/    _/          _/ _/     _/    _/  _/_/    kevin@cursor.biz
_/   _/  _/_/_/_/      _/    _/_/_/  _/    _/