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

Scott Kennedy interchange-users@interchange.redhat.com
Sun May 19 05:23:01 2002


On Fri, 17 May 2002, Kevin Walsh wrote:

[snip]
|>
|>
|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.

I have everything fixed now.  What I did was use the discount tag along with a
formula that calculates the right discount (see code below).

|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?

My intention was to actually use a discount formula.  I wasn't going about
things the right way obviously.

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

I took your suggestion and I'm using that instead now.

|  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.

I was wondering about.  I believe that the new code that I wrote deals with that
situation.

|  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

I see what you're saying.  For this particular application I don't believe this
would be a concern, however, I'll bear this in mind when I am designing other
storefronts in the future.

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

I forgot to remove that code.  I don't know if it's required that I declare all
variables, but I rewrote the code so that all of them are declared.

|>
|> 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,
|    });

I'm not sure about this but I don't believe that was the problem.  I'm not
entirely sure what was causing that problem really.  The new code uses $s to get
the order subtotal.

|--
|   _/   _/  _/_/_/_/  _/    _/  _/_/_/  _/    _/
|  _/_/_/   _/_/      _/    _/    _/    _/_/  _/   K e v i n   W a l s h
| _/ _/    _/          _/ _/     _/    _/  _/_/    kevin@cursor.biz
|_/   _/  _/_/_/_/      _/    _/_/_/  _/    _/
|
|_______________________________________________
|interchange-users mailing list
|interchange-users@interchange.redhat.com
|http://interchange.redhat.com/mailman/listinfo/interchange-users
|


Here is the new code.  I'm including it in the hope that it might be useful to
someone.  A portion of this code was adapted from some code that I found in the
mail archive written by Mark Johnson, by the way.  I've found this mailing list
and the list archive to be an excellent resource.  I greatly appreciate all of
the help that you've given to me.  The code below also returns the amount of
savings when a discount(s) is set.

/*BEGIN INCLUDED TEXT*/
[comment]Set discounts if basket isn't empty.[/comment]
[if items]
[discount code="ENTIRE_ORDER"]
	my $discounta; my $discountb; my $discountc; my $discountd;
	my $amount; my $savings; my $perc;

	$discounta = $Values->{discounta};
	$discountb = $Values->{discountb};
	$discountc = $Values->{discountc};
	$discountd = $Values->{discountd};

	$amount = $s;

	if ($discounta eq 'on') {
		$amount = $amount - ($amount * .2);
	}

	if ($discountb eq 'on') {
		$amount = $amount - ($amount * .05);
	}

	if ($discountc eq 'on') {
		$amount = $amount - ($amount * .05);
	}

	if ($discountd eq 'on') {
		$amount = $amount - ($amount * .1);
	}

       	unless ($amount) {
  		delete $Scratch->{savings};
  		return $s;
	}

	#Savings after discount(s) have been applied.

        $Scratch->{savings} = $s - $amount;
	$perc = 1 - ($amount / $s);

	return $s - $s * $perc;
[/discount]
[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]
/*END INCLUDED TEXT*/


--Scott



***********************************************************
/ Custom Web Promotions /
Economical web hosting & online storefronts featuring
Red Hat Linux 7.2 & Red Hat Interchange 4.8.5
http://custweb.com / 888-287-8932 / sales@custweb.com
***********************************************************