0

I am trying to modify third-party code that adds a discount and then reloads the cart.
What I want to do is skip reloading the cart and updating the totals via JS.
To be able to do this, I need to pass the updated totals via JSON, and here is my problem:
I know the formatting of the totals is done by rendertotals() in Mage_Checkout_Block_Cart_Totals, but how can I access this code?

I can access the cart with Mage::helper('checkout/cart')->getCart(), and I know there's Mage::helper('checkout/cart')->getCart()->getQuote()->getTotals(), but this returns an array while I'd like to call the same formatting procedure that is called when the cart is displayed initially to be sure the update procedure is visually compatible.

I guess it's trivial, but I am not yet familiar with the calling syntax of Magentos helpers and singletons.

Thank you for reading!

1

Did you try:

Mage::app()->getLayout()->createBlock('checkout/cart_totals')->renderTotals()
  • Your suggestion echoes checkout/cart_totals, but replacing toHtml() with renderTotals() does the trick. Thanks! – Pharaoh Jan 20 '14 at 18:29
  • Fixed the answer. – Fabian Blechschmidt Jan 20 '14 at 20:10
1

If you are calling the code from your own controller, then the layout update of the cart controller is not loaded and you will not get the template file defined. So, to get the exact same html that is returned with the cart view, you can set the template in the following way:

 $this->getLayout()->createBlock('checkout/cart_totals')
                   ->setTemplate('checkout/cart/totals.phtml')->toHtml();

That is the reason, I think, that the previous answer not work at the first time.

There is a harder way, forcing the controller to load the layout updates (checkout.xml) of the checkout/cart module, and after, get the block output by his name. This way:

$layout = $this->getLayout();
$layout->getUpdate()
        ->addHandle('default')
        ->addHandle('checkout_cart_index')
        ->load();
$layout->generateXml()->generateBlocks();
$total_html = $layout->getBlock('checkout.cart.totals')->toHtml();

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.