Welcome to WebmasterWorld Guest from 54.234.244.30

Forum Moderators: open

Message Too Old, No Replies

# javascript code for price calculation

## help needed to decipher and amend

#### JonnyWales

6:12 pm on Apr 12, 2003 (gmt 0)

#### Junior Member

joined:Mar 12, 2002
posts:118

My site sells just one type of product of business software with several editions at different prices and uses WorldPay for payment processing. The pricing is currently very simply and if somebody wants one unit then they pay say \$500. If they want two units they pay \$1,000, three copies then \$1,500. Discounts for the purchasing of multiple licences is handled by recrediting their credit card following purchase ie. buy 5 copies for \$2,500 and then receive \$500 back. It is a bit messy and also confusing for customers.

What I now intend to do is have the first copy at full price and then all subsequent copies at half price. So, if 2 bought the cost would be 500+250=\$750 (yep, reducing prices to try and get more multiple sales through). If 5 bought then the cost would be 500+250+250+250+250=\$1,500.

My problem is that I do not understand the WorldPay code. I can stumble about and know enough to implement the current pricing structure but not the new prices. A snippet of the code is as follows and if anyone can help to decipher it I would be very grateful :-

function calc(x) {
x.amount.value = 0;
var y = x.price.length;
var z = x.qty.length;
var a = Number(x.amount.value);
var b,c,d;
d = true;
while(y > 0) {
b = Number(CheckNull(x.price[y-1].value));
c = Number(CheckNull(x.qty[y-1].value));
if(c < 0) {
d = false;
c = 0;
x.qty[y-1].value = c;
}
a += (b * c);
// alert("x.price["+eval(y-1)+"].value = "+x.price[y-1].value+"\nx.qty["+eval(y-1)+"].value = "+x.qty[y-1].value+"\na = "+a);
y--;
}
if(d == false) {
alert("Negative quantities not permitted; these have been set to zero.");
}
return a;
}

Thanks
Jonny

#### tedster

5:16 am on Apr 13, 2003 (gmt 0)

#### Senior Member

joined:May 26, 2000
posts:37301

I'd like to offer help, but man this is grueling stuff. It's very hard for me to understand what each of these lines are accomplishing. A lot of it seems to be checking to make sure the entries are both numeric and not negative.

Also, anything we give you here runs the risk of not aligning well with the rest of the WorldPay code. So this is where I would turn to WorldPay support. What you want to implement is a relatively simple discount scheme, and I'd assume that they have some basic support and instructions somewhere - they are certainly a big enough operation.

The needed logic seems clear enough. The total purchase price is

unitprice + ((quantity-1)*unitprice/2)

The rest of the code seems to be involved with guaranteeing the integrity of the price and quantity variables.

#### JonnyWales

2:54 pm on Apr 13, 2003 (gmt 0)

#### Junior Member

joined:Mar 12, 2002
posts:118

Tedster,

Thanks, but I already tried WorldPay and gor this response :-

"We don't offer support for this code as it can be a lengthy process to correct a simple error."

Do you know what language the function has been written in? Do you know of any web resources which would be able to help?

Jonny

#### sullen

3:09 pm on Apr 13, 2003 (gmt 0)

#### Preferred Member

joined:Apr 4, 2003
posts:410

It's javascript. I'm not a javascript expert, but it would seem that it is simply verifying that the price (a form field on the page somewhere) is a positive number, and multiplying that by the quantity.

The bit you need to change is the

a += (b * c);

but I wouldn't like to give you the code in case I am wrong (like Worldpay say, javascript like this is a bitch to debug). There are beginner javascript books around, but to be honest if you don't even know what the code is you should pay someone to do it (would take about half an hour for someone who knows what they are doing

#### jimbeetle

3:12 pm on Apr 13, 2003 (gmt 0)

#### Senior Member

joined:Oct 26, 2002
posts:3292

This might not be a solution but it gets away from focusing on the code and techie stuff. A couple of low-tech folks I know offer volume discounts as separate items or products.

Product A 1 copy at \$500
Product B 2 copies at \$750
Product C 3 copies at \$1,000

etc.

Might be a simple to implment fix.

#### McIntyre

6:24 pm on Apr 13, 2003 (gmt 0)

#### New User

joined:Apr 5, 2003
posts:17

hmm. It seems that "x" is an element and shouldn't be able to simply be put into the function without "var x=document.getelementbyid(text1)". I think you may have missed something out.

#### g1smd

6:47 pm on Apr 13, 2003 (gmt 0)

#### Senior Member

joined:July 3, 2002
posts:18903

They run this code client side using Javascript? What is to stop someone rewriting that code to get lower prices, running it from their hard drive, and hooking the new result back to the vendors site?

#### tedster

8:20 pm on Apr 13, 2003 (gmt 0)

#### Senior Member

joined:May 26, 2000
posts:37301

The bit you need to change is the
a += (b * c);

That's true. With absolutely no guarantee, and suggesting you test any code that you create before putting it into production, I can say I would try this change as a first shot:

a += (b + b*(c-1)/2)

#### tedster

8:25 pm on Apr 13, 2003 (gmt 0)

#### Senior Member

joined:May 26, 2000
posts:37301

As and added comment, one thing that concerns me here is why the existing code uses the += operator when the variable a "should" be zero. It seems to me that a simple = would do the job. So there may be some logic behind the existing code that I just don't see.

#### JonnyWales

8:52 pm on Apr 13, 2003 (gmt 0)

#### Junior Member

joined:Mar 12, 2002
posts:118

What I have done is amend the code as follows :-

if(c==0){
}else{
a=b+((c-1)*b/2);
}

This seems to resolve part of the problem and does calculate the correct discount for the first item on the page. However, I have 5 items and implementing this code results in a zero value being returned if any of the other 4 items are selected.

One step forward ....

#### tedster

9:07 pm on Apr 13, 2003 (gmt 0)

#### Senior Member

joined:May 26, 2000
posts:37301

Good luck to you. Just keep playing with the code in logical ways and pin down what all the parts are doing.

I can see you'll hit a snag if the other items don't use the same discount scheme.

I also note that you used the plain = operator. Experiment with the += operator. That may play into the problem you already found.

10:20 pm on Apr 13, 2003 (gmt 0)

#### Senior Member

joined:June 13, 2002
posts:2162

Looks like it should be:

total=(qty-1)*(price/2)+price

or with a,b,c:

a=(c-1)*(b/2)+ b

#### JonnyWales

2:04 pm on Apr 14, 2003 (gmt 0)

#### Junior Member

joined:Mar 12, 2002
posts:118

Thanks for the help. In the end I went to [forums.webdeveloper.com...] and paid somebody \$50 US to sort it out for me. Seems to have done the trick and does exactly what was needed.