We help IT Professionals succeed at work.

Custom Shipping Script by weight for my shopping cart

hotnixon2000
hotnixon2000 asked
on
I am trying to write a custom shipping by weight script for my shopping cart.  However, the script is not working correctly.  It is  calling the Shipping Method 1 when and item is 1lb or under.  It is calling Shipping Method 2 when the order is 1.1lbs to 3 lbs. and it is calling the Shipping Method 3 when the order is over 3 lbs.  This is not what I want at all.

Here's a breakdown of what I'm looking to do:

1. If the customer orders anything from 0.1 lbs to 5 lbs I want the shipping to be $3.50/lb

2. If the customer orders anything from 5.1 lbs to 10 lbs I want the shipping to be $2.75/lb.

3. If the customer orders anything from 10.1 lbs to 20 lbs I want the shipping to be $2.15/lb.

4. If the customer orders anything from 20.1 lbs and up I want the shipping to be $1.75/lb.

5. Anything with no weight, would be free shipping.

6. All Shipping methods should be displayed as FedEx Standard Overnight

I thought I had incorporated if and elsif statements to the attached code, perhaps I didn't do it correctly. I am not the most skilled PERL writer so I believe I probably forgot some things as far as creating variables.  I greatly appreciate any assistance in this issue.


##################################################################################################
#########
######### This script bases price on the cart weight.
######### There is a charge per pound.
#########
######### A listing of available variables:
#########
######### $ship_items_found Count Of shippable items
#########
######### $ship_methods_found Count of items with a
######### shipping method already
#########
######### $cart_items_found Count of rows in the cart
#########
######### $cart_quantity_found Total quantity of items
######### in the cart
#########
######### $tracking_subtotal Subtotal of the prices for
######### all items in the cart
#########
######### $cart_total_weight Total weight of all items
######### in the cart in pounds.
#########
######### Directly below, enter in the name for this
######### method to be displayed to the user.
#########

my $ship_meth_name = "Shipping Method 1";
my $price_per_pound = "3.50";
my $ship_total = "0.00";
my $ship_display = "";

my $ship_meth_name1 = "Shipping Method 2";
my $price_per_pound1 = "2.75";
my $ship_total = "0.00";
my $ship_display = "";

my $ship_meth_name2 = "Shipping Method 3";
my $price_per_pound2 = "2.15";
my $ship_total = "0.00";
my $ship_display = "";

#########
######### Figure out what the shipping charge will be by
######### multiplying the weight by the price per pound.
#########

$ship_total = ($price_per_pound * $cart_total_weight);
$ship_total1 = ($price_per_pound1 * $cart_total_weight);
$ship_total2 = ($price_per_pound2 * $cart_total_weight);

#########
######### Format the $ship_total as a price.
#########

$ship_total = sprintf("%.2f", $ship_total);
$ship_total1 = sprintf("%.2f", $ship_total1);
$ship_total2 = sprintf("%.2f", $ship_total2);


#########
######### Create the display based on the price.
#########

if (($ship_total <= "4") && ($ship_total > "0")) {

$ship_display = "$ship_meth_name - $currency_symbol$ship_total";

} elsif (($ship_total <= "10") && ($ship_total > "4")) {

$ship_display = "$ship_meth_name1 - $currency_symbol$ship_total1";

} elsif ($ship_total > "10") {

$ship_display = "$ship_meth_name2 - $currency_symbol$ship_total2";

} else {

$ship_display = "FREE Shipping";

} ######### End of if statement.

#########
######### Print the HTML display for the shipping
######### charge.
#########

print <<ENDOFTEXT;

<CENTER>

<TABLE WIDTH="100\%" CELLPADDING="10" CELLSPACING="3">

<TR BGCOLOR="$html_pri_tablerow_color">

<TD VALIGN="TOP"><FONT FACE="$html_small_font_face" SIZE="$html_small_font_size" COLOR="$html_small_font_color"><B>Shipping Method</b> <FONT COLOR="$html_notnull_font_color"><B>$html_notnull_character</b></font><BR><BR></font>

<INPUT TYPE="RADIO" NAME="shipinfo" VALUE="$ship_meth_name\:$ship_total" CHECKED> <FONT FACE="$html_base_font_face" SIZE="$html_base_font_size" COLOR="$html_base_font_color">

$ship_display

</font></td>

</tr>

<TR BGCOLOR="$html_alt_tablerow_color">

<TD VALIGN="TOP"><FONT FACE="$html_small_font_face" SIZE="$html_small_font_size" COLOR="$html_small_font_color">Total weight is $cart_total_weight.</td></tr>
</table>

</center>

<BR>
ENDOFTEXT
Comment
Watch Question

Commented:
Not quite the way I would have done it but if you replace this bit with the following it should do what you want...

#########
######### Create the display based on the price.
#########

if (($cart_total_weight <= "5") && ($cart_total_weight > "0")) {

$ship_display = "$ship_meth_name - $currency_symbol$ship_total";

} elsif (($cart_total_weight <= "10") && ($cart_total_weight > "4")) {

$ship_display = "$ship_meth_name1 - $currency_symbol$ship_total1";

} elsif ($cart_total_weight > "10") {

$ship_display = "$ship_meth_name2 - $currency_symbol$ship_total2";

} else {

$ship_display = "FREE Shipping";

} ######### End of if statement.


If you want the price bands as in your question with an extra at >20lb you will need to add this as another shipping method and add another clause to your if statement.

Commented:
Sorry - you should change the 4 or the 5 in the if statement so they match up depending on where you want the first boundary.

Author

Commented:
Works like a charm!  Thank you.  I knew I was making it more complicated than it really was.

Commented:
No problem and thanks for the points - I'm sure you would have cracked it but sometimes a fresh look at something makes all the difference.