2

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 1.9.2.4

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:

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

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

The config file of module:

<?xml version="1.0"?>
<config>
  <modules>
    <Test_Recentproducts>
      <version>1.0</version>
    </Test_Recentproducts>
  </modules>
  <global>
    <blocks>
      <recentproducts>
        <class>Test_Recentproducts_Block</class>
      </recentproducts>
    </blocks>
    <models>
      <recentproducts>
        <class>Test_Recentproducts_Model</class>
      </recentproducts>
    </models>
  </global>
</config>

recentproducts.phtml

<?php
$products = $this­->getRecentProducts();
?>

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

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")
                ->getCollection()
                ->addAttributeToSelect('*')
                ->setOrder('entity_id', 'DESC')
                ->setPageSize(5);

    return $products;
  }
}

Any help would be highly appreciated.

Update:

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 Mar 30 '16 at 14:28
  • While calling the block function in template file. – danish Mar 30 '16 at 14:32
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 Mar 30 '16 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 Mar 30 '16 at 14:44
  • You are a star. I lost my whole day debugging . Thank you. – danish Mar 30 '16 at 14:49
0

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 Mar 30 '16 at 14:12
  • paste the RecentProducts.php code on your question – Asish Hira Mar 30 '16 at 14:13
  • pasted above... – danish Mar 30 '16 at 14:16
  • is compilation mode is enabled?? – Asish Hira Mar 30 '16 at 14:19
  • No its disabled – danish Mar 30 '16 at 14:20

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.