好的,我得到它的工作。这是以编程方式从 magento 获取运输报价的最终代码。
该函数将返回特定产品、数量、国家/地区、邮政编码的所有可用运费。该代码不包括免费送货,这可以通过删除来撤消if($_rate->getPrice() > 0) { ...
<?php
require_once("Mage.php");
umask(0);
ini_set('display_errors',true); Mage::setIsDeveloperMode(true);
Mage::app();
function getShippingEstimate($productId,$productQty,$countryId,$postcode ) {
$quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore('default')->getId());
$_product = Mage::getModel('catalog/product')->load($productId);
$_product->getStockItem()->setUseConfigManageStock(false);
$_product->getStockItem()->setManageStock(false);
$quote->addProduct($_product, $productQty);
$quote->getShippingAddress()->setCountryId($countryId)->setPostcode($postcode);
$quote->getShippingAddress()->collectTotals();
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();
$_rates = $quote->getShippingAddress()->getShippingRatesCollection();
$shippingRates = array();
foreach ($_rates as $_rate):
if($_rate->getPrice() > 0) {
$shippingRates[] = array("Title" => $_rate->getMethodTitle(), "Price" => $_rate->getPrice());
}
endforeach;
return $shippingRates;
}
echo "<pre>";
// product id, quantity, country, postcode
print_r(getShippingEstimate(1098,100,"GB","SY21 7NQ"));
echo "</pre>";
这可以放入这样的下拉列表中:
$results = getShippingEstimate(1098,100000,"GB","SY21 7NQ");
$count = -1;
echo "<select>";
foreach ($results as $result):
$count++;
?>
<option value="<?=$count?>"><?=$result["Title"]." - £".$result["Price"]?></option>
<?php
endforeach;
echo "</select>"