# Shopping Cart Price Breaks, Multiples of, Min order qty

I am looking for a function that when passed something like
TheFunction("300;100;100;1,99,0.59-100,499,0.49-500,999,0.45")
TheFunction("Order Qty;Minimum Order Qty;Multiples Of Qty;Price Breaks")
Price Breaks are "MinQty,MaxQty,Price"
the list of price breaks can be 1 to infinity

Then for this function to return true if all is ok so quantity is over minimum qty, fits in multiples of and a price was found in the array otherwise alert on the error.

Hope i explained it OK.

Let me know ppl

Regards
Jim
LVL 3
###### Who is Participating?

Commented:
This is a brain breaking question ;)
the problem that 50 0r 100 will not work because

if(quant > min && quant < max)
should be

if(quant >= min && quant <= max)
0

Commented:
can u explain a bit more...
0

Author Commented:
it is for a shopping cart. All of the items have a minimum order quantity which needs to be met, some have to be order in multiples of 100 or so, some have price breaks, so if you spend more then they dont cost you as much.

I am looking for a function that will check all of these and give me the price.

So something like

Item: My First Item
Minimum Order Qty: 25
Multiples Of: 5
Price Breaks
1 - 49 £1.50
50 - 99 £1.35
100 - 499 £1.25

So I have a text box where they enter the amount then want, then when they click the add button i want it to check the following

That the order qty is greater than the minimum order qty.
The order qty is a multiple of the multiple of qty.
then work out the price break(so if i want 50 then will give me a price of £1.35 this wil be set in a hidden form item and set when i click the add button)
If these checks are not met then should message error such as order qty not greater than min order, must be in multiples of *, price break value not found, please contact for pricing details.
if does match then set the price in a hidden form item say called "Price" and then return true.

But the price breaks can sometimes be just 1 which would be 1 - 999999 sometimes could have a couple sometimes could have several so this needs to be a array.

Hope this helps
0

Commented:
This is what i have got so far
<script>
function dothis(min,multiples)
{

var quan = document.getElementById('quantity')
if(Math.floor(quan.value) <  min)
{
quan.select();
quan.focus();
return;

}
var temp = quan.value % multiples
if(temp != 0)
{
quan.select();
quan.focus();
return;
}
}

</script>

<BODY>
<input type = "text" id = "quantity"><input type = "button" value = "add" onclick = "dothis('25','5');">
<input type = "hidden" id = "price">
</BODY>

///But the price breaks can sometimes be just 1 which would be 1 - 999999 sometimes could have a couple sometimes could have several so this needs to be a array.

i am not sure how to get around with this, that shud be infinite and it might break the code..
0

Author Commented:
If it doesnt fit in the price break then i just want ti to tell the customer to contact us, thats what happens in our internal systems, if it isnt in our price beaks then a special quote is made up, we only have price breaks for orders we can do, if they order more than 999999 then we got problems as were not likely to have that much stock in so they get given a new lead time with there quote.

So i dont know if it is just possible to break up the price break array, check if it is greater than the first value and less than th second if so then set the price if not loop through it, if it gets to the end and does not find a price then just tell them to go away.

here is how i c it but not much of a javascript person so need it converting.

Variables OrderQty, MinOrder, MultipleOf

if orderqty < minorder then
0

Author Commented:
dam sorry pressed tab then enter and forgot where i was typing.

Variables OrderQty, MinOrder, MultipleOf, MinOrder, MaxOrder, PriceBreak, SplitBreak

if orderqty < minorder then
{
return false;
}
if orderqty / multipleof ! wholenumber then
{
return false
}
for each pricebreak in price break '(so first would be 1,49,1.50)
splitbreak = split(pricebreak,",")
minorder = splitbreak(1) '(should be 1)
maxorder = splitbreak(2) '(should be 49)
if orderqty => minorder and orderqty <=maxorder then
{
return true;
}
loop
return false;

0

Commented:
Ok how abt this one?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<TITLE> New Document </TITLE>
<script>
var price0 = new Array("1","49","1.50")
var price1 = new Array("50","99","1.35")
var price2= new Array("100","499","1.25")
function dothis(quan,min,multiples)
{

if(Math.floor(quan.value) <  min)
{
quan.select();
quan.focus();
return;

}
var temp = quan.value % multiples
if(temp != 0)
{
quan.select();
quan.focus();
return;
}

for(i =0; i <3;i++)
{
var quant = quan.value * 1

var pri = eval("price"+i)
if(quant > pri[0] && quant < pri[1])
{
document.getElementById('price').value =pri[2]

break;
}
}
}

</script>

<BODY>
<input type = "text" id = "quantity"><input type = "button" value = "add" onclick = "dothis(document.getElementById('quantity'),'25','5');">
<input type = "hidden" id = "price">
</BODY>
</HTML>
0

Commented:
Oh i understand now what u r trying to say..
let me give that a shot
0

Commented:
Ok this is what i have now
<HTML>
<TITLE> New Document </TITLE>
<script>
var price0 = new Array("1","49","1.50")
var price1 = new Array("50","99","1.35")
var price2= new Array("100","499","1.25")
function dothis(quan,min,multiples)
{

if(Math.floor(quan.value) <  min)
{
quan.select();
quan.focus();
return;

}
var temp = quan.value % multiples
if(temp != 0)
{
quan.select();
quan.focus();
return;
}

var quant = quan.value * 1

var PriceBreak = "1,49,1.50;50,99,1.35;100,499,1.25"
var temp2 = PriceBreak.split(';')
for(i=0; i <temp2.length; i++)
{
var temp3 = temp2[i].split(',')
var min = temp3[0]
var max = temp3[1]
var price = temp3[2]
if(quant > min && quant < max)
{
document.getElementById('price').value = price;
break;
}
}
}

