Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6714437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:29:46+00:00 2026-05-26T08:29:46+00:00

i am trying to Create a Custom module ( A Copy Paste From Definitive

  • 0

i am trying to Create a Custom module ( A Copy Paste From Definitive Guide ) to Show random Products on the Home Page. I Started With Hello world It was Fine. But when I added More Code to Show Products On Home Page It Is Giving Me The Following Error.

Fatal error: Call to a member function getName() on a non-object in /opt/lampp/htdocs/magento/app/code/local/Magentotutorial/Definitivehello/Block/Randomproducts.php on line 12

here goes my code for /app/code/local/Magentotutorial/Definitivehello/Block/Randomproducts.php

<?php
class Magentotutorial_Definitivehello_Block_Randomproducts 
    extends Mage_Core_Block_Template
{
    protected function _toHtml()
    {
        $randProdModel = Mage::getModel('Magentotutorial_Definitivehello/Randomproducts');
        $randProducts = $randProdModel->getRandomProducts();
        $html = "<ul>";
        foreach ($randProducts as $product) {
            $name = $product->getName();
            $price = number_format($product->getPrice(), 2);
            $imageLink = $this->helper('catalog/image')->init($product, 'thumbnail')->resize(100,100);
            $productLink = $this->helper('catalog/product')->getProductUrl($product);
            $html .= "<p><a href='$productLink'><img src='$imageLink' alt='$name'/></a><br/> $name <br/> $price </p>";
        }
        $html .= "<ul>";
        return $html;
    }
}

Can anyone Tell Me Whats Wrong. Thanks

Here Goes My Code For GetRandomProducts() method

<?php
class Magentotutorial_Definitivehello_Model_Randomproducts 
    extends Mage_Core_Model_Abstract
{
    public function getRandomProducts($maxCount = 3)
    {
        $randProducts = array();
        $allProducts = array();
        $productCollection = Mage::getModel('catalog/product')
            ->getCollection()
            ->addAttributeToSelect('*')
            ->getItems();

        foreach ($productCollection as $id => $data) {
            //print_r($data); exit;
            $allProducts[] = $data;
        }

        $productIds = array_keys($allProducts);
        $totalProductIds = count($productIds);
        for ($i=0; $i<$maxCount; $i++) {
            $randIndex = rand(0,$totalProductIds);
            $randProductId = $productIds[$randIndex];
            $randProducts[] = $allProducts[$randProductId];
        }
        return $randProducts;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:29:46+00:00Added an answer on May 26, 2026 at 8:29 am

    Looks like getRandomProducts of Magentotutorial_Definitivehello/Randomproducts model does not return a product collection.

    UPD:

    Here is a simplier implementation of the getRandomProduct method:

    <?php
    public function getRandomProducts($maxCount = 3)
    {
        $productCollection = Mage::getModel('catalog/product')
            ->getCollection()
            ->addAttributeToSelect('*');
    
        $productCollection->getSelect()->order('RAND()')->limit($maxCount);
    
        return $productCollection;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a custom accordion for my page to that display
HI, I am trying to create a router for my custom module. I would
Trying to create a custom component that gets it's layout from an XML file
I am trying to create a new custom module in magento but the layout
I'm writing a custom module and I am trying to create an array of
I am trying to create a custom module in magento admin. I have reached
I'm trying to programmatically create the custom horizontal menu for my custom module, but
I'm trying to create a custom 404 page in my admin when something goes
I'm trying to create a custom module using Magento's EAV structure and have been
Im trying to create a custom version of the RequiredAttribute to replace the built

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.