Magento Extensions: Adding a Custom Block to Product View

magento-1.7PHP

I'm having an issue adding a custom PHP block to the product view in Magento. The block simply won't display on the product page.

This is part of my first Magento extension. Hoping you can help me.

My approach is based on the tutorial at the following link:
https://gist.github.com/grafikchaos/11148771

Note that I am not tied to the process in this tutorial – I am open to any suggestions / adjustments to this approach.

My files created/adjusted are as follows:

/app/etc/modules/Whirlwindecom_Personalizer.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- located at: app/etc/modules/Whirlwindecom_Personalizer.xml -->
<config>
    <modules>
        <Whirlwindecom_Personalizer>
            <active>true</active>
            <codePool>local</codePool>
        </Whirlwindecom_Personalizer>
    </modules>
</config>

/app/code/local/Whirlwindecom/Personalizer/etc/config.xml

Note this file contains additional nodes to support other parts of this extension that are working and are not related to this issue

<?xml version="1.0"?>
<config>
    <modules>
        <Whirlwindecom_Personalizer>
            <version>0.1.0</version>
        </Whirlwindecom_Personalizer>
    </modules>
    <global>
        <blocks>
            <whirlwindecom_personalizer>
                <class>Whirlwindecom_Personalizer_Block</class>
            </whirlwindecom_personalizer>
        </blocks>
        <models>
            <whirlwindecom_personalizer>
                <class>Whirlwindecom_Personalizer_Model</class>
            </whirlwindecom_personalizer>
        </models>
    </global>
    <frontend>
        <routers>
            <whirlwindecom_personalizer>
                <use>standard</use>
                <args>
                    <module>Whirlwindecom_Personalizer</module>
                    <frontName>personalizer</frontName>
                </args>
            </whirlwindecom_personalizer>
        </routers>
        <layout>
            <updates>
                <whirlwindecom_personalizer module="Whirlwindecom_Personalizer">
                    <file>whirlwindecom_personalizer.xml</file>
                </whirlwindecom_personalizer>
            </updates>
        </layout>
    </frontend>
    <adminhtml>
        <layout>
            <updates>
                <whirlwindecom_personalizer>
                    <file>whirlwindecom_personalizer.xml</file>
                </whirlwindecom_personalizer>
            </updates>
        </layout>
        <events>
            <catalog_product_save_after>
                <observers>
                    <Whirlwindecom_save_product_data>
                        <type>singleton</type>
                        <class>Whirlwindecom_Personalizer_Model_Observer</class>
                        <method>saveProductTabData</method>
                    </Whirlwindecom_save_product_data>
                </observers>
            </catalog_product_save_after>
        </events>
    </adminhtml>
</config>

/app/design/frontend/base/default/layout/whirlwindecom_personalizer.xml

<?xml version="1.0"?>

<layout version="0.1.0">
    <catalog_product_view>
        <reference name="whirlwind_personalizer">
            <block type="core/template" name="whirlwind_personalizer_block" template="whirlwindecom/personalizer/personalize.phtml" />
        </reference>
    </catalog_product_view>
</layout>

/app/design/frontend/base/default/template/catalog/product/view.phtml

Added the following:

<?php echo $this->getChildHtml('whirlwind_personalizer_block'); ?>

Before:

<div class="product-shop">

/app/design/frontend/base/default/template/whirlwindecom/personalizer/personalize.phtml

<?php
// let's output a message to verify this script is being included and displayed
echo "Personalizer Success!";
?>

One final note. In an attempt to debug my work and trace the cause of the problem, I temporarilly added the following code to the view.phtml script at the same place as my getChildHtml line:

var_dump(array_keys($this->getLayout()->getAllBlocks()));

It outputted the following, which verifies that Magento sees the block?:

array(100) {
    ...
    [95]=> string(28) "whirlwind_personalizer_block"
    ...
}

Best Answer

Change your code of file

/app/design/frontend/base/default/layout/whirlwindecom_personalizer.xml

<?xml version="1.0"?>

<layout version="0.1.0">
    <catalog_product_view>
        <reference name="content">
            <block type="core/template" name="whirlwind_personalizer_block" template="whirlwindecom/personalizer/personalize.phtml" />
        </reference>
    </catalog_product_view>
</layout>
Related Topic