Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Writing a php script code for shipping
Old 02-21-2008, 01:09 PM Writing a php script code for shipping
Junior Talker

Posts: 3
Name: Heather
Trades: 0
I need help. I'm starting a small business website and the last thing to do before I publish is get a code for shipping. I don't know the first thing about php script codes.
I'm trying to set up shipping costs. I'm having problems with "Rule
(PHP script code) for this shipping method" I need a rule for something like $0-$25 = $7.99, $25.01-$50.00 = $10.99, $50.01-$75 = $12.99, $75.01-$100 = $14.99, $100.01-$150 = $17.99, $150.01... + = $20.00. Can you help me with a rule for this?
Any help is greatly appreciated!!!
goofiness is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-21-2008, 01:25 PM Re: Writing a php script code for shipping
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
This should get you started. Code is for PHP 5.

PHP Code:
<?php

$shipping_rates 
= array();

$shipping_rates[0] = new shippingTableRate(0,25,7.99);
$shipping_rates[1] = new shippingTableRate(25.01,50,10.99);
$shipping_rates[2] = new shippingTableRate(50.01,75,12.99);
$shipping_rates[3] = new shippingTableRate(75.01,100,14.99);
$shipping_rates[4] = new shippingTableRate(100.01,150,17.99);
$shipping_rates[5] = new shippingTableRate(150.01,-1,20.00); //Using -1 as upper limit to indicate infinity

function calculate_shipping($order_total) {
  global 
$shipping_rates;
  foreach (
$shipping_rates as $a_rate) {
    if (
$order_total >= $a_rate->from_amount && ($a_rate->to_amount == -|| $order_total <= $a_rate->to_amount)) {
      return 
number_format($a_rate->shipping_amount,2);
    }
  }
}
class 
shippingTableRate {
  public 
$from_amount 0;
  public 
$to_amount 0;
  public 
$shipping_amount 0;

  function 
__construct($from,$to,$cost) {
    
$this->from_amount $from;
    
$this->to_amount $to;
    
$this->shipping_amount $cost;
  }
}

echo 
"Shipping Cost:$".calculate_shipping(27.34);
?>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE

Last edited by JeremyMiller; 02-21-2008 at 01:31 PM..
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 02-21-2008, 01:31 PM Re: Writing a php script code for shipping
Junior Talker

Posts: 3
Name: Heather
Trades: 0
Thanks so very, very much, Jeremy. That was way more than I could hope for.
goofiness is offline
Reply With Quote
View Public Profile
 
Old 02-21-2008, 01:39 PM Re: Writing a php script code for shipping
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
No problem. I thought about it and decided that I could remove the loop. Here is the refactored code:
PHP Code:
<?php
define
('SHIPPING_TABLE_INCREMENT',25);

$shipping_rates = array();

$shipping_rates[0] = 7.99// 0 - 25
$shipping_rates[1] = 10.99// 25.01 - 50
$shipping_rates[2] = 12.99// 50.01 - 75
$shipping_rates[3] = 14.99// 75.01 - 100
$shipping_rates[4] = 17.99// 100.01 - 125
$shipping_rates[5] = 17.99// 125.01 - 150
$shipping_rates[6] = 20.00// 150.01+

function calculate_shipping($order_total) {
  global 
$shipping_rates;
  
$array_index floor($order_total/SHIPPING_TABLE_INCREMENT);
  if (((
$order_total*100) % (SHIPPING_TABLE_INCREMENT*100)) == 0) {
    
//$order_total is at upper end of range, so need to decrement $array_index by 1
    
$array_index--;
  }
  if (
$array_index >= count($shipping_rates)) {
    
//Take care of those that go beyond the array's end
    
$array_index count($shipping_rates) - 1;
  }
  return 
number_format($shipping_rates[$array_index],2);
}
echo 
"Shipping Cost:$".calculate_shipping(20)."<br />";
echo 
"Shipping Cost:$".calculate_shipping(25)."<br />";
echo 
"Shipping Cost:$".calculate_shipping(25.01)."<br />";
echo 
"Shipping Cost:$".calculate_shipping(123.67)."<br />";
echo 
"Shipping Cost:$".calculate_shipping(136.11)."<br />";
echo 
"Shipping Cost:$".calculate_shipping(270.34)."<br />";
?>

This could should run a bit more efficiently than the previous.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 02-21-2008, 01:50 PM Re: Writing a php script code for shipping
Junior Talker

Posts: 3
Name: Heather
Trades: 0
AAAAHHH!!! This is making me crazy and I'm sure that I'm wasting your time. I've been working on this for three days and it's still not accepting anything that I enter. Thanks anyway.
goofiness is offline
Reply With Quote
View Public Profile
 
Old 02-21-2008, 01:53 PM Re: Writing a php script code for shipping
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Sure. My code gives example usage for you. The refactored one even gives a couple of options.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to Writing a php script code for shipping
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.12382 seconds with 12 queries