1

I have created my custom module. It works fine. But now i want to show two/three simple product in my home page using my custom module. can I use helper or block? which will more efficient or easy to use? If I fetch the product from my custom module block or helper page then i will call my custom helper or block in home page.Is it possible or right way to show? but i have to use my custom module. I have followed this link url but it's not working for me, it shows 404 page. If I added controller class like

class Monojit_Custom_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
      //$helper  = Mage::helper('custom');
      //print_r($helper);
        echo "here is my module";
    }
}  

then it works fine. Can anyone please tell me how can I fixed this issue. Till now I didn't change any settings for admin->design template. I also tried to create helper (url) but unfortunately it also not worked for me.

Any help will be greatly appreciated..

2

If there is a common functionality/method which you might going to use for other modules as well, better you add those methods inside a helper class.
Now for this instance, you can use either block or helper to write method in order to retrieve a set of simple products. But my suggestion is use the block to retrieve data you want and call the block (block method) in home page.

I'll elaborate the answer here itself.
Suppose we have a module name "Home" inside a namespace as "Test".
I hope you know how to create module activation file, so I'll eliminate it here.
So the config.xml is like this:

app\code\local\Test\Home\etc\config.xml

  <?xml version="1.0"?> <config>
         <modules>
             <Test_Home>
                 <version>0.1.0</version>
             </Test_Home>
         </modules>

         <frontend>
             <routers>
                 <home>
                     <use>standard</use>
                     <args>
                         <module>Test_Home</module>
                         <frontName>home</frontName>
                     </args>
                 </home>
             </routers>
             <layout>
                 <updates>
                     <home>
                         <file>home.xml</file>
                     </home>
                 </updates>
             </layout>
         </frontend>
         <global>  
             <blocks>
                 <home>
                     <class>Test_Home_Block</class>
                 </home>
             </blocks>
          <models>
                 <home>
                     <class>Test_Home_Model</class>
                 </home>
             </models>    
         </global>  
         <admin>
             <routers>
                 <home>
                     <use>admin</use>
                     <args>
                         <module>Test_Home</module>
                         <frontName>home</frontName>
                     </args>
                 </home>
             </routers>
         </admin> 
      </config>

And the block file is like below:

app\code\local\Test\Home\Block\Featuredproducts.php

<?php

class Test_Home_Block_Featuredproducts extends Mage_Core_Block_Template{

    public function _construct() {     
        parent::_construct(); 
        $this->setTemplate('test/featured_products.phtml');     
    }
        public function getFeaturedProducts()
        {
            $collection = Mage::getModel('catalog/product')->getCollection()
                                        ->addAttributeToSelect("*")
                                        ->addFieldToFilter('status','1')
                                        ->addFieldToFilter('visibility',array('in' => array(2,4))); // 2 = Search, 4 = Catalog, Search              
            return $collection;
        }
    }

Inside template file name as "featured_products.phtml"

app\design\frontend\default\home\template\test\featured_products.phtml

Now we can call getFeaturedProducts method which is written on the block.

$product_collection = $this->getFeaturedProducts();

Hope this answer will help you.

  • lot of thanks @oshan. do you check your code by own? i have used your code by creating new module(local/Test/Home), module is working fine but block is not showing in footer.i have used $product_collection = $this->getFeaturedProducts(); in footer it;s showing nothing. i think there is something problem for featured_products.phtml page location.can you plz recheck the folder location for featured_products.phtml.Do i need any change in admin/settings for theme or template.Till now i didn't change anything. – monojit banik Nov 5 '13 at 18:08
  • Gone through <link>blog.iyngaran.info/create-custom-module-helloworld-in-magento </link> and followed the tutorial but when i used $this->getLayout()->createBlock('iyngaran/helloworld')->setTemplate('helloworld/helloworld.phtml')->toHtml(); in footer.phtml(frontend/base/default/template/html/) it shows error : Fatal error: Call to a member function setTemplate() on a non-object in C:\xampp\htdocs\magento\app\design\frontend\base\default\template\page\html\footer.phtml on line 28 – monojit banik Nov 5 '13 at 18:15
  • in my magento admin (system->configuation->design) Current Package Name shows : default & Templates,skin,layout,default all the fields are null in themes tab.do i need to create new theme or only just template, which one i totally confused.app\design\frontend\default\home\template\test\featured_products.phtml means i need to create new theme.right?can you please tell me what i will do now.If i create new theme (home), do i need to create layout,etc folder with template folder? I mentioned by setting of magento admin design. – monojit banik Nov 5 '13 at 18:30
  • and @oshan I have just installed a new fresh magento 1.7 and i didn't change anything (theme,settings etc).so what's wrong,please help me.thanks – monojit banik Nov 5 '13 at 18:35
0

If there is a common functionality/method which you might going to use for other modules as well, better you add those methods inside a helper class.
Now for this instance, you can use either block or helper to write method in order to retrieve a set of simple products. But my suggestion is use the block to retrieve data you want and call the block (block method) in home page.

  • thanks but can you please tell me how can i achieve this.i have gone through blog.iyngaran.info/create-custom-module-helloworld-in-magento but it's not working for me, so tell what to do next or give some code or url so that i will able to fetch data in home page from my custom block. – monojit banik Nov 4 '13 at 11:23
  • Check the above answer again. I've re-modified it. – Oshan Nov 5 '13 at 4:55

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.