Use the same .phtml template to show different collection of productsModman - dealing with template files...

What is the oldest European royal house?

The (Easy) Road to Code

PTiJ: How should animals pray?

PTIJ: Aliyot for the deceased

Are there other characters in the Star Wars universe who had damaged bodies and needed to wear an outfit like Darth Vader?

Should we avoid writing fiction about historical events without extensive research?

Why can't we use freedom of speech and expression to incite people to rebel against government in India?

What does "rhumatis" mean?

Does the in-code argument passing conventions used on PDP-11's have a name?

How spaceships determine each other's mass in space?

Should I use HTTPS on a domain that will only be used for redirection?

What is Tony Stark injecting into himself in Iron Man 3?

Was it really inappropriate to write a pull request for the company I interviewed with?

The Key to the Door

Dukha vs legitimate need

Can a space-faring robot still function over a billion years?

Can a Mexican citizen living in US under DACA drive to Canada?

When to use the term transposed instead of modulation?

Do natural melee weapons (from racial traits) trigger Improved Divine Smite?

How do we objectively assess if a dialogue sounds unnatural or cringy?

What is better: yes / no radio, or simple checkbox?

What's the best tool for cutting holes into duct work?

Is divide-by-zero a security vulnerability?

“I had a flat in the centre of town, but I didn’t like living there, so …”



Use the same .phtml template to show different collection of products


Modman - dealing with template files spread across multiple moduleschange Wishlist price phtml templateBasic template question: Combine several products on one page?Bundle Products - how to use 2 different renderers for bundled product options? (to show in tab)catalog_product_collection_load_after - identify the “right” collectionChange list.phtml to show products of multiple categories in same blockRelated products as a dropdownhow can i use the magic methods correctly?Where exactly to place the code to retrieve products by categoryMagento2 : Iterate over blocks and show them as products













2















I have my own custom .phtml file that I would like to use for different collections of products. For example, I would like to use it to display the first n elements of category as well as the already seen products or related products.
In addition I need to able to decide for each block to show/hide the price of the products.



Is it possible to do it with the core modules of Magento or should I extend the existing ones and write a new module?










share|improve this question























  • Can you expand on 'I would like to use for different collections of products'. Does your template contains just product listings like the default list.phtml template or more page content? Are you showing more than one collection of products on the page or just one which has been modified?

    – Jonathan Hussey
    Nov 26 '14 at 12:30
















2















I have my own custom .phtml file that I would like to use for different collections of products. For example, I would like to use it to display the first n elements of category as well as the already seen products or related products.
In addition I need to able to decide for each block to show/hide the price of the products.



Is it possible to do it with the core modules of Magento or should I extend the existing ones and write a new module?










share|improve this question























  • Can you expand on 'I would like to use for different collections of products'. Does your template contains just product listings like the default list.phtml template or more page content? Are you showing more than one collection of products on the page or just one which has been modified?

    – Jonathan Hussey
    Nov 26 '14 at 12:30














2












2








2








I have my own custom .phtml file that I would like to use for different collections of products. For example, I would like to use it to display the first n elements of category as well as the already seen products or related products.
In addition I need to able to decide for each block to show/hide the price of the products.



Is it possible to do it with the core modules of Magento or should I extend the existing ones and write a new module?










share|improve this question














I have my own custom .phtml file that I would like to use for different collections of products. For example, I would like to use it to display the first n elements of category as well as the already seen products or related products.
In addition I need to able to decide for each block to show/hide the price of the products.



Is it possible to do it with the core modules of Magento or should I extend the existing ones and write a new module?







magento-1.9 product-list module blocks






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '14 at 10:15









gianis6gianis6

7051332




7051332













  • Can you expand on 'I would like to use for different collections of products'. Does your template contains just product listings like the default list.phtml template or more page content? Are you showing more than one collection of products on the page or just one which has been modified?

    – Jonathan Hussey
    Nov 26 '14 at 12:30



















  • Can you expand on 'I would like to use for different collections of products'. Does your template contains just product listings like the default list.phtml template or more page content? Are you showing more than one collection of products on the page or just one which has been modified?

    – Jonathan Hussey
    Nov 26 '14 at 12:30

















