Click to See Complete Forum and Search --> : Setup shipping fees by continent


Lotz
08-23-2004, 03:15 PM
Hello,

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!

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";
}

}

Nedals
08-24-2004, 09:04 PM
Do you need the abbr?


use strict; ## I recomend you start using this for all your Perl scripts

my @country_array ( ## already sorted since you are typing in the list
'USA',9.00,
'France',19.00,
..
..
'Zimbabwe',34.50
);

my ($bill_countries,$ship_countries) = ("","");

for (my $i=0; $i<(@country_array/2); $i++) { ## Skipping over the price
my $sel = ($default_country eq $country_array[$i*2]) ? ' selected' : '';
## Using qq saves having to 'escape' the ""
$bill_countries .= qq{ <option value="$i"$sel>$country_array[$i*2]</option>\n };
$ship_countries .= qq{ <option value="$i">$country_array[$i*2]</option>\n };
}

Now your select will look like this
<select name="thecountries">
<option value="0">USA</option>
<option value="1">France</option>
etc,
</select>

When the form is submitted the value for 'thecountries' will be numeric

my $country_value = $q->param('thecountries'); ## or something similar
my $ship_amount = $country_array[($country_value*2)+1];

Hope that helps!

Lotz
08-25-2004, 10:13 AM
Dear Nedals,

Thanks, but yes, I definitely need the abbr, as they are used somewhere else in the script and by the payment gateway as well.

As there are not too many Europeans countries, I preferred to define all of them in the sub custom_shipping, instead of trying to find a solution by continent.

But now, I'd like to configure the 2 shipping options: standard or priority.

Nothing to change if the standard option is chosen.

But, if the priority option (Shipping_option) is chosen (radio button checked), then, I'd like to have this:

- if "priority" checked, and if total_shipping price = 9.00 -> then the new price should be 15.00
- if "priority" checked, and if shipping_country is US or CA -> then the new price should be 30.00
- if "priority" checked, and if total_shipping price = 25.00 -> then the new price should be 45.00

I was able to figure out only this, which add +15.00 on the total price if "priority" is checked. But it applies to all countries, which is not really what I need. :confused:


if (!($Shipping_option eq "standard"))
{
$shipping_total += 15.00;
}


Could you please help me? I'm not familiar with this kind of conditions, so I would really need your help.

Many thanks

Nedals
08-25-2004, 01:08 PM
I'm a little confused here

Why do you want to use the sub custom_shipping at all? Everthing can be included in the @country_array

The priority option does not seem to add up. You say you want to add $15.00 if priority BUT
this does not reflect that....
- if "priority" checked, and if total_shipping price = 9.00 -> then the new price should be 15.00
- if "priority" checked, and if shipping_country is US or CA -> then the new price should be 30.00
- if "priority" checked, and if total_shipping price = 25.00 -> then the new price should be 45.00

Here's what I would do.


## Change the array as follows...
my @country_array ( ## Country, ABBR, Std Ship, Priority Ship
'USA','US',9.00,15.00,
'France','FR',19.00,34.00,
..
..
'Zimbabwe','ZW',34.50,49.50
);

## In the for loop use 4 instead of 2
for (my $i=0; $i<(@country_array/4); $i++) { ## Skipping over the price
my $sel = ($default_country eq $country_array[$i*4]) ? ' selected' : '';
## Using qq saves having to 'escape' the ""
$bill_countries .= qq{ <option value="$i"$sel>$country_array[$i*4]</option>n };
$ship_countries .= qq{ <option value="$i">$country_array[$i*4]</option>n };
}

## Extract data for this entry
my $country_value = $q->param('thecountries'); ## or something similar
my $abbr = $country_array[($country_value*4)+1];
my $ship_amount = ($Shipping_option eq "standard")
? $country_array[($country_value*4)+2]
: $country_array[($country_value*4)+3];

Lotz
08-25-2004, 03:09 PM
Dear Nedals,

Thank you for your help.

By default, the shipping fees are calculated per item, but it is possible to define customised shipping fees under the sub custom_shipping. That's why I used it.

Finally I found a solution that seems to work, but I just need to have advises from Perl expert to check if the syntax is correct.

Here is the code I put in the sub custom_shipping (I haven't put the complete list of the European countries here, but it is in my script):


sub custom_shipping
{
if ($Ship_Country eq US) {
$shipping_total = 19.00;
} elsif ($Ship_Country eq CA) {
$shipping_total = 19.00;
} elsif ($Ship_Country eq FR) {
$shipping_total = 9.00;
} elsif ($Ship_Country eq BE) {
$shipping_total = 9.00;
} elsif ($Ship_Country eq IT) {
$shipping_total = 9.00;
} else {
$shipping_total = 25.00;
}

if (($Shipping_option eq "priority")&&($ShipTo_Country eq US)){
$shipping_total = 30.00;
} elsif (($Shipping_option eq "priority")&&($ShipTo_Country eq CA)){
$shipping_total = 30.00;
} elsif (($Shipping_option eq "priority")&&($ShipTo_Country eq FR)){
$shipping_total = 15.00;
} elsif (($Shipping_option eq "priority")&&($ShipTo_Country eq BE)){
$shipping_total = 15.00;
} elsif (($Shipping_option eq "priority")&&($ShipTo_Country eq IT)){
$shipping_total = 15.00;
} elsif ($Shipping_option eq priority){
$shipping_total = 45.00;
}
}


Thanks

silent11
08-25-2004, 04:01 PM
aren't you forgetting some quotes?


...
if ($Ship_Country eq US) {
...




try


...
if ($Ship_Country eq "US") {
...

Lotz
08-26-2004, 05:56 AM
Thanks.

I added the quotes everywhere.

It was also working without the quotes...

Now, with the quotes, is the syntax correct?

Thanks!