1

I'm trying to update a block in my checkout page via ajax. However, when I call the blocks toHtml() function in the controller the code does not continue to execute. Any suggestions? Below is the code in question.

cartController.php:

couponPostAction(){
    Mage::log('first step');
    ....
    Mage::log('second step');
    $this->loadLayout();
    Mage::log('third step');
    $totals=$this->getLayout()->getBlock('checkout.cart.totals')->toHtml();
    Mage::log('fourth step');
    $coupon_code=$this->getLayout()->getBlock('coupon.code')->toHtml();
    $response['totals']=$totals;
    $response['couponCode']=$coupon_code;
    ...
    $this->getResponse->setBody(Mage::helper('core')->jsonEncode($response));
}

There are also multiple entries of 'coupon.code' in the layout.xml (which I assume the controller somehow knows it's using.. I could be wrong though.).

checkout.xml layout:

    <block type="page/html" name="coupon.code" as="couponCode" template="checkout/cart/couponerror.phtml"/>
    ...
    ...
    <block type="page/html" name="coupon.code" as="couponCode" template="checkout/cart/couponerror.phtml"/>
    <block type="checkout/cart_totals" name="checkout.cart.totals" as="totals" template="checkout/cart/totals.phtml"/>

Contents of log:

    first step
    second step
    third step
2

What does ajax call show in Firebug? Anything in error logs?

Code looks good except:

    $response['couponCode']=$coupon_code;

Is missing apostrophe and you should remove return on last line.

    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));

Try that and see if it works. hope that helps

EDIT

Eh, sorry, I missed this somehow:

There are also multiple entries of 'coupon.code' in the layout.xml (which I assume the controller somehow knows it's using.. I could be wrong though.).

No, the controller is in fact using yourmodule_cart_couponpost handle. Please try with following corrections:

$this->loadLayout(false);
....
$totals=$this->getLayout()->createBlock('checkout/cart_totals')->setTemplate('checkout/cart/totals.phtml')->toHtml();
....
$coupon_code=$this->getLayout()->createBlock('checkout/cart_coupon')->setTemplate('checkout/cart/coupon.phtml')->toHtml();
  • I'm attempting to add a string to a log file before and after the execution of the tohtml() function , however, only the initial log attempt is generated (I assume code execution has stopped at this point). No errors are being logged in my exceptions or system log. I'll add this bit into the original formulation. – easymoden00b Jan 2 '15 at 19:25
  • And in regards to the ajax call, are you speaking of the data sent to the controller itself? – easymoden00b Jan 2 '15 at 19:31
  • Slight brain lag on my side, check the edited post and let me know if it works now. – Lord Skeletor Jan 2 '15 at 19:49
  • disregard that, i'll just move the javascript to a seperate file. You the man. – easymoden00b Jan 2 '15 at 20:21

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.