Complex Shipping Rules

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eskimo42
Forum Newbie
Posts: 1
Joined: Thu May 24, 2007 1:18 pm

Complex Shipping Rules

Post by eskimo42 »

I am looking to develop a function for my client’s ecommerce solution. The set of ‘shipping rules’ is kind of complex, and I am perplexed at how exactly I should approach a solution. My client sells a study guide as a complete set, or she sells each 6 sections individually. In total there are 7 products which can be ordered. Let me first give an example of how shipping is determined:



Complete Set:


Continental USA:

-UPS Ground $12
-2nd Day Air: $20

Alaska, Hawaii, Puerto Rico, USVI:

-USPS Mail: $20.00
-Global Express: $28.50

International:
-Real-time Quotes from USPS

Section Orders:


Continental USA:
1-2 Sections:
-USPS Priority Mail: $8
-3+ Sections
-USPS Priority Mail: $11

International, Alaska, Hawaii, Puerto Rico, USVI:

-1-2 Sections:
-Not Yet Determined
-3+ Sections
-Not Yet Determined


In the end I want a function where I could pass needed arguments, and then return a $ShippingPrice.

To do this in if/else and case statements seems completely illogical and I’m sure most we’ll agree. I was thinking to set a database up and query to get rates the schema might look something like this:

Table: Shipping Rates
Country | State | Method | CompleteSetCost | SectionCost1-2 | SectionCost 3+

USA | NY | Ground | 12.00 | 8.00 | 11.00
USA | NY | 2nd Day| 20.00 | Don't Do | NULL?

Also, the client is unable/unwilling to change the method of determining shipping, so that's out of the question. I’m really not sure how to approach this, but any help would be much appreciated.


Thank You,

Evan
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Your know what your arguments have to be and what the logic is to select the appropriate record and choose which category it is. Something like this should do it:

Code: Select all

//...assuming you are already connected to the database:
function get_shipping ($country, $state, $method, $product) {
  $sql="SELECT * FROM tblShipping WHERE `country`='$country' AND `state`='$state' AND `method`='$method'";
  $result=mysql_query($sql) or die("Select Query failed");
  $row=mysql_fetch_assoc($result);
  extract($row);
  switch ($product) {
    case 'CompleteSet':
      $price=$completesetcost;
    break;
    case 'Sections1-2':
      $price=$sectioncost1-2;
    break;
    case 'Sections3+':
      $price=$sectioncost3+;
    break;
    case default:
      $price="N/A";
  }
  return $price;
}
Post Reply