Paul Taylor wrote:
I have a shopping cart script with a pop up menu for the shipping
variables. I have two options at present, option 0, standard 'UK
Delivery' charge rate, option 1, for 'Collect'. The default is option
0.
I would like to add a third for 'Free Delivery' (which I have added).
However the option should only be available and selectable when the
sub total of the order is 75.00 or more. The option on the pop up
should also default to 'Free Delivery' when this amount or more is
reached. Like wise the 'Free Delivery' option should not be selectable
on amounts of 74.99 or less. Is this possible?
Can anybody help as this is a little beyond my scripting ability. Many
thanks.
Apparently. The code you have posted doesn't do much, it just sets up
some variables. Provide a link to the page and perhaps something can
be done, otherwise there simply isn't enough here to go on.
[...] function writeShipping() {
leng = 3;
options = new Array(leng);
options[0] = "UK Delivery";
options[1] = "Collect";
options[2] = "Free Delivery";
// **Begin code section for order totals calculations**
var shipCost = 0
// **Shipping charges.**
// **These definitions correspond to the shipping select**
// **options defined in the writeShipping() function.**
function shippingCost() {
shipCost = 0
Why initialise shipCost again?
itemUp = counter - 1 // variable to increment charges
Where is "counter" initialised/given a value?
multiplier = .5 // additional charge per item after first.
Does "multiplier" need to be global?
if (document.shopCart.ShipVia.options[0].selected) {
shipCost = 5.95 } // Standard Delivery
if (document.shopCart.ShipVia.options[1].selected) {
shipCost = 0.00 } // Collect
if (document.shopCart.ShipVia.options[2].selected) {
shipCost = 0.00 } // Free Delivery
Surely it would be better to grab the ShipVia elements, then iterate
through them to find out which is selected? And if the options are
given a value of the shipping cost, you get it from there so you only
maintain the value in one place.
A better way of writing the above is:
var fElements = document.forms['shopCart'].elements['ShipVia'];
for (var i=0; i<fElements.length; i++) {
if (fElements[i].selected) {
shipCost = fElements[i].value;
// This is a debug line, you don't want it in the finished code
alert('Selected: ' + i + ' cost $' + shipCost);
}
}
The related HTML may look like:
<form name="shopCart">
<select name="ShipVia">
<option value="5.95">Standard Delivery<br>
<option value="0.00">Collect<br>
<option value="0.00">Free Delivery<br>
</select>
<input type="button" value="Click me" onclick="shippingCost();">
</form>
Of course I would do shippingCost(this.form) to save myself having to
hard-code the form name in the shippingCost function, but that's up to
you.
Of course, when you get this at the server you have to do all this work
again just in case the user has spoofed your input page...
}
*/
document.shopCart.shippingcharge.value = fix(with_Shipping)
Presumably there is a function fix() somewhere?
Where is "with_Shipping" initialised/given a value?
transaction_amount = sub_total + with_Shipping
{
document.shopCart.transactionamount.value = fix(transaction_amount)
}
}
}
Does "transaction_amount" need to be global?
Where is "sub_total" initialised/given a value?
At some point somewhere you have worked out subtotal. At that point you
can enable/disable your shipping options depending on the value of the
sub_total:
if (sub_total >= 75) {
document.forms['shopCart'].elements['ShipVia'].options[2].enabled = true;
document.forms['shopCart'].elements['ShipVia'].options[0].enabled = false;
} else {
document.forms['shopCart'].elements['ShipVia'].options[2].enabled = false;
document.forms['shopCart'].elements['ShipVia'].options[0].enabled = true;
}
All totally untested of course, since I have absolutely nothing to test
against. Please don't implement this on a commercial site until you
are utterly certain of what you are doing and all entries and values
are verified on the server - or you will be ripped-off.
Have fun - Fred.