Magento – How to display related products on the cart page (checkout)

cartce-1.7.0.2checkoutproduct

I have products added to the cart who have related products.
My question: if it's possible to call and display all related products on the end of cart page (not in the cart).
thanks

Best Answer

There is no built in way of doing this because the block that renders the related products depends on the 'current product'.
See Mage_Catalog_Block_Product_List_Related::_prepareData().
What I recommend is to create your own block. Here is what you need.
You can get the related products for a product like this

$related = $product->getRelatedProductCollection()
       ->addMinimalPrice()
       ->addFinalPrice()
       ->addTaxPercents()
       ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
       ->addUrlRewrite()
       ->addAttributeToSelect('required_options')
       ->setPositionOrder()
       ->addStoreFilter();
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($related);

You can get all the products in the cart like this:

$inCartItems = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
$inCartProduct = array();
foreach ($inCartItems as $item){
    $inCartProducts[] = $item->getProduct();
}

You can add a block to the cart page by adding this in a layout file

<checkout_cart_index>
    <reference>
        <block type="your/block_type" name="related" as="related" template="path/to/template.phtml" />
    </reference>
</checkout_cart_index>

Put all these together and you should get your result

Related Topic