Kryptronic Software Support Forum

You are viewing this forum as a guest. Login to an existing account, or create a new account, to reply to topics and to create new topics.

#1 01-21-2009 21:57:16

stage
Member
Registered: 12-12-2005
Posts: 170

help with custom shipping script

Pursuant to yesterday's thread of mine, I've decided to stick with value-based shipping charges, even for international customers.  But I need the calculations to be different, and I have no programming knowledge (other than common sense and unix admin experience), so I hope somebody here (who is that most likely to be? haven't a clue :-) will help me.

Here is the basis of my current script (not written by me):

Code:

my $ship_meth_name1 = "UPS Ground";
my $ship_meth_name2 = "UPS Economy Air";
my $ship_meth_name3 = "UPS Express Air";
my $ship_total1 = "5.95";
my $ship_total2 = "30.00";
my $ship_total3 = "40.00";
my $ship_display1 = "";
my $ship_display2 = "";
my $ship_display3 = "";
my $ship_20percent = "";
my $ship_25percent = "";
my $comment_fee= "";

#########
######### Figure out what 20% and 30% of total
######### shipping charges would be
#########

$ship_20percent = (".2"*$item_subtotal);
$ship_30percent = (".3"*$item_subtotal);

#########
######### Figure out what the shipping charge will be by
######### using if statements and less than/greater than
######### logic.
#########

if ($ship_20percent >= "30") {

$ship_total2 = "$ship_20percent";

}
if ($ship_30percent >= "40") {

$ship_total3 = "$ship_30percent";


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

As you can see, I'm charging a $5.95 flat fee for Ground; for Economy Air I'm charging 20% with $30 minimum; for Express Air I'm charging 30% with $40 minimum.

This is for USA ship addresses only.  I would like to replicate this for all other countries, except with different figures.

Rachel provided this "if" statement, to separate foreign from domestic:

Code:

if ($fd_trackitem_shipcountry eq $site_owner_country) {

This would go AFTER the main code above, correct?

And then after this statement I put another copy of the main code, but with the values changed appropriately to reflect foreign shipping?  Is that correct?  With a "}" at the end to finish the "if" statement?

Answers to the above would be much appreciated.

Nathan

Offline

 

#2 01-21-2009 22:33:00

rachaelseven
Member
From: Massachusetts, USA
Registered: 01-23-2006
Posts: 3169
Website

Re: help with custom shipping script

Hi Nathan,

The basic structure you're looking for to separate your calculations into foreign and domestic would go like this:

Code:

if ($fd_trackitem_shipcountry eq $site_owner_country) {

put domestic calculations here

} else {

put international calculations here

} ## endif

If you're looking for more specific than just foreign/domestic, you could do a structure like this:

Code:

if ($fd_trackitem_shipcountry eq $site_owner_country) {

put domestic calculations here

} elsif ($fd_trackitem_shipcountry eq "country name") {

put country specific calculations here

} elsif ($fd_trackitem_shipcountry eq "country name") {

put country specific calculations here

} elsif ($fd_trackitem_shipcountry eq "country name") {

put country specific calculations here

} elsif ($fd_trackitem_shipcountry eq "country name") {

put country specific calculations here

} else {

put calculations here in case none of the specific countries were matched.

} ## endif

That's the basic idea, but if you need more specific advice, feel free to ask.  I'll be checking on the forum again in about 12 hours.  Good luck!


Rachael Katz
- Custom Focusing Screens for DSLR Cameras

Offline

 

#3 01-22-2009 09:12:16

stage
Member
Registered: 12-12-2005
Posts: 170

Re: help with custom shipping script

Thanks, that's very helpful.  And thanks also for the second bit of code because I did want to do something different for Canada.

Can one complete if/else statement be nested within another?  If so, can you please show me an example of that?

And one more question - "## endif" is a comment for clarity? or a required part of the code?

Thanks,
Nathan

Offline

 

#4 01-22-2009 09:28:51

rachaelseven
Member
From: Massachusetts, USA
Registered: 01-23-2006
Posts: 3169
Website

Re: help with custom shipping script

You're very welcome.  The "## endif" is just a comment.  It can be omitted, but I find it helpful when I'm trying to match up braces in longer bits of code, so it's just become habit.  And yes, if/else statements can be nested.  An example might be:

Code:

my $ship_meth_name = "";
my $ship_total = "0.00";
my $ship_display = "";

if ($fd_trackitem_shipcountry eq $site_owner_country) {

  $ship_meth_name = "Domestic Shipping Name";
  if ($item_subtotal >= "0" && $item_subtotal <= "30.00") {
    $ship_total = "10.50";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } elsif ($item_subtotal > "30.00" && $item_subtotal <= "60.00") {
    $ship_total = "15.50";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } elsif ($item_subtotal > "60.00" && $item_subtotal <= "90.00") {
    $ship_total = "22.50";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } elsif ($item_subtotal > "90.00" && $item_subtotal <= "120.00") {
    $ship_total = "33.75";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } elsif ($item_subtotal > "120.00" && $item_subtotal <= "150.00") {
    $ship_total = "37.25";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } elsif ($item_subtotal > "150.00" && $item_subtotal <= "200.00") {
    $ship_total = "40.00";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } else {
    $ship_total = "0.00";
    $ship_display = "Please contact us at $html_site_name for your shipping price.";
  } ## endif

} elsif ($fd_trackitem_shipcountry eq "Canada") {

  $ship_meth_name = "Canadian Shipping Name";
  if ($item_subtotal >= "0" && $item_subtotal <= "30.00") {
    $ship_total = "10.50";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } elsif ($item_subtotal > "30.00" && $item_subtotal <= "60.00") {
    $ship_total = "15.50";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } elsif ($item_subtotal > "60.00" && $item_subtotal <= "90.00") {
    $ship_total = "22.50";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } elsif ($item_subtotal > "90.00" && $item_subtotal <= "120.00") {
    $ship_total = "33.75";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } elsif ($item_subtotal > "120.00" && $item_subtotal <= "150.00") {
    $ship_total = "37.25";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } elsif ($item_subtotal > "150.00" && $item_subtotal <= "200.00") {
    $ship_total = "40.00";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } else {
    $ship_total = "0.00";
    $ship_display = "Please contact us at $html_site_name for your shipping price.";
  } ## endif

} else {

  $ship_meth_name = "Everywhere Else Shipping Name";
  if ($item_subtotal >= "0" && $item_subtotal <= "30.00") {
    $ship_total = "10.50";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } elsif ($item_subtotal > "30.00" && $item_subtotal <= "60.00") {
    $ship_total = "15.50";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } elsif ($item_subtotal > "60.00" && $item_subtotal <= "90.00") {
    $ship_total = "22.50";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } elsif ($item_subtotal > "90.00" && $item_subtotal <= "120.00") {
    $ship_total = "33.75";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } elsif ($item_subtotal > "120.00" && $item_subtotal <= "150.00") {
    $ship_total = "37.25";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } elsif ($item_subtotal > "150.00" && $item_subtotal <= "200.00") {
    $ship_total = "40.00";
    $ship_display = "$ship_meth_name - $currency_symbol$ship_total";
  } else {
    $ship_total = "0.00";
    $ship_display = "Please contact us at $html_site_name for your shipping price.";
  } ## endif

} ## endif

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

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

#########
######### Return the HTML in the variable 
######### $custom_code_result.
#########

$custom_code_result = <<ENDOFTEXT;

<INPUT TYPE="RADIO" NAME="shipinfo-$shipid" 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<BR></FONT>

ENDOFTEXT

Rachael Katz
- Custom Focusing Screens for DSLR Cameras

Offline

 

#5 02-12-2009 16:22:08

jrfisher
Member
Registered: 02-12-2009
Posts: 2

Re: help with custom shipping script

rachaelseven wrote:

The "## endif" is just a comment...

Naturally, and I share your practice.

Hi Rachael, my name is Jeff. Nathan has asked me to take on this task for him, and if successful, I'll do more. I am an experienced server-side script programmer (PHP), so I will know structured and O-O principles (e.g. the code snips in this thread make sense to me).

However, I haven't used Perl in 10 years, and I am coming into the cart code cold, so I'll probably ask Q's of ignorance while climbing those learning curves. Feel free to assign me reading homework...

} elsif ($fd_trackitem_shipcountry eq "Canada") {

* Newbie Perl Q: Are these string comparisons case sensitive?

* Is there a canonical list of country and state names used by the cart, or must I try to match fat-fingered user entries?

* Where can I find an exhaustive list of predefined cart variables available as hooks for further 'if' statements? (e.g. state/province name or abbreviation)

Thanks, I'm off to find a Perl reference and brush up on its syntax / idiosyncrasies.

-- Jeff Fisher

Last edited by jrfisher (02-13-2009 17:40:06)

Offline

 

#6 02-12-2009 17:10:44

rachaelseven
Member
From: Massachusetts, USA
Registered: 01-23-2006
Posts: 3169
Website

Re: help with custom shipping script

Hi Jeff,

If you can do PHP, you won't have much problem with PERL... at least in terms of the basic stuff you need to write shipping scripts and the like.  PERL has some odd syntactical idiosyncrasies when you get deeper into it, but I doubt you'll mess with much of that in working with CCP unless you get into some heavy mods of the core.  In answer to your questions:

- Yes, the string comparison you quoted is case sensitive.  If you would like a case-insensitive way, you could always use a regex comparison (one of PERL's biggest strengths):

Code:

} elsif ($fd_trackitem_shipcountry =~ /canada/i) {

- The country and state names used by the cart are stored in tables - country.csv and stateprov.csv if the cart is operating in CSV mode or simply the country and stateprov tables if running in an alternate DB mode (usually MySQL).

- There is no exhaustive, prepared list, I'm afraid.  State/province abbreviation won't be populated at shipping script time - that would take another call to the DB to retrieve.  But some of the others you might be interested in for shipping script purposes are:

$fd_trackitem_shipfirstname
$fd_trackitem_shiplastname
$fd_trackitem_shipaddressone
$fd_trackitem_shipaddresstwo
$fd_trackitem_shipcity
$fd_trackitem_shipstateprov
$fd_trackitem_shipzip
$item_quantity (Total quantity of items)
$item_subtotal (Subtotal for all items)
$item_total_weight (Total weight of all items)

Most of them are pretty self-explanatory and I commented the couple that aren't.  There are several example shipping scripts accessible through the admin (Shipping Settings -> Manage Custom Shipping Methods) that will make for light reading.  There are also a TON of them on the forum that you can certainly pull up in searches (make sure you stay with the v5.1 scripts, obviously).  Good luck and if you have any questions, feel free to ask!


Rachael Katz
- Custom Focusing Screens for DSLR Cameras

Offline

 

#7 02-12-2009 17:14:45

rachaelseven
Member
From: Massachusetts, USA
Registered: 01-23-2006
Posts: 3169
Website

Re: help with custom shipping script

One other thought - if you want to see some of the global variables available, like $site_owner_country, have a look at the settings.csv table - the first column is the variable name (precede with $ in PERL context obviously) and all of those are available globally.  You can also take a look at the hidden form fields on the shipping method selection page (with a tool like the web developer toolbar for Firefox) to see what's there as well.


Rachael Katz
- Custom Focusing Screens for DSLR Cameras

Offline

 

#8 02-12-2009 18:07:34

jrfisher
Member
Registered: 02-12-2009
Posts: 2

Re: help with custom shipping script

rachaelseven wrote:

some of the others you might be interested in for shipping script purposes are:

$fd_trackitem_shipfirstname
$fd_trackitem_shiplastname
$fd_trackitem_shipaddressone
$fd_trackitem_shipaddresstwo
$fd_trackitem_shipcity
$fd_trackitem_shipstateprov
$fd_trackitem_shipzip
$item_quantity (Total quantity of items)
$item_subtotal (Subtotal for all items)
$item_total_weight (Total weight of all items)

Thanks.

Viewing the source from a customer PoV showed me the canonical values possible for the drop-boxes. I think I have enough to start.

if you have any questions, feel free to ask!

Can you recommend a tool or method for analyzing new Perl before exposing it to the world? F'rinstance, if I first install it as a new script, can I expose it only to a special condition that only my tests would trigger?

Thanks

Offline

 

#9 02-12-2009 19:01:48

rachaelseven
Member
From: Massachusetts, USA
Registered: 01-23-2006
Posts: 3169
Website

Re: help with custom shipping script

You're welcome.  First, you can check the syntax of your script by either installing an open source PERL (like active state perl) on your local machine or else with  handy syntax checker.  Then, once you know you have a syntactically valid script, just make a test product and assign the new script to that product.  Don't put the product in any categories or give it any search terms and it won't come up for customers.  But by entering the product URL directly, you can add the new product to the cart and go through the checkout (you only need to go to step 2 to see your script work).


Rachael Katz
- Custom Focusing Screens for DSLR Cameras

Offline

 

Board footer