当我将一些产品添加到购物车时,我遇到了一个问题,我总是得到错误的运输成本。我之前认为,我得到错误运费的原因是因为购物车中奖励产品的重量,因此奖励产品的价格是0。据我所知,Magento将对购物车中的所有产品进行汇总。
所以为了排除价格,我写了以下内容:
//If we have no weight, try to calculate this
$weight = 0;
if ($quote->getShippingAddress()->getWeight() == null ||
$quote->getShippingAddress()->getWeight() == 0 ||
$quote->getShippingAddress()->getWeight() == ''){
//this is where I do a check for bonus product. because a bonus product might have either aweight of 0 or 0 price
foreach ($quote->getAllItems() as $item){
$itemWeight = $item->getWeight();
if ($itemWeight != null && $item->getPrice() > 0){
$weight += $itemWeight;
}
}
}else{
$weight = $quote->getShippingAddress()->getWeight();
}
但我仍然得到了购物车总数中包含的奖励产品的重量,奇怪的是,当我按下购物车中的更新按钮时,我得到了正确的运费。
foreach ($quote->getAllItems() as $item){
if($item->isBonusItem(){
continue;
}
$itemWeight = $item->getWeight();
if ($itemWeight != null && $item->getPrice() > 0){
$weight += $itemWeight;
}
else{
$weight = $quote->getShippingAddress()->getWeight();
}
您必须对函数isBonusItem()进行编码,该函数将返回一个标志值。