Can you expand on 'I would like to use for different collections of products'. Does your template contains just product listings like the default list.phtml template or more page content? Are you showing more than one collection of products on the page or just one which has been modified?

– Jonathan Hussey
Nov 26 '14 at 12:30





Can you expand on 'I would like to use for different collections of products'. Does your template contains just product listings like the default list.phtml template or more page content? Are you showing more than one collection of products on the page or just one which has been modified?

– Jonathan Hussey
Nov 26 '14 at 12:30










2 Answers
2






active

oldest

votes


















1














You have a wonderful example for this in Magento itself. I am talking about Mage_Catalog module. In order to show different categories, Magento uses the same phtml file and same block itself !.



This is how Magento makes it possible. The template file that is used to show the product lists (in general) is app/design/frontend/base/default/catalog/product/list.phtml. The block which "controls" this template in almost all case is Mage_Catalog_Block_Product_List. How magento shows different product collection using the same block and same phtml file ? To get the answer, you need to have a look on the controller files that is processing different criterias. For an example, I am taking the CategoryController which will use to show different categories in frontend. Have a look on the viewAction (Module is Mage_Catalog).



 /**
* Category view action
*/
public function viewAction()
{
if ($category = $this->_initCatagory()) {
$design = Mage::getSingleton('catalog/design');
$settings = $design->getDesignSettings($category);
....
}


First of all it calls $this->_initCatagory(). So let us have a look on what is that method does.



protected function _initCatagory()
{
Mage::dispatchEvent('catalog_controller_category_init_before', array('controller_action' => $this));
$categoryId = (int) $this->getRequest()->getParam('id', false);
if (!$categoryId) {
return false;
}
...
}


There are lot of things happens here. BUt you need to look on this



 $this->getRequest()->getParam('id', false);


Here the controller retrieves the passing id through the url which is corresponding to a category. Using this category id, the controller loads the layout. The category view layout holds Mage_Catalog_Block_Product_List block and it then uses this category id to load the product collection. Have look on the method _getProductCollection() that comes inside this block class.



So in general, there are lot of methods to do this. If the collection that you need to show an entirely different and you still need to use the same template, then there is an alternative way. For each product collection, you need to create distinct block classes.



An example is shown below. Suppose your template is using a common function, let's say getMyCollection() to show the product. So your template somewhat looks like this.



FIle : app/design/frontend/<package>/<theme>/template/<namespace_module>/custom/product/list.phtml



  $collection = $this->getMyCollection();
foreach ($collection as $item){
//do something
}


Now in your module, you have three blocks that looks like this.



File : app/code/local/Namespace/Module/Block/Custom/Product/List.php



<?php
class Namespace_Module_Block_Custom_Product_List extends Mage_Core_Block_Template
{
protected $_collection = '';

public function setListCollection($collection)
{
$this->_collection = $collection;
return $this;
}

public function getListCollection($collection)
{
return $this->_collection = $collection;
}
}


File : app/code/local/Namespace/Module/Block/Custom/Product/List/List1.php



    <?php
class Namespace_Module_Block_Custom_Product_List_List1 extends Namespace_Module_Block_Custom_Product_List
{

public function setList1Collection()
{
//do some collection logic here
$collection = Mage::getCollection('something/something')
->addAttributeToFilter(...)
.....
->load();
//setting collection
$this->setListCollection($collection);
}

public function getMyCollection()
{
return $this->getListCollection();
}

}


File : app/code/local/Namespace/Module/Block/Custom/Product/List/List2.php



    <?php
class Namespace_Module_Block_Custom_Product_List_List2 extends Namespace_Module_Block_Custom_Product_List
{

public function setList2Collection()
{
//do some collection logic here
$collection = Mage::getCollection('something/something')
->addAttributeToFilter(...)
.....
->load();
//setting collection
$this->setListCollection($collection);
}

public function getMyCollection()
{
return $this->getListCollection();
}

}


You can see that List1 and List2 has getMyCollection() method. BUt they return differnt collection, because we are setting different collection on the parent List block using corresponding setList{1,2}Collection() method.



Now you are declaring two blocks in your layout like this.



For Namespace_Modulename_ListController::list1Action()



 <block type="namespace_module/custom/product/list/list1" name="list1.list" as="some.alias" template="app/design/frontend/<package>/<theme>/template/<namespace_module>/custom/product/list.phtml" />


For Namespace_Modulename_ListController::list2Action()



 <block type="namespace_module/custom/product/list/list2" name="list2.list" as="some.other.alias" template="app/design/frontend/<package>/<theme>/template/<namespace_module>/custom/product/list.phtml" />


This will load two different product collection, but still uses same template.



Hope you get some idea now. So ultimately you need to decide which method is good for you and thus proceed.



Good Luck !






share|improve this answer































    0














    excellent explanation. helped me a lot. thanks






    share|improve this answer








    New contributor




    Manish Mishra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.




















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "479"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f45463%2fuse-the-same-phtml-template-to-show-different-collection-of-products%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You have a wonderful example for this in Magento itself. I am talking about Mage_Catalog module. In order to show different categories, Magento uses the same phtml file and same block itself !.



      This is how Magento makes it possible. The template file that is used to show the product lists (in general) is app/design/frontend/base/default/catalog/product/list.phtml. The block which "controls" this template in almost all case is Mage_Catalog_Block_Product_List. How magento shows different product collection using the same block and same phtml file ? To get the answer, you need to have a look on the controller files that is processing different criterias. For an example, I am taking the CategoryController which will use to show different categories in frontend. Have a look on the viewAction (Module is Mage_Catalog).



       /**
      * Category view action
      */
      public function viewAction()
      {
      if ($category = $this->_initCatagory()) {
      $design = Mage::getSingleton('catalog/design');
      $settings = $design->getDesignSettings($category);
      ....
      }


      First of all it calls $this->_initCatagory(). So let us have a look on what is that method does.



      protected function _initCatagory()
      {
      Mage::dispatchEvent('catalog_controller_category_init_before', array('controller_action' => $this));
      $categoryId = (int) $this->getRequest()->getParam('id', false);
      if (!$categoryId) {
      return false;
      }
      ...
      }


      There are lot of things happens here. BUt you need to look on this



       $this->getRequest()->getParam('id', false);


      Here the controller retrieves the passing id through the url which is corresponding to a category. Using this category id, the controller loads the layout. The category view layout holds Mage_Catalog_Block_Product_List block and it then uses this category id to load the product collection. Have look on the method _getProductCollection() that comes inside this block class.



      So in general, there are lot of methods to do this. If the collection that you need to show an entirely different and you still need to use the same template, then there is an alternative way. For each product collection, you need to create distinct block classes.



      An example is shown below. Suppose your template is using a common function, let's say getMyCollection() to show the product. So your template somewhat looks like this.



      FIle : app/design/frontend/<package>/<theme>/template/<namespace_module>/custom/product/list.phtml



        $collection = $this->getMyCollection();
      foreach ($collection as $item){
      //do something
      }


      Now in your module, you have three blocks that looks like this.



      File : app/code/local/Namespace/Module/Block/Custom/Product/List.php



      <?php
      class Namespace_Module_Block_Custom_Product_List extends Mage_Core_Block_Template
      {
      protected $_collection = '';

      public function setListCollection($collection)
      {
      $this->_collection = $collection;
      return $this;
      }

      public function getListCollection($collection)
      {
      return $this->_collection = $collection;
      }
      }


      File : app/code/local/Namespace/Module/Block/Custom/Product/List/List1.php



          <?php
      class Namespace_Module_Block_Custom_Product_List_List1 extends Namespace_Module_Block_Custom_Product_List
      {

      public function setList1Collection()
      {
      //do some collection logic here
      $collection = Mage::getCollection('something/something')
      ->addAttributeToFilter(...)
      .....
      ->load();
      //setting collection
      $this->setListCollection($collection);
      }

      public function getMyCollection()
      {
      return $this->getListCollection();
      }

      }


      File : app/code/local/Namespace/Module/Block/Custom/Product/List/List2.php



          <?php
      class Namespace_Module_Block_Custom_Product_List_List2 extends Namespace_Module_Block_Custom_Product_List
      {

      public function setList2Collection()
      {
      //do some collection logic here
      $collection = Mage::getCollection('something/something')
      ->addAttributeToFilter(...)
      .....
      ->load();
      //setting collection
      $this->setListCollection($collection);
      }

      public function getMyCollection()
      {
      return $this->getListCollection();
      }

      }


      You can see that List1 and List2 has getMyCollection() method. BUt they return differnt collection, because we are setting different collection on the parent List block using corresponding setList{1,2}Collection() method.



      Now you are declaring two blocks in your layout like this.



      For Namespace_Modulename_ListController::list1Action()



       <block type="namespace_module/custom/product/list/list1" name="list1.list" as="some.alias" template="app/design/frontend/<package>/<theme>/template/<namespace_module>/custom/product/list.phtml" />


      For Namespace_Modulename_ListController::list2Action()



       <block type="namespace_module/custom/product/list/list2" name="list2.list" as="some.other.alias" template="app/design/frontend/<package>/<theme>/template/<namespace_module>/custom/product/list.phtml" />


      This will load two different product collection, but still uses same template.



      Hope you get some idea now. So ultimately you need to decide which method is good for you and thus proceed.



      Good Luck !






      share|improve this answer




























        1














        You have a wonderful example for this in Magento itself. I am talking about Mage_Catalog module. In order to show different categories, Magento uses the same phtml file and same block itself !.



        This is how Magento makes it possible. The template file that is used to show the product lists (in general) is app/design/frontend/base/default/catalog/product/list.phtml. The block which "controls" this template in almost all case is Mage_Catalog_Block_Product_List. How magento shows different product collection using the same block and same phtml file ? To get the answer, you need to have a look on the controller files that is processing different criterias. For an example, I am taking the CategoryController which will use to show different categories in frontend. Have a look on the viewAction (Module is Mage_Catalog).



         /**
        * Category view action
        */
        public function viewAction()
        {
        if ($category = $this->_initCatagory()) {
        $design = Mage::getSingleton('catalog/design');
        $settings = $design->getDesignSettings($category);
        ....
        }


        First of all it calls $this->_initCatagory(). So let us have a look on what is that method does.



        protected function _initCatagory()
        {
        Mage::dispatchEvent('catalog_controller_category_init_before', array('controller_action' => $this));
        $categoryId = (int) $this->getRequest()->getParam('id', false);
        if (!$categoryId) {
        return false;
        }
        ...
        }


        There are lot of things happens here. BUt you need to look on this



         $this->getRequest()->getParam('id', false);


        Here the controller retrieves the passing id through the url which is corresponding to a category. Using this category id, the controller loads the layout. The category view layout holds Mage_Catalog_Block_Product_List block and it then uses this category id to load the product collection. Have look on the method _getProductCollection() that comes inside this block class.



        So in general, there are lot of methods to do this. If the collection that you need to show an entirely different and you still need to use the same template, then there is an alternative way. For each product collection, you need to create distinct block classes.



        An example is shown below. Suppose your template is using a common function, let's say getMyCollection() to show the product. So your template somewhat looks like this.



        FIle : app/design/frontend/<package>/<theme>/template/<namespace_module>/custom/product/list.phtml



          $collection = $this->getMyCollection();
        foreach ($collection as $item){
        //do something
        }


        Now in your module, you have three blocks that looks like this.



        File : app/code/local/Namespace/Module/Block/Custom/Product/List.php



        <?php
        class Namespace_Module_Block_Custom_Product_List extends Mage_Core_Block_Template
        {
        protected $_collection = '';

        public function setListCollection($collection)
        {
        $this->_collection = $collection;
        return $this;
        }

        public function getListCollection($collection)
        {
        return $this->_collection = $collection;
        }
        }


        File : app/code/local/Namespace/Module/Block/Custom/Product/List/List1.php



            <?php
        class Namespace_Module_Block_Custom_Product_List_List1 extends Namespace_Module_Block_Custom_Product_List
        {

        public function setList1Collection()
        {
        //do some collection logic here
        $collection = Mage::getCollection('something/something')
        ->addAttributeToFilter(...)
        .....
        ->load();
        //setting collection
        $this->setListCollection($collection);
        }

        public function getMyCollection()
        {
        return $this->getListCollection();
        }

        }


        File : app/code/local/Namespace/Module/Block/Custom/Product/List/List2.php



            <?php
        class Namespace_Module_Block_Custom_Product_List_List2 extends Namespace_Module_Block_Custom_Product_List
        {

        public function setList2Collection()
        {
        //do some collection logic here
        $collection = Mage::getCollection('something/something')
        ->addAttributeToFilter(...)
        .....
        ->load();
        //setting collection
        $this->setListCollection($collection);
        }

        public function getMyCollection()
        {
        return $this->getListCollection();
        }

        }


        You can see that List1 and List2 has getMyCollection() method. BUt they return differnt collection, because we are setting different collection on the parent List block using corresponding setList{1,2}Collection() method.



        Now you are declaring two blocks in your layout like this.



        For Namespace_Modulename_ListController::list1Action()



         <block type="namespace_module/custom/product/list/list1" name="list1.list" as="some.alias" template="app/design/frontend/<package>/<theme>/template/<namespace_module>/custom/product/list.phtml" />


        For Namespace_Modulename_ListController::list2Action()



         <block type="namespace_module/custom/product/list/list2" name="list2.list" as="some.other.alias" template="app/design/frontend/<package>/<theme>/template/<namespace_module>/custom/product/list.phtml" />


        This will load two different product collection, but still uses same template.



        Hope you get some idea now. So ultimately you need to decide which method is good for you and thus proceed.



        Good Luck !






        share|improve this answer


























          1












          1








          1







          You have a wonderful example for this in Magento itself. I am talking about Mage_Catalog module. In order to show different categories, Magento uses the same phtml file and same block itself !.



          This is how Magento makes it possible. The template file that is used to show the product lists (in general) is app/design/frontend/base/default/catalog/product/list.phtml. The block which "controls" this template in almost all case is Mage_Catalog_Block_Product_List. How magento shows different product collection using the same block and same phtml file ? To get the answer, you need to have a look on the controller files that is processing different criterias. For an example, I am taking the CategoryController which will use to show different categories in frontend. Have a look on the viewAction (Module is Mage_Catalog).



           /**
          * Category view action
          */
          public function viewAction()
          {
          if ($category = $this->_initCatagory()) {
          $design = Mage::getSingleton('catalog/design');
          $settings = $design->getDesignSettings($category);
          ....
          }


          First of all it calls $this->_initCatagory(). So let us have a look on what is that method does.



          protected function _initCatagory()
          {
          Mage::dispatchEvent('catalog_controller_category_init_before', array('controller_action' => $this));
          $categoryId = (int) $this->getRequest()->getParam('id', false);
          if (!$categoryId) {
          return false;
          }
          ...
          }


          There are lot of things happens here. BUt you need to look on this



           $this->getRequest()->getParam('id', false);


          Here the controller retrieves the passing id through the url which is corresponding to a category. Using this category id, the controller loads the layout. The category view layout holds Mage_Catalog_Block_Product_List block and it then uses this category id to load the product collection. Have look on the method _getProductCollection() that comes inside this block class.



          So in general, there are lot of methods to do this. If the collection that you need to show an entirely different and you still need to use the same template, then there is an alternative way. For each product collection, you need to create distinct block classes.



          An example is shown below. Suppose your template is using a common function, let's say getMyCollection() to show the product. So your template somewhat looks like this.



          FIle : app/design/frontend/<package>/<theme>/template/<namespace_module>/custom/product/list.phtml



            $collection = $this->getMyCollection();
          foreach ($collection as $item){
          //do something
          }


          Now in your module, you have three blocks that looks like this.



          File : app/code/local/Namespace/Module/Block/Custom/Product/List.php



          <?php
          class Namespace_Module_Block_Custom_Product_List extends Mage_Core_Block_Template
          {
          protected $_collection = '';

          public function setListCollection($collection)
          {
          $this->_collection = $collection;
          return $this;
          }

          public function getListCollection($collection)
          {
          return $this->_collection = $collection;
          }
          }


          File : app/code/local/Namespace/Module/Block/Custom/Product/List/List1.php



              <?php
          class Namespace_Module_Block_Custom_Product_List_List1 extends Namespace_Module_Block_Custom_Product_List
          {

          public function setList1Collection()
          {
          //do some collection logic here
          $collection = Mage::getCollection('something/something')
          ->addAttributeToFilter(...)
          .....
          ->load();
          //setting collection
          $this->setListCollection($collection);
          }

          public function getMyCollection()
          {
          return $this->getListCollection();
          }

          }


          File : app/code/local/Namespace/Module/Block/Custom/Product/List/List2.php



              <?php
          class Namespace_Module_Block_Custom_Product_List_List2 extends Namespace_Module_Block_Custom_Product_List
          {

          public function setList2Collection()
          {
          //do some collection logic here
          $collection = Mage::getCollection('something/something')
          ->addAttributeToFilter(...)
          .....
          ->load();
          //setting collection
          $this->setListCollection($collection);
          }

          public function getMyCollection()
          {
          return $this->getListCollection();
          }

          }


          You can see that List1 and List2 has getMyCollection() method. BUt they return differnt collection, because we are setting different collection on the parent List block using corresponding setList{1,2}Collection() method.



          Now you are declaring two blocks in your layout like this.



          For Namespace_Modulename_ListController::list1Action()



           <block type="namespace_module/custom/product/list/list1" name="list1.list" as="some.alias" template="app/design/frontend/<package>/<theme>/template/<namespace_module>/custom/product/list.phtml" />


          For Namespace_Modulename_ListController::list2Action()



           <block type="namespace_module/custom/product/list/list2" name="list2.list" as="some.other.alias" template="app/design/frontend/<package>/<theme>/template/<namespace_module>/custom/product/list.phtml" />


          This will load two different product collection, but still uses same template.



          Hope you get some idea now. So ultimately you need to decide which method is good for you and thus proceed.



          Good Luck !






          share|improve this answer













          You have a wonderful example for this in Magento itself. I am talking about Mage_Catalog module. In order to show different categories, Magento uses the same phtml file and same block itself !.



          This is how Magento makes it possible. The template file that is used to show the product lists (in general) is app/design/frontend/base/default/catalog/product/list.phtml. The block which "controls" this template in almost all case is Mage_Catalog_Block_Product_List. How magento shows different product collection using the same block and same phtml file ? To get the answer, you need to have a look on the controller files that is processing different criterias. For an example, I am taking the CategoryController which will use to show different categories in frontend. Have a look on the viewAction (Module is Mage_Catalog).



           /**
          * Category view action
          */
          public function viewAction()
          {
          if ($category = $this->_initCatagory()) {
          $design = Mage::getSingleton('catalog/design');
          $settings = $design->getDesignSettings($category);
          ....
          }


          First of all it calls $this->_initCatagory(). So let us have a look on what is that method does.



          protected function _initCatagory()
          {
          Mage::dispatchEvent('catalog_controller_category_init_before', array('controller_action' => $this));
          $categoryId = (int) $this->getRequest()->getParam('id', false);
          if (!$categoryId) {
          return false;
          }
          ...
          }


          There are lot of things happens here. BUt you need to look on this



           $this->getRequest()->getParam('id', false);


          Here the controller retrieves the passing id through the url which is corresponding to a category. Using this category id, the controller loads the layout. The category view layout holds Mage_Catalog_Block_Product_List block and it then uses this category id to load the product collection. Have look on the method _getProductCollection() that comes inside this block class.



          So in general, there are lot of methods to do this. If the collection that you need to show an entirely different and you still need to use the same template, then there is an alternative way. For each product collection, you need to create distinct block classes.



          An example is shown below. Suppose your template is using a common function, let's say getMyCollection() to show the product. So your template somewhat looks like this.



          FIle : app/design/frontend/<package>/<theme>/template/<namespace_module>/custom/product/list.phtml



            $collection = $this->getMyCollection();
          foreach ($collection as $item){
          //do something
          }


          Now in your module, you have three blocks that looks like this.



          File : app/code/local/Namespace/Module/Block/Custom/Product/List.php



          <?php
          class Namespace_Module_Block_Custom_Product_List extends Mage_Core_Block_Template
          {
          protected $_collection = '';

          public function setListCollection($collection)
          {
          $this->_collection = $collection;
          return $this;
          }

          public function getListCollection($collection)
          {
          return $this->_collection = $collection;
          }
          }


          File : app/code/local/Namespace/Module/Block/Custom/Product/List/List1.php



              <?php
          class Namespace_Module_Block_Custom_Product_List_List1 extends Namespace_Module_Block_Custom_Product_List
          {

          public function setList1Collection()
          {
          //do some collection logic here
          $collection = Mage::getCollection('something/something')
          ->addAttributeToFilter(...)
          .....
          ->load();
          //setting collection
          $this->setListCollection($collection);
          }

          public function getMyCollection()
          {
          return $this->getListCollection();
          }

          }


          File : app/code/local/Namespace/Module/Block/Custom/Product/List/List2.php



              <?php
          class Namespace_Module_Block_Custom_Product_List_List2 extends Namespace_Module_Block_Custom_Product_List
          {

          public function setList2Collection()
          {
          //do some collection logic here
          $collection = Mage::getCollection('something/something')
          ->addAttributeToFilter(...)
          .....
          ->load();
          //setting collection
          $this->setListCollection($collection);
          }

          public function getMyCollection()
          {
          return $this->getListCollection();
          }

          }


          You can see that List1 and List2 has getMyCollection() method. BUt they return differnt collection, because we are setting different collection on the parent List block using corresponding setList{1,2}Collection() method.



          Now you are declaring two blocks in your layout like this.



          For Namespace_Modulename_ListController::list1Action()



           <block type="namespace_module/custom/product/list/list1" name="list1.list" as="some.alias" template="app/design/frontend/<package>/<theme>/template/<namespace_module>/custom/product/list.phtml" />


          For Namespace_Modulename_ListController::list2Action()



           <block type="namespace_module/custom/product/list/list2" name="list2.list" as="some.other.alias" template="app/design/frontend/<package>/<theme>/template/<namespace_module>/custom/product/list.phtml" />


          This will load two different product collection, but still uses same template.



          Hope you get some idea now. So ultimately you need to decide which method is good for you and thus proceed.



          Good Luck !







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '14 at 12:01









          Rajeev K TomyRajeev K Tomy

          14.6k54589




          14.6k54589

























              0














              excellent explanation. helped me a lot. thanks






              share|improve this answer








              New contributor




              Manish Mishra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

























                0














                excellent explanation. helped me a lot. thanks






                share|improve this answer








                New contributor




                Manish Mishra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.























                  0












                  0








                  0







                  excellent explanation. helped me a lot. thanks






                  share|improve this answer








                  New contributor




                  Manish Mishra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.










                  excellent explanation. helped me a lot. thanks







                  share|improve this answer








                  New contributor




                  Manish Mishra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  Manish Mishra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 10 mins ago









                  Manish MishraManish Mishra

                  1




                  1




                  New contributor




                  Manish Mishra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Manish Mishra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Manish Mishra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Magento Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f45463%2fuse-the-same-phtml-template-to-show-different-collection-of-products%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

                      How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

                      夢乃愛華...