Forum Moderators: open
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
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.
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
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 ....
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.