Question

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.

Was it helpful?

Solution

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();

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top