UPS 运费计算器
我已经成功使用 PHP 运行 UPS 运输计算器。我现在发现 UPS 的标准运输上限为 150 磅。对于超过 150 磅的任何物品,我需要通过货运运输。
有人用 UPS 编写过运费计算器吗?我为 150 磅以下物品编写的计算器不需要 UPS 帐户。我希望对运费计算器做同样的事情,但是,我无法找到有关如何完成此操作的任何信息。
任何帮助将不胜感激。
编辑
根据 Nicolaesse 的要求,我添加了一些我在 2009 年编写的代码片段来解决我的问题。
演示可以在这里看到: http://cart.jgallant.com(将产品添加到购物车,结帐,然后输入邮政编码以估算运费)
核心 UPS 类别:
<?php
class ups {
function getMethod() {
$method= array(
'1DA' => 'Next Day Air',
'2DA' => '2nd Day Air',
'3DS' => '3 Day Select',
'GND' => 'Ground',
);
return $method;
}
function get_item_shipping($weight, $zipcode)
{
unset($_SESSION['zipcode_error']);
if($weight > 150) {
$_SESSION['zipcode_error'] = "A cart item requires freight.";
}
if (!isset($_SESSION['zipcode_error'])) {
$services = $this->getMethod();
$ch = curl_init();
foreach ($services as $key => $service) {
$Url = join("&", array("http://www.ups.com/using/services/rave/qcostcgi.cgi?accept_UPS_license_agreement=yes",
"10_action=3",
"13_product=".$key,
"14_origCountry=US",
"15_origPostal=90210",
"19_destPostal=" . $zipcode,
"22_destCountry=US",
"23_weight=" . $weight,
"47_rate_chart=Regular+Daily+Pickup",
"48_container=00",
"49_residential=2",
"billToUPS=no")
);
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$Results[]=curl_exec($ch);
}
curl_close($ch);
$pre = "";
$shipping_list = array();
foreach($Results as $result) {
$result = explode("%", $result);
if ($services[$result[1]] != ''){
if ((($result[1]=='XPR') && ($pre == 'XPR')) || (($result[1]=='XDM') && ($pre == 'XDM')) || (($result[1]=='1DP') && ($pre == '1DP')) || (($result[1]=='1DM') && ($pre == '1DM')) || (($result[1]=='1DA') && ($pre == '1DA')) || (($result[1]=='2DA') && ($pre == '2DA')))
$shipping_list += array($services[$result[1]."L"] => $result[8]);
else if (($result[1]=='GND') && ($pre == 'GND'))
$shipping_list += array($services[$result[1]."RES"] => $result[8]);
else
$shipping_list += array($services[$result[1]] => $result[8]);
$pre = $result[1];
}
}
}
$shipping_list = array_reverse($shipping_list);
return $shipping_list;
}
}
?>
重量计算器助手:
<?php
//UPS calculator - Divides packages into 150lbs max packages, and requests multiple quotes if needed
//Returns a sum of all the quotes added together.
private function calculate_per_item_shipping() {
$currWeight = 0;
$packageCount = 0;
//Loop thru cart items - adding weights to packages
foreach($this->get_contents() as $item) {
for ($i = 0; $i < $item["qty"]; $i++) {
$itemWeight = $item["weight"];
if ($itemWeight > 150) { // A single item cannot be more than 150lbs
$_SESSION['zipcode_error'] = $item['name'] . " weighs more than 150lbs.";
return false;
}
else {
$currWeight += $itemWeight; //Add item weight to active package
if ($currWeight > 150) { //Max weight reached for active package
$currWeight -= $itemWeight; //Remove item from current package, too heavy
$loopPack = 0;
$itemUsed = false;
//Check if an existing package can take the item
while (($loopPack != $packageCount) or ($itemUsed = false)) {
if ($packages[$loopPack] + $itemWeight < 150) {
$packages[$loopPack] += $itemWeight;
$itemUsed = true;
}
$loopPack++;
}
//if the item didn't fit in an existing package, create a new package for it
if ($itemUsed == false) {
$packageCount++;
$packages[$packageCount-1] = $currWeight;
$currWeight = $item["weight"]; //Put unused item back in active package
}
}
}
}
}
//The remainder becomes a package
$packageCount++;
$packages[$packageCount-1] = $currWeight;
for ($i = 0; $i < $packageCount; $i++) {
$temptotal = $this->calc_package_shipping($packages[$i]);
if ($temptotal['Ground'] != 0) { $total['Ground'] += $temptotal['Ground']; }
if ($temptotal['3 Day Select'] != 0) { $total['3 Day Select'] += $temptotal['3 Day Select']; }
if ($temptotal['2nd Day Air'] != 0) { $total['2nd Day Air'] += $temptotal['2nd Day Air']; }
if ($temptotal['Next Day Air Saver'] != 0) { $total['Next Day Air Saver'] += $temptotal['Next Day Air Saver']; }
if ($temptotal['Next Day Air Early AM'] != 0) { $total['Next Day Air Early AM'] += $temptotal['Next Day Air Early AM']; }
if ($temptotal['Next Day Air'] != 0) { $total['Next Day Air'] += $temptotal['Next Day Air']; }
}
$this->selectedQuoteAmount = $total['Ground'];
return $total;
}
private function calc_package_shipping($weight) {
$ups = new ups();
$shipping = $ups->get_item_shipping($weight, $this->zipcode);
return $shipping;
}
?>
I have managed to get a UPS Shipping calculator working in PHP. I now found out that the standard shipping from UPS is capped at 150lbs. For anything over 150lbs, I need to ship via freight.
Has anyone ever coded a Freight Calculator with UPS? The calculator I wrote for anything under 150lbs does not require a UPS account. I was hoping to do the same for the freight calculator, however, I am unable to find any information on how to accomplish this.
Any help will be greatly appreciated.
EDIT
As per request by Nicolaesse, I have added some code snippets that I wrote in 2009 to solve my problem.
Demo can be seen here:
http://cart.jgallant.com (Add product to cart, checkout, and enter zipcode for shipping estimate)
Core UPS class:
<?php
class ups {
function getMethod() {
$method= array(
'1DA' => 'Next Day Air',
'2DA' => '2nd Day Air',
'3DS' => '3 Day Select',
'GND' => 'Ground',
);
return $method;
}
function get_item_shipping($weight, $zipcode)
{
unset($_SESSION['zipcode_error']);
if($weight > 150) {
$_SESSION['zipcode_error'] = "A cart item requires freight.";
}
if (!isset($_SESSION['zipcode_error'])) {
$services = $this->getMethod();
$ch = curl_init();
foreach ($services as $key => $service) {
$Url = join("&", array("http://www.ups.com/using/services/rave/qcostcgi.cgi?accept_UPS_license_agreement=yes",
"10_action=3",
"13_product=".$key,
"14_origCountry=US",
"15_origPostal=90210",
"19_destPostal=" . $zipcode,
"22_destCountry=US",
"23_weight=" . $weight,
"47_rate_chart=Regular+Daily+Pickup",
"48_container=00",
"49_residential=2",
"billToUPS=no")
);
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$Results[]=curl_exec($ch);
}
curl_close($ch);
$pre = "";
$shipping_list = array();
foreach($Results as $result) {
$result = explode("%", $result);
if ($services[$result[1]] != ''){
if ((($result[1]=='XPR') && ($pre == 'XPR')) || (($result[1]=='XDM') && ($pre == 'XDM')) || (($result[1]=='1DP') && ($pre == '1DP')) || (($result[1]=='1DM') && ($pre == '1DM')) || (($result[1]=='1DA') && ($pre == '1DA')) || (($result[1]=='2DA') && ($pre == '2DA')))
$shipping_list += array($services[$result[1]."L"] => $result[8]);
else if (($result[1]=='GND') && ($pre == 'GND'))
$shipping_list += array($services[$result[1]."RES"] => $result[8]);
else
$shipping_list += array($services[$result[1]] => $result[8]);
$pre = $result[1];
}
}
}
$shipping_list = array_reverse($shipping_list);
return $shipping_list;
}
}
?>
WEIGHT Calculator Helper:
<?php
//UPS calculator - Divides packages into 150lbs max packages, and requests multiple quotes if needed
//Returns a sum of all the quotes added together.
private function calculate_per_item_shipping() {
$currWeight = 0;
$packageCount = 0;
//Loop thru cart items - adding weights to packages
foreach($this->get_contents() as $item) {
for ($i = 0; $i < $item["qty"]; $i++) {
$itemWeight = $item["weight"];
if ($itemWeight > 150) { // A single item cannot be more than 150lbs
$_SESSION['zipcode_error'] = $item['name'] . " weighs more than 150lbs.";
return false;
}
else {
$currWeight += $itemWeight; //Add item weight to active package
if ($currWeight > 150) { //Max weight reached for active package
$currWeight -= $itemWeight; //Remove item from current package, too heavy
$loopPack = 0;
$itemUsed = false;
//Check if an existing package can take the item
while (($loopPack != $packageCount) or ($itemUsed = false)) {
if ($packages[$loopPack] + $itemWeight < 150) {
$packages[$loopPack] += $itemWeight;
$itemUsed = true;
}
$loopPack++;
}
//if the item didn't fit in an existing package, create a new package for it
if ($itemUsed == false) {
$packageCount++;
$packages[$packageCount-1] = $currWeight;
$currWeight = $item["weight"]; //Put unused item back in active package
}
}
}
}
}
//The remainder becomes a package
$packageCount++;
$packages[$packageCount-1] = $currWeight;
for ($i = 0; $i < $packageCount; $i++) {
$temptotal = $this->calc_package_shipping($packages[$i]);
if ($temptotal['Ground'] != 0) { $total['Ground'] += $temptotal['Ground']; }
if ($temptotal['3 Day Select'] != 0) { $total['3 Day Select'] += $temptotal['3 Day Select']; }
if ($temptotal['2nd Day Air'] != 0) { $total['2nd Day Air'] += $temptotal['2nd Day Air']; }
if ($temptotal['Next Day Air Saver'] != 0) { $total['Next Day Air Saver'] += $temptotal['Next Day Air Saver']; }
if ($temptotal['Next Day Air Early AM'] != 0) { $total['Next Day Air Early AM'] += $temptotal['Next Day Air Early AM']; }
if ($temptotal['Next Day Air'] != 0) { $total['Next Day Air'] += $temptotal['Next Day Air']; }
}
$this->selectedQuoteAmount = $total['Ground'];
return $total;
}
private function calc_package_shipping($weight) {
$ups = new ups();
$shipping = $ups->get_item_shipping($weight, $this->zipcode);
return $shipping;
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您为什么不直接获取一个UPS API 帐户并请求最多- 直接从 UPS 获取日期费率?
Why wouldn't you just get a UPS API account and request up-to-date rates directly from UPS?
我最终将多个报价加在一起 - 因此,重量在 150 磅 - 300 磅之间的货件将被分成两个包裹,两个费率报价将加在一起形成总费率成本。
I ended up adding multiple quotes together - So a shipment that was between 150 lbs - 300 lbs would be split into two packages, and the two rate quotes would be added together to form the total rate cost.
如果您在美国,则应尝试使用此类来了解费率和价格追踪。
我确信您需要一个 UPS API 帐户。
If you are in the US you should try to use this classes for the rate and the tracking.
I am sure you will need a UPS API account.