1

Inside our list view I want to load an external php file. The file is displayed, but when loading the php code, the page break down.

When I place the php code inside the catalog/product/list.php it does work. Loading it inside the external file, it breaks down.

I guess I am missing some helpers. How can I fix this problem?

Line inside list.phtml:

   <ul class="productbox-beknoptespecs">
   <?php echo $this->getChildHtml('beknopte_specificaties_new'); ?>
   </ul>

Local.xml file:

<catalog_category_layered>
<reference name="product_list">

        <block type="core/template" name="beknopte_specificaties_new" as="beknopte_specificaties_new" template="catalog/product/list/beknoptespecs_new.phtml"/>
</reference>
</catalog_category_layered>

External php file:

<?php
    $_helper = $this->helper('catalog/output');
    $_product = $this->getProduct();
    $theme = $this->helper('ultimo');
?>

<!-- LAPTOPS -->

<?php $yourCatIds = array(27,233,234,235);
$productCats = $_product->getAvailableInCategories();

if (count(array_intersect($yourCatIds,$productCats))) {

    if($_product->getData('i_display_diagonal_944_1')) :
    echo '<li class="short-specifications"><span class="attributename">' . $_product->getResource()->getAttribute('i_display_diagonal_944_1')->getFrontendLabel() . '</span>';
    echo '<span class="attributetext">' . $_product->getAttributeText('i_display_diagonal_944_1') . '</span></li>';
    endif;

}
?>
  • what do you mean by an "external php file"? How do you include that in the template? what does that file contain? Why do you need an external file? – Marius May 7 '15 at 13:24
  • @Marius I edit my question – JGeer May 7 '15 at 13:27
  • What error message do you have when this page loads? How exactly do you load 'external' php file? – DmitryR May 7 '15 at 13:41
  • @DmitryR Not, the page only loads the first product and nothing below. So only the first product is loaded. – JGeer May 7 '15 at 13:42
  • Is your 'external php file' located at catalog/product/list/beknoptespecs_new.phtml? If yes - then why not to call it a 'custom template file'? Or I've misunderstood your description somehow?.. – DmitryR May 7 '15 at 13:47
2

Your block associated with the template is of type core/template (Mage_Core_Block_Template).
This block does not contain a getProduct method so it magically calls getData('product') that returns null.

So your _product variable from this call $_product = $this->getProduct(); is set to null.

Later you call $_product->getAvailableInCategories(). Since $_product is null this causes an error and your content is not displayed.

Try changing this code from the list tempalte

<ul class="productbox-beknoptespecs">
<?php echo $this->getChildHtml('beknopte_specificaties_new'); ?>
</ul>

To this one:

<ul class="productbox-beknoptespecs"> 
<?php $this->getChild('beknopte_specificaties_new')->setProduct($_product);?>
<?php echo $this->getChildHtml('beknopte_specificaties_new', false); ?>
</ul>

But this will work if your custom code is inside the foreach loop that goes through the products collection.

Also use <?php echo $this->getChildHtml('beknopte_specificaties_new', false); ?> (like I did above) because getChildHtml without the second parameter false returns a cached copy of the child block html. So when you call it the first time the html will be generated, the second time and from then on it will return the HTML generated the first time. You probably don't want that since you are trying to show the block for different products. The second parameter false tells Magento to rebuild the HTML again and not use the cache.

[EDIT]
I forgot that the same template is used for the search (rookie mistake).

You need to make your code look like this to avoid displaying it in the search results:

<?php if ($this->getChild('beknopte_specificaties_new')) : ?>
    <ul class="productbox-beknoptespecs"> 
    <?php $this->getChild('beknopte_specificaties_new')->setProduct($_product);?>
    <?php echo $this->getChildHtml('beknopte_specificaties_new', false); ?>
    </ul>
<?php endif;?>

[EDIT to EDIT]
you need to declare your block in the search results page also.
So add this to the layout xml file of your module.

<catalogsearch_result_index> <!-- for quick search results -->
    <reference name="search_result_list">
        <block type="core/template" name="beknopte_specificaties_new" as="beknopte_specificaties_new" template="catalog/product/list/beknoptespecs_new.phtml"/>
    </reference>
</catalogsearch_result_index>
<catalogsearch_advanced_result> <!-- for advanced search results -->
    <reference name="search_result_list">
        <block type="core/template" name="beknopte_specificaties_new" as="beknopte_specificaties_new" template="catalog/product/list/beknoptespecs_new.phtml"/>
    </reference>
</catalogsearch_advanced_result>
  • Marius, this code provide a problem on the search result page. On the catalog view page everything works fine, but the page break down on the search result page. Only one product is displayed and after that the page breaks. How can I solve this? I do want to load this ChildHtml also on the search result page. – JGeer May 8 '15 at 8:07
  • @Jelle See my update. – Marius May 8 '15 at 8:11
  • Thanks, but that does not display the code. I want to display the code also on the search result page. But that currently broke down. Do I need to make some modifications inside the XML file? – JGeer May 8 '15 at 8:14
  • @Jelle. Sorry I understood you don't want the block on the search results. Apparently I cannot read today. See my second edit. – Marius May 8 '15 at 8:17

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.