...

View Full Version : Changing price in shopping cart based on additions



SABuddy
03-04-2003, 12:23 AM
Hi,

I sell, among other stuff, soap and on my website I want to give users the options to add additives to their soap. The additives cost extra. The shopping cart I am using allows me to "join" several options i.e. color, fragrance, etc. with the use of the name="product[]". My problem is that I don't know how to reference this field. I tried window.product[].value, etc. etc. Can anyone help me figure this out?

I also tried giving the user two radio buttons and depending on which one was checked, the script increased the price or left it alone. With that I wasn't able to check if the user chose an additive without clicking the "Additive" radio button first - meaning that the price increase would not happen.

I would appreciate any help!

Thank you.
Riva

glenngv
03-04-2003, 01:38 AM
you can do it like this:

document.formNameHere.elements["product[]"].value

the most common way is :

document.formNameHere.fieldNameHere.value

but since the field's name has special characters, you can't use that.

to check if radio button is checked:

if (document.formNameHere.radioNameHere[0].checked){
//radio button 1 is checked
}

I have given you the idea, you can expand on that to suit your needs.

SABuddy
03-04-2003, 01:45 AM
I will work on it and let you know if I get stuck again. Thanks!

:-)
Riva

SABuddy
03-04-2003, 01:52 AM
I tried alert(document.theForm.elements["product[]"].value) and it came up as undefined. ? ?

Thanks,
Riva

glenngv
03-04-2003, 01:59 AM
when do you call the alert? can you post the code?

SABuddy
03-04-2003, 02:08 AM
Sure. This is where I give the user the options:


Additives:<BR>($1 extra per bar)<BR>
<select size="1" name="product[]" onChange="newMsg()">
<option value="selected">--
<option value="Goatsmilk">Goatsmilk</option>
<option value="Honey">Honey</option>
<option value="Oatmeal">Oatmeal</option>
<option value="Goatsmilk and honey">Goatsmilk &amp; Honey</option></select>

And this is where I call the alert: (in the <head> section)

function newMsg()
{
alert(document.theForm.elements["product[]"].value);
}


Thanks!

Riva

