我正在努力计算购物车中产品的总量,这是我的代码:
//If we have no weight, try to calculate this
$weight = 0;
if ($quote->getShippingAddress()->getWeight() == null ||
$quote->getShippingAddress()->getWeight() == 0 ||
$quote->getShippingAddress()->getWeight() == ''){
foreach ($quote->getAllItems() as $item){
$itemWeight = $item->getWeight();
if ($itemWeight != null){
$weight += $itemWeight;
}
现在问题是我在购物车中有免费/奖励产品,这个免费产品有重量。上面的代码也增加了免费产品的重量。我想知道有没有办法可以排除购物车中免费产品的重量。或者我如何使用Singleton过滤magento上的免费/奖励产品。
答案 0 :(得分:2)
根据我的说法,你必须获取免费物品的sku并在foreach($ quote-> getAllItems()作为$ item)中再创一个条件,如果$ item-> getsku == in_array(免费)项目sku)然后继续;否则它会增加你的代码的重量。
答案 1 :(得分:2)
foreach ($quote->getAllItems() as $item){
$itemWeight = $item->getWeight();
if ($itemWeight != null && $item->getPrice() != 0){ //if the weight is not null and the price is not 0 (no free product) add the weight to the total.
$weight += $itemWeight;
}
....
}