Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP help with VBScript function 1

Status
Not open for further replies.

sdagger2

Programmer
May 5, 2008
39
GB
Hi

I am building a shopping cart which is more or less complete but I am having difficulty trying to create a function to amend quantities. It should be a basic function but I'm not sure on the best way to do it.

I am trying to code a function where before submitting the cart to paypal, I want to check for quantities of an item so 2 items are free for every 12 items they buy.

For example:
Buy 1-10 items at £1 each

Buy 12 items and they they get 2 free. ie, qty is 12 but they pay £10. If they buy 14 items then they pay £12 as 2 are free if they spend more than £10 (they won't get 2 free if they only choose 10 items).

If they buy 24 items - they pay £20 (4 free).
If they buy 36 items - they pay £30 (6 free).
If they buy 38 items - they pay £32 (6 free + 2 @ £1).

So basically they get 2 items free for every 12 they buy.

My coding is not brilliant but any help to code a function to pass in qty and amount values and update amounts would be great.

Thank you any help offered.
 
Code:
<%

function Price(n)
dim p
p = 1  ' price = 1
Price = (n * p) - (int(n/12)* 2 * p)
end function


response.Write "12 = " & Price(12) & "<br>"
response.Write "14 = " &  Price(14) & "<br>"
response.Write "24 = " &  Price(24) & "<br>"
response.Write "36 = " &  Price(36) & "<br>"
response.Write "38 = " &  Price(38) & "<br>"


%>
 
Thank you Foxbox

You're a star. That worked exactly as it says on the tin. Brilliant help and much appreciated.

I may need to tweak it a little as found out that when displaying item lines in the cart, it worked lovely, ie, 1 item line showing 12 items at £1 would be £10 which is great. But then realised if I did three items (3 lines) each with a qty of 4 at £1 then each line would show £4 but I would have nowhere to show the reduced rate which would be £10 not £12.

I think I'll need to somehow add all the qty's together and display a final line before the totals saying something like multisave discount..then display £2 there.

Hmm.

Thanks for your great help.
 
sdagger2 said:
You're a star. That worked exactly as it says on the tin. Brilliant help and much appreciated.

This is where you give foxbox a star so others don't waste their time clicking into the thread when they know it's already be solved :)

I did it for you....


--------

GOOGLE is a great resource to find answers to questions like "how do i..."


--------
 
Oh sorry vicvirk. I didn't realise you can rate people or close subjects. I'll remember for next time.

 
Hi

FoxBox provided me with a great example of how I apply discounts to qty:

<%
function Price(n)
dim p
p = 1 ' price = 1
Price = (n * p) - (int(n/12)* 2 * p)
end function

response.Write "12 = " & Price(12) & "<br>"
response.Write "14 = " & Price(14) & "<br>"
response.Write "24 = " & Price(24) & "<br>"
response.Write "36 = " & Price(36) & "<br>"
response.Write "38 = " & Price(38) & "<br>"
%>

This is fantastic if it is always based on 1 = £1 but I have realised that by changing this integer value to pay currency (i,e. 1.50) then the calculations don't work anymore. Could anybody possibly provide any help with making it work with currency?

ie, say I had 13 items at 1 then after 12 it would be:
1 x 13 = £12 - 2 items = 10 + 1 extra = £11
that works ok but if they where 1.5 then it would be:
1.5 x 13 = £18.00 - 2 items = 15 + 1 extra = £16.50.

Thank you to anyone who can take the time to make the small tweak to the above function. I'm guessing it should be straight forward but i'm missing the plot somewhere and tearing hair out!!
 
Sorry. I think it is ok. I was passing in the value of the item in the function and not the qty. Sorry about that. I think I have cracked it now.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top