Gossamer Forum
Home : General : Perl Programming :

Setup shipping fees by continent

Quote Reply
Setup shipping fees by continent
I have a shopping cart and I'd like to define the shipping fees upon the continent (US-Canada / Europe / Other countries), where the items will be sent (USA = US, CA / EUR = AT, BE, BG, CZ, DK, EE * plus all the European countries / OTH = all the other countries, not listed above).

The code below works if I charge by country, but that means I have to enter the code for all the countries (not all are listed below) in the sub custom_shipping, which is a lot of lines of code.

I though to group the countries by continent, and define for each country to which continent it belongs, so instead of having $Ship_Country eq US, I could have $Ship_Continent eq EUR.

But I don't know exactly how to start… Does any one could help me and tell me how to do that?

Thanks a lot for your precious help!
Code:

sub custom_shipping
{
if ($Ship_Country eq US) {
$shipping_total = 9.00;
} elsif ($Ship_Country eq FR) {
$shipping_total = 19.00;
} else {
$shipping_total = 49.00;
}
}
sub country
{
local ($field);

%country_array =('USA', 'US',
'France', 'FR',
'Germany', 'GE',
'Ecuador','EC',
'Egypt','EG',
'El Salvador','SV',
'Equitorial Guinea','GQ',
'Eritrea','ER',
'Estonia','EE',
'Ethiopia','ET',
'Zimbabwe','ZW');

foreach $field ( sort ( keys %country_array ) )
{
if ($Bill_Country eq $country_array{$field})
{
$Bill_Country_Value = $field;
$Bill_Country_ABR = $country_array{$field};
}
if ($Ship_Country eq $country_array{$field})
{
$Ship_Country_Value = $field;
$Ship_Country_ABR = $country_array{$field};
}

if ($country_array{$field} eq $default_country && $Bill_Country eq "")
{
$bill_countries .= "<option selected value=\"$country_array{$field}\">$field</option>\n";
} else {
$bill_countries .= "<option value=\"$country_array{$field}\">$field</option>\n";
}
$ship_countries .= "<option value=\"$country_array{$field}\">$field</option>\n";
}

}
Quote Reply
Re: [Lotz] Setup shipping fees by continent In reply to
Hey there,

It's kinda hard to base an answer on seeing just a couple of functions out of context but here's an idea.
Use a multidimensional hash.

For example:
Code:
my %shippingRates = (
'USA' => {'DEFAULT' => 10.0, 'US' => 9.0, 'CA' => 9.5},
'EUR' => {'DEFAULT' => 22.0, 'AT' => 20.0, 'FR' => 19.0, 'BE' => 19.5, 'BG' => 20.0}
);
Note: I have no idea what values you would actually use so I just put random ones.

To define the value for continents not in your hash, you could use a subroutine like this:
Code:
sub GetShippingRate {
my ($continent, $country) = @_;
my $rate;
if (exists $shippingRates{$continent}) {
$country = 'DEFAULT' unless ($country && exists $shippingRates{$continent}{$country});
$rate = $shippingRates{$continent}{$country};
} else {
$rate = 49.00;
}
return $rate;
}

And get the rate like this:
Code:
print "Rate = ", GetShippingRate('USA', 'CA'), "\n";

Of course you can add some sort of validation given that continent codes are always three characters and country codes are always two characters.

Let me know if that helps.

Last edited by:

Crolguvar: Aug 26, 2004, 12:02 PM
Quote Reply
Re: [Crolguvar] Setup shipping fees by continent In reply to
Dear Crolguvar,

Thank you for your post.

As it was quite difficult for me to define it by continents, I left this idea and finally I figured it out differently. As there were not too many Europeans countries, I preferred to define all of them in the sub custom_shipping.

Not sure if it was the best solution, but it seems to work!

Thanks for your help.