2

I want my custom block to be dependent on whether my module is enabled or not and I want do not want it to be dependent on a theme so using the following, for example, is out of the question: /catalog/product/view.phtml - using getChild().

I am attempting to add a block to product.info. I can update the product page without modifying any default template files if I use but, of course, if I use....

 <reference name="product.info">

I cannot show it unless I add a getChild() method to /catalog/product/view.phtml.

Module config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Importer_Nutrition>
            <version>0.1.5</version>
        </Importer_Nutrition>
    </modules>
    <global>
        <models>
            <nutrition>
                <class>Importer_Nutrition_Model</class> 
            </nutrition> 
        </models>
    </global>
    <frontend>
        <routers>
            <nutrition>
                <use>standard</use>
                <args>
                    <module>Importer_Nutrition</module>
                    <frontName>nutrition</frontName>
                </args>
            </nutrition>
        </routers>  
        <!-- Establish Layout Rules -->
        <layout>
            <updates>
                <nutrition>
                    <file>Importer/Nutrition/product_view.xml</file>
                </nutrition>
            </updates>
        </layout>
    </frontend>
    <crontab> 
        <jobs>
            <Importer_Fetch_Nutrition_Info> 
                <schedule>
                    <cron_expr>0 2 1,5 * *</cron_expr>
                </schedule> 
                <!-- Stocks are being added to database every 30 minutes at H:01 and H:31
                it can take up to 5 minutes to finish, so we are waiting ten minutes after each import -->
                <run>
                    <model>nutrition/Fetchnutritioninfo::run</model>
                </run>
            </Importer_Fetch_Nutrition_Info>
        </jobs>
    </crontab>

</config>

[theme]/layout/Importer/Nutrition/product_view.xml

 <layout version="0.1.0">
<catalog_product_view translate="label"> 
    <reference name="product.info"> 
        <block type="catalog/product_view_description" name="product.nutritionLabel" after="product.description" template="Importer/Nutrition/catalog/product/view/nutritionLabel.phtml" />
    </reference>
</catalog_product_view>

Can I use an observer to accomplish this?

2 Answers 2

1

iUseMagentoNow, You cannot add block using Observer without using getChildHtml('product.nutritionLabel') at view.phtml.

Because of at view.phtml file.... view.phtml file's all child block is not render using getChildHtml();

According magento a block all child block are render using getChildHtml() that if we do not send any param to getChildHtml() function the magento is render all block automatically.

If send parameter then it render only that particular block.

Example: Magento default render the media block using below code:

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

see at catalog xml is define media block name

4
  • So, from a module point of view how do I add this to the template without overriding any extensions that may have already modified view.phtml? Commented Jan 30, 2015 at 14:47
  • from my point of view ,it is not possible at view.phtml
    – Amit Bera
    Commented Jan 30, 2015 at 14:49
  • put this code <block type="catalog/product_view_description" name="product.nutritionLabel" after="product.description" template="Importer/Nutrition/catalog/product/view/nutritionLabel.phtml" /> at catalog.xml at catalog_product_view handler and check
    – Amit Bera
    Commented Jan 30, 2015 at 14:55
  • Sorry, it came out to a typo after I pasted the suggestion above - hence the comment delete. For educational purposes, since this must be embedded in view.phtml - where should I apply the logic that dictates whether or not the block is added based on the module being enabled or disabled? Commented Jan 30, 2015 at 14:57
1

There might be an easier way depending on what you want. Try modifying your layout update file like this:

<catalog_product_view>
    <reference name="product.info">
        <block type="catalog/product_view_description" name="product.nutritionLabel" after="product.description" template="importer/nutrition/catalog/product/view/nutritionLabel.phtml">
            <action method="setTitle" translate="value"><value>Nutrition Label</value></action>
            <action method="addToParentGroup"><group>detailed_info</group></action>
        </block>
    </reference>
</catalog_product_view>

Would something like this work for you?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.