Magento 1.8 – How to Add Minicart to Custom Block

magento-1.8mini-carttopmenu

My intention is to create something close to rwd from 1.9 but I'm on 1.8.1 and it's difficult to update as there have been so many changes on this magento.
I've successfully added something from the minicart copying some text from mini_cart_top.phtml.

<?php

$prova = new Mage_Checkout_Block_Cart_Sidebar();
$_items = $prova->getRecentItems() ?>
    <?php if(count($_items)): ?>
            <div id="header-items">

                <div class="block-subtitle"><span><?php echo $prova->__('Producto(s) a&ntilde;adido(s)') ?></span> <div style="clear: both;"></div></div>

                <ol id="cart-sidebar" class="mini-products-list">
                <?php foreach($_items as $_item): ?>
                    <?php echo $prova->getItemHtml($_item) ?>
                <?php endforeach; ?>
                </ol>
                <div class="summary">
                    <?php echo $prova->__('Total + iva:') ?> 
                    <?php if ($prova->canApplyMsrp()): ?>
                        <span class="map-cart-sidebar-total"><?php echo $prova->__('ORDER TOTAL WILL BE DISPLAYED BEFORE YOU SUBMIT THE ORDER'); ?></span>
                    <?php else: ?>
                        <?php echo Mage::helper('checkout')->formatPrice($prova->getSubtotal()) ?>
                        <?php if ($_subtotalInclTax = $prova->getSubtotalInclTax()): ?>
                            <?php echo Mage::helper('checkout')->formatPrice($_subtotalInclTax) ?> <?php echo Mage::helper('tax')->getIncExcText(true) ?>
                        <?php endif; ?>
                    <?php endif; ?> 
                </div>                                  
                <button type="button" title="<?php echo $prova->__('Ver presupuesto') ?>" class="button btn-view" onclick="window.location='<?php echo $prova->getUrl('checkout/cart')?>';"><span><span><?php echo $prova->__('Ver presupuesto') ?></span></span></button>                                
                <button type="button" title="<?php echo $prova->__('Solicitar presupuesto') ?>" class="button btn-checkout" onclick="window.location='<?php echo $prova->getUrl('checkout/onepage')?>';"><span><span><?php echo $prova->__('Solicitar presupuesto') ?></span></span></button>                                
            </div>
        <script type="text/javascript">decorateList('cart-sidebar', 'none-recursive')</script>

    <?php else: ?>
        <div id="header-items" class="empty">
            <p class="empty"><?php echo $prova->__('No hay ning&uacute;n art&iacute;culo seleccionado.') ?></p>
        </div>
    <?php endif ?>

But <?php echo $prova->getItemHtml($_item) ?> gives me error:

Fatal error: Call to a member function createBlock() on a non-object in C:\wamp\www\ravanetto\app\code\core\Mage\Checkout\Block\Cart\Abstract.php on line 107

Why am I missing?

Edit:
That's how I call it through xml.

<default>
    <reference name="header">
        <block type="core/template" name="rwdtopCart" as="rwdtopCart" template="rwdmenu/rwdtopcart.phtml"/>
    <reference/>
<default/>

And I add this somewhere in header.phtml.

<div id="rwd-header-cart" class="skip-content">
    <?php echo $this->getChildHtml('rwdtopCart') ?>
</div>

Best Answer

You're referencing to the core/template block which doesn't have the functions you're using in your template. Because it's based on the minicart I think you've to use the checkout/cart_minicart block. Try this:

<default>
    <reference name="header">
        <block type="checkout/cart_minicart" name="rwdtopSearch" as="rwdtopSearch" template="rwdmenu/rwdtopsearch.phtml"/>
    <reference/>
<default/>

If that's not working try the checkout/cart_sidebar block. Have a look at the default XML for the default used blocks, for example in app/design/frontend/rwd/default/layout/checkout.xml. Maybe you need the define the item renders (addItemRender) to.

After that you can change

$prova = new Mage_Checkout_Block_Cart_Sidebar();
$_items = $prova->getRecentItems();

To what it originally was:

$_items = $this->getRecentItems();
Related Topic