...

View Full Version : Extanding functionality need HELP???



dmitryseliv
12-21-2005, 02:24 AM
Hi !
Get stumped by this little deal.
I'm using javascript shopping cart(nebucart). I just added new product that has a free shipping but script doesn't have ability to configure shipping for each product.
But it has a function which allow to use custom shipping rules.
New product has id 'dvd'. I need help with additing some new rule for this new product 'dvd' so shipping for this product is $0.00

Appriciate Any Help!!!

=====================================================
HERE IS THE FUNCTION
====================================================

function calcShipping(){
// BEGIN CUSTOM SHIPPING RULES
var numBoxSets = 0;
var tmpSnH = 0;

// get the country we're shipping to.
if(shipeeArray.length > 0){
tmpCountry = shipeeArray[8];
} else {
tmpCountry = shopperArray[8];
}
// see if they ordered the box set
for(itmC = 0; itmC < Cart.length; itmC++){
if(Cart[itmC].prodID == 'boxset'){
numBoxSets = Cart[itmC].qty;
break;
}
}
// calc the shipping $3.00 per item
tmpSnH = (3 * totalQty);
// if we're shipping outside the US,
// add $2.00 per item -> $5.00 each item
if(tmpCountry != 'US'){
tmpSnH += (2 * totalQty);
// if we're outside the US and they ordered the box set,
// add another $5.00 -> $10.00 each box set
if(numBoxSets > 0){
tmpSnH += (5 * numBoxSets);
}

} else {
// if they have the box set and inside the US,
// add another $2.00 => $5.00 per box set
if(numBoxSets > 0){
tmpSnH += (2 * numBoxSets);
}

}
return tmpSnH;


// END CUSTOM SHIPPING RULES


}



=====================================================

Pyth007
12-21-2005, 01:53 PM
New product has id 'dvd'.
Which property is this refering to? Is it a web-field's id or is it an item's prodID? What I'm getting at is that in the function, there is a reference to a user-defined property called 'prodID':

if(Cart.prodID == 'boxset'){
If 'dvd' is the value of prodID, then you can simply add in an if-statement to adjust the shipping... However, an 'id' attribute is a common HTML-defined property, in which case we might need to see the HTML (at least for this object) to help you better.

Also, it looks like this function adjusts shipping for the entire order (as you said, no way to adjust shipping on a per-item basis). Were you wanting $0 shipping charge if they ordered a 'dvd', reguardless of whatever else they had purchased (ie as in some type of promotion: "You purchase the DVD and we'll pay for [I]all of your shipping!") or did you mean that if 'dvd' was the only item purchased, then shipping was $0?

If 'dvd' is the prodID and you want $0 shipping if this is the only item purchased, then here is a possible solution (only showing a snippet of your function...):

// see if they ordered the box set or only the dvd
var isDVD=false;
var isOtherItem=false;
for(itmC = 0; itmC < Cart.length; itmC++){
if(Cart[itmC].prodID == 'dvd')
{
isDVD=true;
} else {
isOtherItem = true;
if(Cart[itmC].prodID == 'boxset'){
numBoxSets = Cart[itmC].qty;
break;
} // end inner if-statement
} // end outer if-statement
} // end for-loop
if(isDVD && (!isOtherItem)) return 0; // returns $0 SnH if 'dvd' is only item


You could also do some boolean logic to use only one variable... set var isDVD=true; before the for-loop, and then:


if(Cart[itmC].prodID == 'dvd')
{
isDVD=isDVD && true;
} else {
isDVD=isDVD && false;
// other stuff for 'boxset'...
} // end outer if-statement
} // end for-loop
if(isDVD) return 0; // returns $0 SnH if 'dvd' is only item

dmitryseliv
12-21-2005, 06:21 PM
Hi Pyth007!

Thanks a lot for you Respond.
I meant only for DVD item shipping should be free for the rest Items rules are not changed. Like if somebody adds to cart one ItemX they pay $3 for shipping in US and outside US $5 and if they added DVD item they pay $0.00 S/H only for DVD.

Yes dvd is value of prodID.
Actually I added <input hidden value='' name'prodID'> in one of the JS scripts that creates HTML output.

I tried to use if statements but I'm not JavaScript expert I'm using it time to time.

So any way it looks like - return tmpSnH;
Overwritten any changes I tried to apply.

You can see it in action here it's testing area - http://www.mickeynewbury.com/nebucart-old/dvd.html
http://www.mickeynewbury.com/nebucart-old/

