[ic] My first usertag... see any glaring errors?

Ed LaFrance interchange-users@icdevgroup.org
Fri Feb 14 11:40:01 2003


At 10:27 PM 02/13/2003 -0500, you wrote:
>Here's my first usertag... I didn't write it from scratch but modified one 
>I found. It seems to work but sometimes I get error log entries like:
>
>  Safe: syntax error at (eval 749) line 1, near ">"
>  >
>  > >0
>  >
>
>I thought I had this solved before but spoke too soon. The tag totals 
>(price*qty) of all items that have an sku starting with "AT-" and writes 
>this total to a scratch variable used on the cart page (the error *may* 
>occur when the total is zero but I need more testing to confirm that). If 
>any ic/perl gurus can glance over the tag I'd very much like to have feedback.
>
>Here's the tag:
>
>#---- begin tag------------
>UserTag atv Routine <<EOR
>sub {
>         my $atvtot = 0;
>foreach my $atvindex (0 .. $#$Vend::Items) {
>         my $atvcode = $Vend::Items->[$atvindex]{code};
>         my $atvbase = $Vend::Items->[$atvindex]{mv_ib};
>         my $atvqty = $Vend::Items->[$atvindex]{quantity};
>         my $atvprice = $Tag->price({code => $atvcode , quantity => 
> $atvqty , noformat => 1});
>         my $isatv = 1           if $atvcode =~ m/^AT-/;
>         $atvprice = 0         if !$isatv;
>         $atvtot = $atvtot + ($atvprice * $atvqty);
>
>$Scratch->{'atvtotal'} = $atvtot;
>}}
>EOR
>#----  end tag ------------
>
>I use the scratch variable in following code on the cart page and this is 
>what I think causes the error. But it doesn't happen all the time and the 
>calculations are made correctly.
>
>[if type=explicit compare="[calc][scratch atvtotal]>0 [/calc]"]
>[and type=explicit compare="[calc][scratch atvtotal]<25 [/calc]"]
>[then]
>[discount ENTIRE_ORDER] $s + 4 [/discount]
>[/then]
>[/if]
>
>I tried Racke's suggestion of using
>$Scratch->{atvtotal} = $atvtot || 0;
>but that broke the calculations... maybe just a typo was to blame there?
>
>DB

The syntax error is caused by the first [if type=explicit ...], because 
[scratch atvtotal] could easily be empty, in which cause your perl becomes 
[calc] >0[/calc], which is definitely bad syntax. One easy way to solve 
this would be just to change the way you are doing things a bit:

[calc]
         my $disc = $Session->{discount};
         my $subtotal = $Tag->subtotal( { noformat => 1 } );
         if (    $Scratch->{atvtotal} > 0
                         and
                 $Scratch->{atvtotal} < 25
         ) {
                 $disc->{ENTIRE_ORDER} = "$subtotal - 4";
         }
         else {
                 $disc->{ENTIRE_ORDER} = '';
         }
         return;
[/calc]

You would want to call this each time the cart contents could change, so 
you would want to place it right at the top of the content area of the 
checkout page, for example.

- Ed L.


===============================================================
New Media E.M.S.              Technology Solutions for Business
463 Main St., Suite D         eCommerce | Consulting | Hosting
Placerville, CA  95667        edl@newmediaems.com
(530) 622-9421                http://www.newmediaems.com
(866) 519-4680 Toll-Free      (530) 622-9426 Fax
===============================================================