0

I'm trying to caltulate the total wight of products in cart and here is my code:

//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;
        }
    }
}

Now the problem is I have free/bonus products in the cart and this free product has weight. The above code just add the weight of the free product as well. I want to know is there a way I can exclude the weight of the free product in my cart. Or how can I filter the free/bonus products on magento using the Singleton.

1
  • Why not just check the price of your products (i guess price is 0?) before adding the weight: if ($itemWeight != null && $item->getPrice>0){ ... } – Anna Völkl Aug 10 '14 at 13:41
2

So as commented by Anna you could filter by price

if ($itemWeight != null && $item->getPrice() > 0) {
    $weight += $itemWeight;
}

But I would suggest that maybe this would not be the best idea. If the "free" product has a specific sku or attribute that marks it as "free" then I would use this as part of the check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.