Shipping calculations happends on this page - http://www.mickeynewbury.com/nebucart-old/gateway.html
it's the last page before it goes to Payment Gateway.

The shipping function sits here - <script language="javascript" src="js/NebuCart.js"></script>
Line 573.

dmitryseliv
12-23-2005, 12:12 AM
Well finally!
I figure it out. As I said I'm not JavaScript expert.
So it takes a while for me to get it done.
Any way this what I come up with.


// see if they ordered dvd and ordered outside US

var tmp_SnH = 0;

for(itmC = 0; itmC < Cart.length; itmC++){
if(Cart[itmC].prodID == 'dvd' && tmpCountry == 'US'){
tmp_SnH += (0 * totalQty);
return tmpSnH + (tmp_SnH - shipAmt);
}else{
return tmpSnH + (tmp_SnH - 5);
}
}



// END CUSTOM SHIPPING RULES

Pyth007
12-23-2005, 02:12 PM
I hate to "rain on your parade" but I just tested out your site and found a logical error with it... I "purchased" 5 DVD's, 2 boxsets, and 1 album ("silver"). Since I didn't provide any shipping info (I just went to the "gateway" that you posted above), my country != "us" so I should expect:
$5 per non-dvd item + $5 per boxset = ($5 * 3 non-dvd items) + ($5 * 2 boxsets) = $15 + $10 = $25
However, I got $45 S/H as the final calculation... I'm guessing that the extra $20 comes from 4 DVD's being counted as non-dvd items (thus you are only accounting for 1 dvd...). I couldn't quite understand what the solution you posted did, but I think I figured out how to work with the function that you had posted before...

In the calcShipping() function, there was one variable that was not defined, but I think I'm beginning to understand what it means: I'm assuming that totalQty is the total number of items in the cart. It is this value that keeps getting multiplied by the s/h charges per item. However, if an item is a dvd, then I'm assuming that you effectively what this item subtracted from the total number of items in the cart (so that its quantity doesn't add to the s/h amount). However, we can't just subtract 1 from totalQty in case the person ordered more than 1 dvd, so we would need to subtract the quantity of dvd's from totalQty. In the function, the quantity of boxsets was determined by:

numBoxSets = Cart[itmC].qty;

Thus we could add a similar variable (say "numDVDs"), and set that when the prodID = 'dvd'. Finally, because totalQty was not defined inside this function, it must be a global variable. Since the global variable may have other uses (as well as problems with recalculating s/h), we should make a copy of this value before changing it inside the function (say "var copyTotalQty=totalQty;") Thus the function would change to (bolded line I added):


function calcShipping()
{
// BEGIN CUSTOM SHIPPING RULES
var numBoxSets = 0;
var numDVDs = 0;
var copyTotalQty=totalQty;
var tmpSnH = 0;

// get the country we're shipping to.
if(shipeeArray.length > 0)
{
tmpCountry = shipeeArray[8];
} else {
tmpCountry = shopperArray[8];
}
// see if they ordered the box set or dvds
for(itmC = 0; itmC < Cart.length; itmC++)
{
if(Cart[itmC].prodID == 'boxset')
{
numBoxSets = Cart[itmC].qty;
// Delete "break;" line since we need to search all items for both dvds and boxsets
} else {
if(Cart[itmC].prodID == 'dvd')
{
numDVDs = Cart[itmC].qty;
} // end DVD if
} // end boxset if
} // end for-loop
copyTotalQty -= numDVDs; // This might also be done above in the for-loop

// calc the shipping $3.00 per item
tmpSnH = (3 * copyTotalQty);
// if we're shipping outside the US,
// add $2.00 per item -> $5.00 each item
if(tmpCountry != 'US')
{
tmpSnH += (2 * copyTotalQty);
// if we're outside the US and they ordered the box set,
// add another $5.00 -> $10.00 each box set
if(numBoxSets > 0)
{
tmpSnH += (5 * numBoxSets);
}
} else {
// if they have the box set and inside the US,
// add another $2.00 => $5.00 per box set
if(numBoxSets > 0)
{
tmpSnH += (2 * numBoxSets);
}
}
return tmpSnH;
// END CUSTOM SHIPPING RULES
}

dmitryseliv
12-23-2005, 07:01 PM
You right!

It was a logical error. Hey at least I tried.
Yep. Your changes solved the problem.

Appriciate you help a lot.

Have a Mery Mery Christmas!
Cheers DS :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum