Hi I am a newbie to magento. I'm trying to call a custom module block using getChildHtml() function which unfortunately is showing a fatal error: Call to undefined function getRecentProducts()

I am using magento

I have set up a child theme (childtheme) under magento default rwd theme.

I have copied page.xml from the default theme of rwd package.

I have also created a new layout file (home.phtml) and I am trying to get the block in this file.

I created a new block in active themes page.xml under <default> tag:

<block type="core/text_list" name="newreference" as="newreference"/>

Then in active themes local.xml:

    <reference name="newreference">
        <block type="recentproducts/recentproducts" name="recentproducts_recentproducts" template="recentproducts/recentproducts.phtml"></block>

And in my home.phtml file calling: getChildHtml('newreference');

The config file of module:

<?xml version="1.0"?>


$products = $this­->getRecentProducts();

<div id="product_list">
  <h1>Recent Products</h1>
  <?php if (is_array($products) && count($products)) { ?>
    <?php foreach($products as $product) { ?>
        <a href="<?php echo $product['url'] ?>"><?php echo $product['name'] ?></a>
    <?php } ?>
  <?php } ?>

Recentproducts.php (block)

class Test_Recentproducts_Block_Recentproducts extends Mage_Core_Block_Template {
  public function getRecentProducts() {
    // call model to fetch data
    $arr_products = array();
    $products = Mage::getModel("recentproducts/recentproducts")->getRecentProducts();

    foreach ($products as $product) {
      $arr_products[] = array(
        'id' => $product->getId(),
        'name' => $product->getName(),
        'url' => $product->getProductUrl()

    return $arr_products;

Recentproducts.php (model)

class Test_Recentproducts_Model_Recentproducts extends Mage_Core_Model_Abstract {
  public function getRecentProducts() {
    $products = Mage::getModel("catalog/product")
                ->setOrder('entity_id', 'DESC')

    return $products;

Any help would be highly appreciated.


I tried to get_class($this) and it is perfectly returning the right class which is Test_Recentproducts_Block_Recentproducts.

Its also returning getRecentProducts() as a class method.

  • Where is the error occuring? Template file? Block class?
    – fmrng
    Commented Mar 30, 2016 at 14:28
  • While calling the block function in template file.
    – danish
    Commented Mar 30, 2016 at 14:32

2 Answers 2


EDIT: I tested your code on a local installation, I assume you copied it from somewhere. The problem there is that - is not really a minus but some different special character (I haven't investigated further, sorry), and I was getting the same error you get. I wrote $this-> myself and it did work.

Probably $this­>getRecentProducts(); should be $this­->getRecentProducts();

  • Sorry that's a typo here. I'll edit it
    – danish
    Commented Mar 30, 2016 at 14:25
  • Well, it was an answer somehow, it wasn't a critique. The problem is just a typo, nothing more. I updated my answer
    – fmrng
    Commented Mar 30, 2016 at 14:44
  • You are a star. I lost my whole day debugging . Thank you.
    – danish
    Commented Mar 30, 2016 at 14:49

Make sure that you created a file in recentproducts/block/recentproducts.php and this recentproducts.php file contains getRecentProducts() function.

  • 1
    Yes the files and functions are all present.
    – danish
    Commented Mar 30, 2016 at 14:12
  • paste the RecentProducts.php code on your question
    – Asish Hira
    Commented Mar 30, 2016 at 14:13
  • pasted above...
    – danish
    Commented Mar 30, 2016 at 14:16
  • is compilation mode is enabled??
    – Asish Hira
    Commented Mar 30, 2016 at 14:19
  • No its disabled
    – danish
    Commented Mar 30, 2016 at 14:20

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.