0

I have a code the gets the Product Name and Weight of the ordered items in the onePage Checkout. My code works if items selected are simple product but if an item ordered is a Configurable Product it compute it twice before proceeding to the next item or next stage of the OnePage Checkout. It's just a simple formula that will calculate the total weight of the items ordered and if it's <=5kg AED20 and adds AED2 for succeeding kg.

                        // retrieve quote items collection
                    $itemsCollection = Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection();

                    // get array of all items what can be display directly
                    $itemsVisible = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();

                    // retrieve quote items array
                    $items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
                     $weight = 0;
                        foreach($items as $item) {
                        echo '<tr><td>'.$item->getName().'</td>';
                        echo '<td>'.(float)number_format($item->getWeight(), 2, '.', '').'kg</td>'; 
                        echo '<td>'.$item->getQty().'</td>';                        
                        echo '<td>'.(float)number_format($item->getWeight(), 2, '.', '') * $item->getQty().'kg</td></tr>';  
                        // echo 'ID: '.$item->getProductId().'<br />';
                        // echo 'Sku: '.$item->getSku().'<br />';
                        // echo 'Price: '.$item->getPrice().'<br />';
                        $weight += ($item->getWeight() * $item->getQty()) ;
                        }
                    $cod_fee = (($weight - 5) * 2) + 20;
                    $round_cod_fee = ceil($cod_fee / 5) * 5;

                    //TOTAL Weight of the Order 
                    if ($round_cod_fee <= 20) {
                        echo '<br>Total Weight of your order is : ' .$weight.'kg = AED20 Total for the Shipping and Handling (COD)';
                        }
                    else {
                        echo '<br>Total Weight of your order is : ' .$weight.'kg = AED' . $round_cod_fee . ' Total for the Shipping and Handling (COD)';
                    }

Looking forward to hear from you. Please I need the ninjas/experts help. Thank you!

1 Answer 1

2

Note getAllVisibleItems (1) vs getAllItems (2). What you want to use is (1) because (2) retrieves two records for each configurable product ordered. Configurable products have two records (parent and child) in the database.

Basically, all parent-child products, when bought, have two records in the sales table: one for the parent product and one for the child product that has inventory. Use getAllVisibleItems for getting all of the products as you see them in your cart/order.

1
  • So your saying that it will do the trick? Or I have to do more changes? I'll try and keep you posted.
    – KiD Cajes
    Commented Jul 22, 2015 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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