Link to home
Start Free TrialLog in
Avatar of Jim_Bob_Joe
Jim_Bob_Joe

asked on

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
Avatar of archrajan
archrajan

can u explain a bit more...
Avatar of Jim_Bob_Joe

ASKER

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
This is what i have got so far
<script>
function dothis(min,multiples)
{

var quan = document.getElementById('quantity')
if(Math.floor(quan.value) <  min)
{
alert("Your quantity is less than the minimum quantity");
quan.select();
quan.focus();
return;

}
var temp = quan.value % multiples
if(temp != 0)
{
alert("Your quantity should be in multiples of\n" +multiples);
quan.select();
quan.focus();
return;
}
}

</script>
</HEAD>

<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..
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
dam sorry pressed tab then enter and forgot where i was typing.

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

if orderqty < minorder then
{
alert('Please check your order quantity against the minimum order qty')
return false;
}
if orderqty / multipleof ! wholenumber then
{
alert('Please make sure your order quantity is in multiples of ' + multiples of)
return false
}
PriceBreak = split("1,49,1.50;50,99,1.35;100,499,1.25",";")  'sorry about this some is VB but hope you understand what i mean
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
    {
        alert('Price is' + splitbreak(3))
        return true;
    }
loop
'got here so must have not found value in list so
alert('price break not found, please contact us for a quote');
return false;

again if your not already to confused see if that helps.
Ok how abt this one?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<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)
{
alert("Your quantity is less than the minimum quantity");
quan.select();
quan.focus();
return;

}
var temp = quan.value % multiples
if(temp != 0)
{
alert("Your quantity should be in multiples of\n" +multiples);
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>
</HEAD>

<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>
Oh i understand now what u r trying to say..
let me give that a shot
Ok this is what i have now
<HTML>
<HEAD>
<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)
{
alert("Your quantity is less than the minimum quantity");
quan.select();
quan.focus();
return;

}
var temp = quan.value % multiples
if(temp != 0)
{
alert("Your quantity should be in multiples of\n" +multiples);
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;
alert(document.getElementById('price').value)
break;
}
}
}

</script>
</HEAD>

<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>
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)
{
alert("Your quantity is less than the minimum quantity");
quan.select();
quan.focus();
return;

}
var temp = quan.value % multiples
if(temp != 0)
{
alert("Your quantity should be in multiples of\n" +multiples);
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)
{
alert("Out of range, Please contact us");
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;
alert(document.getElementById('price').value)
break;
}
}
}

</script>
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
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)
{
alert("Out of range, Please contact us");
return;
}
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>
<HEAD>
<TITLE> New Document </TITLE>
<script>
function dothis(quan,min,multiples,PriceBreak)
{
if(Math.floor(quan.value) <  min)
{
alert("Your quantity is less than the minimum quantity");
quan.select();
quan.focus();
return false;

}
var temp = quan.value % multiples
if(temp != 0)
{
alert("Your quantity should be in multiples of\n" +multiples);
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;
}
}
alert('Quantity not found in price breaks, please contact us for a quote.')
return false
}
</script>
</HEAD>
<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>
Here is the working version:
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script>
function dothis(quan,min,multiples,PriceBreak)
{
if(Math.floor(quan.value) <  min)
{
alert("Your quantity is less than the minimum quantity");
quan.select();
quan.focus();
return false;

}
var temp = quan.value % multiples
if(temp != 0)
{
alert("Your quantity should be in multiples of\n" +multiples);
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)
{
alert("Out of range, Please contact us");
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>
</HEAD>
<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>

      
      
ASKER CERTIFIED SOLUTION
Avatar of archrajan
archrajan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK all works a treat, have added it in and tested it in my site.

Thankx alot.

Jim
U r welcome anytime