-
Notifications
You must be signed in to change notification settings - Fork 90
/
estimate-shipping-prices.php
136 lines (118 loc) · 4.3 KB
/
estimate-shipping-prices.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/*
In this tutorial we want to calculate our average shipping costs so
we set pricing for customers.
We have a sender address, a parcel and a set of delivery zip codes.
We will retrieve all available shipping rates for each delivery
address and calculate the min, max and average price for specific
transit time windows (next day, 3 days, 7 days).
Sample output:
For a delivery window of 1 days:
--> Min. costs: 5.81
--> Max. costs: 36.14
--> Avg. costs: 24.6875
For a delivery window of 3 days:
--> Min. costs: 5.81
--> Max. costs: 36.14
--> Avg. costs: 19.411666666667
For a delivery window of 7 days:
--> Min. costs: 5.81
--> Max. costs: 36.14
--> Avg. costs: 16.158888888889
Before running it, remember to do
composer install
*/
require_once(__DIR__ . '../../vendor/autoload.php');
// or if you do not have or want the composer autoload feature do
// require_once('path/to/shippo/library/folder/' . 'lib/Shippo.php');
// Replace <API-KEY> with your credentials from https://app.goshippo.com/api/
Shippo::setApiKey('<API-KEY>');
// Define delivery windows in max. days
// Pick an east coast, a west coast and a mid-west destination
$delivery_windows = array(1, 3, 7);
$destination_zip_codes = array('10007', '60290', '95122');
// Example from_address array
// The complete refence for the address object is available here: https://goshippo.com/docs/reference#addresses
$from_address = array(
'name' => 'Mr Hippo',
'company' => 'Shippo',
'street1' => '215 Clayton St.',
'city' => 'San Francisco',
'state' => 'CA',
'zip' => '94117',
'country' => 'US',
'phone' => '+1 555 341 9393',
'email' => 'mr-hippo@goshipppo.com',
);
// Parcel information array
// The complete reference for parcel object is here: https://goshippo.com/docs/reference#parcels
$parcel = array(
'length'=> '5',
'width'=> '5',
'height'=> '5',
'distance_unit'=> 'in',
'weight'=> '2',
'mass_unit'=> 'lb',
);
// Collect the shipments to each address
$shipments = array();
foreach ($destination_zip_codes as $zip_code)
{
// Example to_address with the zip code
// The complete refence for the address object is available here: https://goshippo.com/docs/reference#addresses
$to_address = array(
'country' => 'US',
'zip' => $zip_code,
);
// For each destination address we now create a Shipment object.
// async=false indicates that the function will wait until all rates are generated before it returns.
// The reference for the shipment object is here: https://goshippo.com/docs/reference#shipments
// By default Shippo API operates on an async basis. You can read about our async flow here: https://goshippo.com/docs/async
$shipments[] = Shippo_Shipment::create(array(
'address_from'=> $from_address,
'address_to'=> $to_address,
'parcels'=> array($parcel),
'async'=> false
));
}
// Collect all shipments rates
$all_rates = array();
foreach ($shipments as $shipment) {
$all_rates = array_merge($all_rates, $shipment['rates']);
}
// This function takes a list of $rates, filters only those rates in
// the $delivery_window, and returns the rates estimation
function calculate_rates_estimation($rates, $delivery_window) {
// Filter rates by delivery window
$eligible_rates = array_values(array_filter(
$rates,
function($rate) use($delivery_window){
return $rate['days'] <= $delivery_window;
}
));
// Calculate estimations on the eligible_rates
$min = $eligible_rates[0]['amount'];
$max = 0.0;
$sum = 0.0;
foreach ($eligible_rates as $rate) {
$amount = $rate['amount'];
$min = min($min, $amount);
$max = max($max, $amount);
$sum += $amount;
}
return array(
'delivery_window' => $delivery_window,
'min' => $min,
'max' => $max,
'average' => $sum / count($eligible_rates),
);
}
// Show estimations for each delivery window
foreach ($delivery_windows as $delivery_window) {
$estimations = calculate_rates_estimation($all_rates, $delivery_window);
echo "For a delivery window of {$delivery_window} days:" . "\n";
echo "--> " . "Min. costs: " . $estimations['min'] . "\n";
echo "--> " . "Max. costs: " . $estimations['max'] . "\n";
echo "--> " . "Avg. costs: " . $estimations['average'] . "\n";
}
?>