在EasyPost中定义送货方式或费率

时间:2014-01-26 15:09:59

标签: php api session archive easypost

好的,我在一个文件中有以下代码。这只是EasyPost的部分代码,便于发货api

$created_rates = \EasyPost\Rate::create($shipment);

$availableRates = array();

foreach($created_rates as $rate){

    $fedexR = str_replace("FEDEX_", " ", $rate->service);

    $displayS = str_replace("_", " ", $fedexR);

    $displayP = $rate->rate;

    $availableRates[] = $displayS;

}

$_SESSION['displayRates'] = $availableRates;

这会将用户发送到选择送货方式的下一步,它们显示如下:

<?php foreach($_SESSION['displayRates'] as $rate): ?>
     <option value="<?php echo $rate; ?>"><?php echo $rate; ?></option>
<?php endforeach; ?>

这提交到以下页面:

$_SESSION['shipping_method'] = $_POST['shippingRate'];

$shipment = \EasyPost\Shipment::retrieve(array('id' => $_SESSION['shipment_id']));

$shipment->buy($shipment->rates[1]);

$_SESSION['shipment_label_url'] = $shipment->postage_label->label_url;

echo $_SESSION['shipment_label_url'];

如何使用EasyPost API发送选定的运费以告知其购买该运费方式的邮资?我明白了

$shipment->buy($shipment->rates[1]);

我试过这样做:

$shipment->buy($shipment->rates[$_SESSION['shipping_method']]);但导致费率根本没有发送。

顺便说一句,会话是像“GROUND”这样的实际单词而不是数字。我不确定如何实现这一目标,但如果我希望它能够完成,我必须这样做!

2 个答案:

答案 0 :(得分:2)

货件上有一个名为lowest_rate的过滤器。您可以像运营商名称和服务名称一样将args传递给它。类似的东西:

$shipment->buy($shipment->lowest_rate(array('Fedex'), array($_SESSION['shipping_method'])));

答案 1 :(得分:1)

首先,您需要更改输出<option value="<?php echo $rate; ?>"><?php echo $rate; ?></option>

的方式

它应该更像<option value="<?php echo $rate[id]; ?>"><?php echo $rate[rate].' '.$displayS; ?></option>

您需要此费率[id]才能购买这样的运费标签

$shipment->buy(array('rate'=>array('id'=>$_POST[rate_id])));

然后你可以这样做:

<img src="<?php echo $shipment->postage_label->label_url; ?>">