[ic] Discount problem (left out error message in log file)

Kevin Walsh interchange-users@interchange.redhat.com
Thu May 16 19:50:00 2002


> [comment]Set discounts if basket isn't empty.[/comment]
> [if items]
> [perl]
> 	$discount1 = 0; $discount2 = 0;
> 	$discount = 0; $discount = 0;
> 
> 	$discounta = $Tag->value('discounta');
> 	$discountb = $Tag->value('discountb');
> 	$discountc = $Tag->value('discountc');
> 	$discountd = $Tag->value('discountd');
> 
> 	$cartname = $Tag->value('c_nickname');
> 	$amount = $Tag->subtotal($cartname, 'noformat');
> 
> 	if ($discounta eq 'on') {
> 		$amount = $amount - ($amount * .2);
> 		$Tag->discount('ENTIRE_ORDER', $amount);
> 	}
> 
> 	if ($discountb eq 'on') {
> 		$amount = $amount - ($amount * .05);
> 		$Tag->discount('ENTIRE_ORDER', $amount);
> 	}
> 
> 	if ($discountc eq 'on') {
> 		$amount = $amount - ($amount * .05);
> 		$Tag->discount('ENTIRE_ORDER', $amount);
> 	}
> 
> 	if ($discountd eq 'on') {
> 		$amount = $amount - ($amount * .1);
> 		$Tag->discount('ENTIRE_ORDER', $amount);
> 	}
> [/perl]
> [else]
> [if discount ENTIRE_ORDER][discount ENTIRE_ORDER][/discount][/if]
> [value name=discounta set=""]
> [value name=discountb set=""]
> [value name=discountc set=""]
> [value name=discountd set=""]
> [/else]
> [/if]
> 
> 
What are you doing?

The [discount] tag is intended to be passed a "formula" so that
it can calculate and apply the discount on demand.  For instance,
when the [subtotal] tag, or $Tag->subtotal(), is called.  You
should work out a block of Perl to use as your discounting
formula and pass that to a [discount] tag.

You appear to be attempting to set a specific value to be used
as a subtotal.  If you are going to do that, then why not just
work out your new subtotal value and then call the [assign] tag?

Other notes:

  1. You can use $Values->{someval} instead of calling
     $Tag->value('someval'} as a subtoutine.

  2. If all four of your discount values are set 'on' then the last
     call to the discount sub will be passed an $amount that has lost
     all touch with reality.  Your code needs a general rethink.

  3. One last thing.  You shouldn't use "value" or "cgi" values to
     hold your user discount status; use "scratch" values instead.
     Anyone would be able to do this to set their own discount by
     typing a URI like this into their browser:

     http://www.yourdomain.com/cgi-bin/whatever/index.html?discounta=on

  4. Your (undeclared) variables, $discount1, $discount2 and $discount
     are set to zero but never actually used for anything.

>
> 65.66.23.10 uuszLadh:65.66.23.10 - [16/May/2002:15:38:39 -0700] altchoice
> /cgi-bin/altchoice/process.html Discount ENTIR
> E_ORDER has bad formula. Returning normal subtotal.
> eval "string" trapped by operation mask at (eval 504) line 1.
>       (in cleanup) Undefined subroutine &MVSAFE49123:: called at
> /usr/lib/perl5/5.6.1/i386-linux/Safe.pm line 222.
>
The message means that the [discount] tried to use a bad formula.
I'll hazard a guess that the problem could be traced back to these
lines:

    $cartname = $Tag->value('c_nickname');
    $amount = $Tag->subtotal($cartname, 'noformat');

Perhaps you could try replacing those lines with the following:

    my $amount = $Tag->subtotal({
        name => 'main',
        noformat => 1,
    });

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