|
<<prev |
1 |
2 |
3 |
4 |
5 |
next>>
Smart Shipping Based on Country - Adding the code
The lines of code we are adding are as follows: (new code in bright blue, lines we may need to change in red, existing code in dark blue for reference.)
function UpdateShipping(i)
{
// make a copy so value doesn't change in calling function.
var nIndex = i;
if(isFramed)
{
// RealCartUniversity Smart
// Shipping Based on Country
// added following 30 lines.
// Assign Country pull-down selection ID to var nIdx.
// See http://courses.realcartu.com/math/shipping/smartship/
// Page 5 for important info about this next line:
var nIdx = eval(document.pay.country.selectedIndex);
if(nIdx != 2) // Home Country NOT selected
{
if(nIdx == 0) // Country NOT selected - return
{
alert("Please choose country.");
return false;
}
else
{
// 0 and 1 are the "Domestic" Shipping Methods.
// change these values as needed for your situation.
// if((i == 0) || (i == 1) || (i == 2) || (i == 3))
if((i == 0) || (i == 1))
{
// changes the shipping method to a
// default international shipping method
nIndex = 2; // 3rd ship option
document.pay.shipCombo.selectedIndex = 2;
alert("International Shipping Selected.");
}
}
}
// end RealCartUniversity Smart
// Shipping Based on Country
var nHandlingFee = shipOption[nIndex].HandlingFee;
var nShipCharge = GetShippingCharge(nIndex);
var nTotalShipCharge = nShipCharge + nHandlingFee;
The first line in red above references the country field in the checkout page. It may need to be changed depending on how you have RealCart configured for checkout. The line we are referring to is this:
var nIdx = eval(document.pay.country.selectedIndex);
Depending on which checkout method you have specified in RealCarts Checkout Wizard, the above line may or may not need to be changed. If you are using any of the following checkout methods then no change will need to be made:
- My NT Server
- My UNIX Server
- PayPal
- PayPal (for NT)
- PayPal (for UNIX)
- eCheck
- EMS Ecommerce
- WorldPay JR
If the checkout method you are using is NOT listed above then you WILL need to make changes to the line of code based on which checkout method you are using. See page 5 for complete details.
Making the other needed changes
Above you will note the bit of code that determines which shipping method is selected. This brings us to the next red line that needs to be modified. It looks like this:
// 0 and 1 are the "Domestic" Shipping Methods.
// change these values as needed for your situation.
// if((i == 0) || (i == 1) || (i == 2) || (i == 3))
if((i == 0) || (i == 1))
{
// changes the shipping method to a
// default international shipping method
nIndex = 2; // 3rd ship option
document.pay.shipCombo.selectedIndex = 2;
alert("International Shipping Selected.");
}
Note that the red line above if((i == 0) || (i == 1)) is really the one that determines if a domestic shipping method is selected. This is because i stands for the position of the Shipping pull-down menu. The first position, the 0 position, is the default shipping method while 1 is the second method. RealCart wants to know if the first one -OR- the second one is selected. OR is signified by the double vertical lines (pipes)||.
(i == 0) is checking to see if the first method is chosen OR (||) the second one is chosen with (i == 1). We will need to change this line to test for the specific domestic shipping methods you are using.
The if statement syntax is as follows: You have to have a set of parenthesis around the question like this:
if( condition to be tested )
In between the outside parenthesis (where it says condition to be tested) we add a series of these tests:
(i == 0) || (i == 1) || (i == 2)
Each test is asking:
Is the first (0) shipping method selected?
Is the second (1) shipping method selected?
Is the third (2) shipping method selected?
You could just as easily have it test to see if the 3rd, 7th or 9th shipping method is chosen (in case your domestic shipping options are not right together) like this:
(i == 2) || (i == 6) || (i == 8)
Keep in mind there that JavaScript starts counting at 0 not at 1. If any of these tests turns out to be true then the code continues on. In our example we are changing the shipping option from the default 0 or whichever domestic shipping option was chosen to our first international option: 2.
In our example, our domestic shipping methods are the first two, 0 and 1, with our international methods being third (2) and fourth (3). If your domestic shipping methods are 0, 1, 2 and 3 (in other words, the first 4 you defined) then you would use the line of code that looks like this instead of the one that is being used in our previous example above:
if((i == 0) || (i == 1) || (i == 2) || (i == 3))
{
// changes the shipping method to a
// default international shipping method
nIndex = 4; // 5th ship option
document.pay.shipCombo.selectedIndex = 4;
alert("International Shipping Selected.");
}
The if statement is literally evaluating this logic:
If i (the index of the currently chosen shipping method) is the first one, or if i is the second one, or if i is the third one, or if i is the fourth one...
then RealCart will change the default shipping to whatever you specify below the if statement.
The final two lines to change. Last of the red lines
Lets say you have six shipping methods and the first four are all domestic. Only 5 and 6 are international. Since the first one is 0, 2nd one is 1, and so on then our code would need to be modified to resemble the following:
if((i == 0) || (i == 1) || (i == 2) || (i == 3))
{
// changes the shipping method to a
// default international shipping method
nIndex = 4; // 5th ship option
document.pay.shipCombo.selectedIndex = 4;
alert("International Shipping Selected.");
}
This will cause RealCart to default to the 5th shipping method if a country other than your home country is chosen. If you want RealCart to default to the 6th method then you would use:
if((i == 0) || (i == 1) || (i == 2) || (i == 3))
{
// changes the shipping method to a
// default international shipping method
nIndex = 5; // 6th ship option
document.pay.shipCombo.selectedIndex = 5;
alert("International Shipping Selected.");
}
Lastly RealCart will show this message to your customer:
International Shipping Selected.
and will then charge the amount for the 5th shipping option.
<<prev |
1 |
2 |
3 |
4 |
5 |
next>>
|