1

I'm trying to update my header mini cart dynamically by creating a new block, I've tried several ways including:

$this->loadLayout();
$block = $this->getLayout()->createBlock(
    'Mage_Checkout_Block_Cart',
    //'checkout/cart', // OR 'Mage_Checkout_Block_Cart'
    'minicart_content',
    array('template' => 'ajax/checkout/cart/sidebar/default.phtml')
);

$this->getLayout()->getBlock('content')->append($block);
//Release layout stream... lol... sounds fancy
$this->renderLayout();

And also

$block = $this->getLayout()->createBlock('checkout/cart')->setTemplate('ajax/checkout/cart/sidebar/default.phtml')->toHtml();

They always come back empty, I can get it to bring back static content fine. Any ideas please?

3

In your module/controller/CartController.php action (or another) :

$data["cart_sidebar"] = $this->getLayout()
->createBlock('checkout/cart_sidebar')
->setTemplate('checkout/cart/minicart/items.phtml')
->addItemRender("default", "checkout/cart_item_renderer", "checkout/cart/minicart/default.phtml")
->addItemRender("simple", "checkout/cart_item_renderer", "checkout/cart/minicart/default.phtml")
->addItemRender("grouped", "checkout/cart_item_renderer_grouped", "checkout/cart/minicart/default.phtml")
 ->addItemRender("configurable", "checkout/cart_item_renderer_configurable", "checkout/cart/minicart/default.phtml")
 ->toHtml();

You can use json to serialize your data.

Then have just to put in your js :

jQuery(".block.block-cart").html(data.cart_sidebar);

If you want upgrade cart qty, serialize it too ;)

  • ;) to you too there partner. – easymoden00b Dec 17 '14 at 21:08
2

Try this in your controller as an action (which you'd then call via ajax)

public function getTopCartAction()
    {
        $this->loadLayout();
        $layout = Mage::getSingleton('core/layout');
        $block = $layout->getBlock('cart_sidebar');
        $html = $block->toHtml();
        $this->getResponse()->setHeader('Content-type',
            'application/json')->setBody($this->_success($html));
        return $this;
    }

This would then use whatever layout.xml directive may have altered 'cart_sidebar' block definition, from any 3rd party modules.

I use the following (which is in a template file):

<script>
    var request = new Ajax.Request(
            '<?php echo mage::getUrl("*/cart/getTopCartQty") ?>',
            {
                method: 'get',
                onSuccess: function(response) {
                    var json = response.responseText.evalJSON();
                    var cartElm = $$('.top-link-cart')[0];
                    if(cartElm){
                        var updatedTopCart = cartElm.innerHTML.replace(/(\d+)/g, 
                        json.content);
                        cartElm.innerHTML = updatedTopCart;
                        cartElm.title = updatedTopCart;
                    }
                }
            }
    );
</script>    

Hope that helps

1

You can get minicart HTML in a controller with:

public function yourAction() {
    $this->loadLayout();
    $minicart = $this->getLayout()->getBlock('minicart_content')->toHtml();
    echo $minicart;
    return;
}

Then you can replace minicart HTML using jQuery or similar:

jQuery('#header-cart').html(RESPONSE);
  • This code only update minicart content. But what about if I want to update mini cart qty as well which display beside cart? – Indian Aug 8 '17 at 7:18

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.