Mthe beseti
Mthe beseti

Reputation: 619

Wrong shipping rate gets applied on the cart

I have an issue here when I add some products to the cart, I always get the wrong shipping cost. I thought before that the reason I got the wrong shipping price was because of the weight of a bonus product in cart, hence the price of a bonus product is 0. As far as I know Magento will make a sum of all products found in cart.

So to exclude the price that I wrote the following:

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

But still I get the weight of a bonus product included in cart totals, and the weird part is when I press the update button in cart I get a correct shipping price.

Upvotes: 1

Views: 106

Answers (1)

Oscprofessionals
Oscprofessionals

Reputation: 2174

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();
       }

You will have to code function isBonusItem() which will return a flag value.

Upvotes: 1

Related Questions