1

I need to call a custom block on success page

I have tried by layout as mention below

   <checkout_onepage_success translate="label">
        <reference name="content">
            <block type="checkout/onepage_success" name="checkout.success" template="checkout/success.phtml" >
                <block type="core/template" name="birthday" template="checkout/message/birthday.phtml"/>
            </block>
        </reference>
    </checkout_onepage_success>

In success page

echo $this->getChildHtml('birthday');

I am getting birthday block twice on the page.

Please let me know how we can call custom block on success page.

I don't want to use dynamic block i.e.

echo $this->getLayout()->createBlock('core/template')
->setTemplate('checkout/message/birthday.phtml')->toHtml();
2

Added an answer for you here.
Here it is again:

You are getting the birthday block twice because of this line from success.phtml:

 <?php echo $this->getChildHtml() ?>

This line renders all child blocks of the main block.
If you want it listed only once remove the line

echo $this->getChildHtml('birthday');

It will be redered by the getChildHtml method anyway.

7
  • can we remove echo $this->getChildHtml() Sep 8 '14 at 11:04
  • In theory you can, but if there are other modules that add something to the success page you may lose functionality. If you want your block in a specific location, take a look at this: magento.stackexchange.com/a/32695/146. It's for the header section but it applies to this case also.
    – Marius
    Sep 8 '14 at 11:07
  • can i use like this echo $this->getChildHtml('checkout.success') Sep 8 '14 at 11:09
  • You are already using it like that. the difference is the parameter name.
    – Marius
    Sep 8 '14 at 11:12
  • because by writing this my issue is resovled Sep 8 '14 at 11:13
2

U have manage this code in checkout.xml

   <checkout_onepage_success translate="label">
        <reference name="content">
            <block type="checkout/onepage_success" name="checkout.success" template="checkout/success.phtml" >
                <block type="core/template" name="birthday" template="checkout/message/birthday.phtml"/>
            </block>
        </reference>
    </checkout_onepage_success>

Describe in checkout.xml auto call your bithday.phtml file on success page.

So not use this function on success file.

echo $this->getChildHtml('birthday');

I have test your code on my site.

When u describe function on file so twice call a file.

1

Yes i got an anwser.

 <checkout_onepage_success translate="label">
    <reference name="content">
      <block type="checkout/onepage_success" name="checkout.success" template="checkout/success.phtml" />
    </reference>

    <reference name="checkout.success">
            <block type="core/template" name="birthday" as="birthday" template="checkout/message/birthday.phtml" />
            <block type="core/template" name="test" as="test" template="checkout/message/test.phtml" />
             <block type="core/template" name="test1" template="checkout/message/test1.phtml"/>
    </reference>
</checkout_onepage_success>

In success page

echo $this->getChildHtml('birthday');
echo $this->getChildHtml('test');
echo $this->getChildHtml('test1');

Hope this will help others.

3
  • As I suspected. this solution might work for you now, but in case you get later other extensions that add something to the success page you won't get those blocks printed out. What if there is an extension like yours that adds a block called some_block. You will never see that unless you add <?php echo $this->getChildHtml('some_block') ?> to the phtml file
    – Marius
    Sep 8 '14 at 11:22
  • but i thought this is the only solution for calling custom blocks Sep 8 '14 at 11:24
  • Not quite, as mentioned above $this->getChildHtml() will print out your module anyway as it captures all child blocks and renders them. Apologies for bumping this old thread but it's still quite relevant. Jun 29 '18 at 9:36

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.