</script>

<BODY>
<input type = "text" id = "quantity"><input type = "button" value = "add" onclick = "dothis(document.getElementById('quantity'),'25','5');">
<input type = "hidden" id = "price">
</BODY>
</HTML>
0

Commented:
This is much more better, included the out of range error message also
<script>
var price0 = new Array("1","49","1.50")
var price1 = new Array("50","99","1.35")
var price2= new Array("100","499","1.25")
function dothis(quan,min,multiples)
{

if(Math.floor(quan.value) <  min)
{
quan.select();
quan.focus();
return;

}
var temp = quan.value % multiples
if(temp != 0)
{
quan.select();
quan.focus();
return;
}

var quant = quan.value * 1
var pricebreak1 = "1,49,1.50;50,99,1.35;100,499,1.25"
var pricebreak2 = pricebreak1.split(',')
var minfinal = pricebreak2[0]*1
var maxfinal = pricebreak2[pricebreak2.length-2]*1

if(quant < minfinal || quant > maxfinal)
{
return;
}
var PriceBreak = "1,49,1.50;50,99,1.35;100,499,1.25"
var temp2 = PriceBreak.split(';')
for(i=0; i <temp2.length; i++)
{
var temp3 = temp2[i].split(',')
var min = temp3[0]
var max = temp3[1]
var price = temp3[2]
if(quant > min && quant < max)
{
document.getElementById('price').value = price;
break;
}
}
}

</script>
0

Author Commented:
Yeah thats better as then can pass it in the function

have changed the line
if(quant > min && quant < max)

to

if(quant >= min && quant <= max)

else does not let you order on the price break

but would just like to that when it gets to the end of the price breaks and does not find anything then it alerts and returns false
0

Commented:
for that reason i modified my code..
instead of allowing it to go inside the loop and then not find anything, determine it before sending it to the loop, did u test the code  i posted last?
this part:
var pricebreak1 = "1,49,1.50;50,99,1.35;100,499,1.25"
var pricebreak2 = pricebreak1.split(',')
var minfinal = pricebreak2[0]*1
var maxfinal = pricebreak2[pricebreak2.length-2]*1

if(quant < minfinal || quant > maxfinal)
{
return;
}
0

Author Commented:
yeah i tried that, but if someone tries to order 50, 100 then it cannot find it in the price breaks.

this all seams to work

<HTML>
<TITLE> New Document </TITLE>
<script>
function dothis(quan,min,multiples,PriceBreak)
{
if(Math.floor(quan.value) <  min)
{
quan.select();
quan.focus();
return false;

}
var temp = quan.value % multiples
if(temp != 0)
{
quan.select();
quan.focus();
return false;
}

var quant = quan.value * 1
var temp2 = PriceBreak.split(';')
for(i=0; i <temp2.length; i++)
{
var temp3 = temp2[i].split(',')
var min = temp3[0]
var max = temp3[1]
var price = temp3[2]
if(quant > min && quant < max)
{
document.getElementById('price').value = price;
return true;
break;
}
}
return false
}
</script>
<BODY>
<input type = "text" id = "quantity"><input type = "button" value = "add" onclick = "if(dothis(document.getElementById('quantity'),'25','5','1,49,1.50;50,99,1.35;100,499,1.25')){alert('Adding '+document.getElementById('quantity').value + ' at £' + document.getElementById('price').value)};">
<input type = "hidden" id = "price">
</BODY>
</HTML>
0

Commented:
Here is the working version:
<HTML>
<TITLE> New Document </TITLE>
<script>
function dothis(quan,min,multiples,PriceBreak)
{
if(Math.floor(quan.value) <  min)
{
quan.select();
quan.focus();
return false;

}
var temp = quan.value % multiples
if(temp != 0)
{
quan.select();
quan.focus();
return false;
}

var quant = quan.value * 1
var pricebreak1 = PriceBreak
var pricebreak2 = pricebreak1.split(',')
var minfinal = pricebreak2[0]*1
var maxfinal = pricebreak2[pricebreak2.length-2]*1

if(quant < minfinal || quant > maxfinal)
{
return;
}
var temp2 = PriceBreak.split(';')
for(i=0; i <temp2.length; i++)
{
var temp3 = temp2[i].split(',')
var min = temp3[0]*1
var max = temp3[1]*1
var price = temp3[2]*1

if(quant >= min && quant <= max)
{
document.getElementById('price').value = price;
return true;
break;
}
}

}
</script>
<BODY>
<input type = "text" id = "quantity"><input type = "button" value = "add" onclick = "if(dothis(document.getElementById('quantity'),'25','5','1,49,1.50;50,99,1.35;100,499,1.25')){alert('Adding '+document.getElementById('quantity').value + ' at £' + document.getElementById('price').value)};">
<input type = "hidden" id = "price">
</BODY>
</HTML>

0

Author Commented:
OK all works a treat, have added it in and tested it in my site.

Thankx alot.

Jim
0

Commented:
U r welcome anytime
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.