glenngv
03-04-2003, 02:29 AM
you didn't say that it's a select field (or i didn't get it?). the code I gave you is for text fields, but that will also work in IE for select field. The correct way to get the value of the select is:

document.theForm.elements["product[]"].options[document.theForm.elements["product[]"].selectedIndex].value;

since that is called by the select element itself, you can simplify it like:

<select size="1" name="product[]" onChange="newMsg(this)">
...

function newMsg()
{
alert(this.options[this.selectedIndex].value);
}

SABuddy
03-04-2003, 03:02 AM
I tried both options and keep getting the following error:

"xxx is null or not an object"

Is there some way to make two fields and then append them together right before submit?

Thanks for the help.

Riva

glenngv
03-04-2003, 03:27 AM
can you post the code again? are you sure you have <form> tags?

SABuddy
03-04-2003, 03:48 AM
Sure. Here is more of the code:

<script language="JavaScript">
<!--
function newMsg()
{
alert(document.theForm.product.value);
}
-->
//-->
</script>
.
.
.
.
.
.
<form action="http://ww5.aitsafe.com/cf/add.cfm" method="post">
<input type="hidden" name="userid" value="******">
<input type="hidden" name="price" value="6.00">
<input type="hidden" name="product[]" value="Hummingbird soap">
<input type="hidden" name="return" value="http://pages.ivillage.com/thebubblebath/products.html">
<FONT SIZE="3" FACE="Arial">Select your fragrance:<BR>
<select size="1" name="product[]">
<FONT SIZE="3" FACE="Arial"><option value="selected">---
<OPTION VALUE="Apricot Freesia">Apricot Freesia</OPTION>
<OPTION VALUE="Baby Powder">Baby Powder</OPTION>
<OPTION VALUE="Banana Cream">Banana Cream</OPTION>
<OPTION VALUE="Bay Rum">Bay Rum</OPTION>
<OPTION VALUE="Butterscotch">Butterscotch</OPTION>
<OPTION VALUE="Cucumber Melon">Cucumber Melon</OPTION>
<OPTION VALUE="Freesia">Freesia</OPTION>
<OPTION VALUE="Freshly Cut Grass">Freshly Cut Grass</OPTION>
<OPTION VALUE="Fresh Brewed Coffee">Fresh Brewed Coffee</OPTION>
<OPTION VALUE="Gardenia">Gardenia</OPTION>
<OPTION VALUE="Georgia Peach">Georgia Peach</OPTION>
<OPTION VALUE="Hazelnut Cappuccino">Hazelnut Cappuccino</OPTION>
<OPTION VALUE="Kiwi">Kiwi</OPTION>
<OPTION VALUE="Lavender EO">Lavender ESSENTIAL OIL</OPTION>
<OPTION VALUE="Lemon, Lemon, Lemon">Lemon, Lemon, Lemon</OPTION>
<OPTION VALUE="Lemon Poundcake">Lemon Poundcake</OPTION>
<OPTION VALUE="Masculine Musk">Masculine Musk</OPTION>
<OPTION VALUE="Nutcracker">Nutcracker</OPTION>
<OPTION VALUE="Oatmeal, Milk and Honey">Oatmeal, Milk and Honey</OPTION>
<OPTION VALUE="Ocean Rain">Ocean Rain</OPTION>
<OPTION VALUE="Peppermint EO">Peppermint ESSENTIAL OIL</OPTION>
<OPTION VALUE="Pineapple">Pineapple</OPTION>
<OPTION VALUE="Plumeria">Plumeria</OPTION>
<OPTION VALUE="Strawberries and Cream">Strawberries and Cream</OPTION>
<OPTION VALUE="Sun Ripened Raspberry">Sun Ripened Raspberry</OPTION>
<OPTION VALUE="Sweet Juicy Pear">Sweet Juicy Pear</OPTION>
<OPTION VALUE="Tangerine">Tangerine</OPTION>
<OPTION VALUE="Tropical Paradise">Tropical Paradise</OPTION>
<OPTION VALUE="True Lilac">True Lilac</OPTION>
<OPTION VALUE="Unscented">Unscented</OPTION>
<OPTION VALUE="Vanilla Cream">Vanilla Cream</OPTION>
<OPTION VALUE="Warm Apple Pie">Warm Apple Pie</OPTION></select>
<BR><FONT FACE="Arial" SIZE=3>Color Preference:<BR>
<input type="text" name="product[]" size="23"><BR><BR>
Additives:<BR>($1 extra per bar) <BR>
<select size="1" name="product[]" onChange="newMsg(this)">
<option value="selected">--
<option value="Goatsmilk">Goatsmilk</option>
<option value="Honey">Honey</option>
<option value="Oatmeal">Oatmeal</option>
<option value="Goatsmilk and honey">Goatsmilk &amp; Honey</option></select></TD>
<TD VALIGN="top">
Quantity:<BR><input type="text" name="qty" size="2" value="1">
<BR><BR>
<DIV ALIGN="right">
<input type="submit" value="Add to Cart"><BR>
<input type="submit" value="View Cart"></DIV></form>

This is code I am working on on my computer. I haven't uploaded this yet.

Thanks,
Riva

glenngv
03-04-2003, 04:00 AM
use this:

function newMsg(objSelect)
{
alert(objSelect.options[objSelect.selectedIndex].value);
}

sorry, i gave you the wrong code in my earlier post. :o

why do you have many fields named "product[]", 2 selects and 1 text field. you can't have different field type having the same name. you can name the 2 selects the same but not along with the text field. is there some reasons you name them like that?

SABuddy
03-04-2003, 04:07 AM
I will try the new code you posted - thanks.

The "info" on how to use the shopping cart said that if you want to add options to your products, name another field "product[]" and it will append to the first one so you get all your options in the cart. It is from mal's-e-commerce.

:-)
Riva

SABuddy
03-04-2003, 04:10 AM
Thanks a bunch! This is great!!

:-)
Riva



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum