Web Development Blog – Alphadigital

Tips and Experiences

Magento – Filter by multiple categories
Thursday, 11 de June de 2009

We know that we can call a block to show products in your home page or in any CMS page. We MUST specify category ID:

{{block type=”catalog/product_list” category_id=”5″
template=”catalog/product/list.phtml”}}

Now, what if I want to show products from multiple categories or simply show all my products?

Well, here’s what I did. I created a custom module to extend some core classes, and I did it this way:

Activate custom module

app/etc/modules/ModuleName_All.xml

<?xml version="1.0"?>
<config>
   <modules>
      <ModuleName_Catalog>
         <active>true</active>
         <codePool>local</codePool>
      </ModuleName_Catalog>
   </modules>
</config>

Set up the module

Here, we set up our blocks and extend our Colletion class.

app/code/local/ModuleName/Catalog/etc/config.xml

<?xml version="1.0"?>
<config>
   <modules>
      <ModuleName_Catalog>
         <version>0.1.0</version>
      </ModuleName_Catalog>
   </modules>
   <global>
      <blocks>
         <ModuleName_catalog>
            <class>ModuleName_Catalog_Block</class>
         </ModuleName_catalog>
      </blocks>
      <models>
         <catalog_resource_eav_mysql4>
            <rewrite>
               <product_collection>
                  ModuleName_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
               </product_collection>
            </rewrite>
         </catalog_resource_eav_mysql4>
      </models>
   </global>
</config>

Listcategories class

app/code/local/ModuleName/Catalog/Block/Product/Listcategories.php

class ModuleName_Catalog_Block_Product_Listcategories
  extends Mage_Catalog_Block_Product_List{

  //same function from Mage_Catalog_Block_Product_List
  protected function _getProductCollection(){

    // get simple product collection
    $this->_productCollection = Mage::getModel('catalog/product')->getCollection();
    $this->_productCollection->addAttributeToSelect('*');

    // if we receive categories parameter we filter with our custom function addCategoriesFilter
    // currently there's a addCategoryFilter, but only accepts 1 category
    // if no param is passed, simply show everything
    if($this->getCategories()!="")
      $this->_productCollection->addCategoriesFilter($this->getCategories());

    return $this->_productCollection;
  }
}

function addCategoriesFilter

Now, we have to extend Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection class and add the addCategoriesFilter function:

app/code/local/ModuleName/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php

class ModuleName_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
  extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection{

  public function addCategoriesFilter($categories){

  $alias = 'cat_index';
  $categoryCondition = $this->getConnection()->quoteInto(
    $alias.'.product_id=e.entity_id AND '.$alias.'.store_id=? AND ',
    $this->getStoreId()
  );

  $categoryCondition.= $alias.'.category_id IN ('.$categories.')';

  $this->getSelect()->joinInner(
    array($alias => $this->getTable('catalog/category_product_index')),
    $categoryCondition,
    array('position'=>'position')
  );

  $this->_categoryIndexJoined = true;
  $this->_joinFields['position'] = array('table'=>$alias, 'field'=>'position' );

  return $this;

  }
}

Now on a CMS page you call your block this way:

{{block type=”ModuleName_catalog/product_listcategories” categories=”14,16,18″
template=”catalog/product/list.phtml”}}

  • Share/Bookmark

8 comentarios

  • criography 06.18.2009 - 10:24

    Oh man! you have no idea! You’ve just saved me couple of days of digging through core files and getting even more irritated with Magento;) Now I have to figure out how to do the same thing with attributes, add some nice mod_rewrite to it and pray everything works (;

  • Heriquet 06.24.2009 - 15:15

    Hi,

    Thanks for this usefull tutorial. I tried this but I got this error at pages load :

    Warning: Varien_Autoload::include(
    //////////////////ModuleName/Catalog/Model/Resource/Eav/Mysql4/Product/Collection
    ///////////////.php) [function.Varien-Autoload-include]: failed to open stream: No such file or directory i

    Do you know what could happen ?

    Thanks

    Heriquet

  • flash 07.05.2009 - 3:00

    Cool!

  • Dominik 08.19.2009 - 12:04

    Hi, thanks for your great tutorial, doesn’t quite work the way I’d like, but at the moment I’m still hopeful.

    @Heriquet

    The problem you describe comes up, because of the whitespace in the config.xml.

    ModuleName_Catalog_Model_Resource_Eav_Mysql4_Product_Collection

    if you put the code above on a single line and delete all whitespace between the tags, the error should disappear

  • pablo 08.03.2010 - 13:50

    Hi,
    How can I filter to get products that are both in category1 and category2? Your code implements or condition.

    Can I combine addCategoriesFilter with prepareProductCollection in Mage_Catalog_Model_Layer.php ?
    I’m trying to show in the product list products that are in some category in addition to the selected category and rest of the filters.

    Thanks

  • nagaraja 08.07.2010 - 10:31

    Hi, I have implemented above rule for getting all products from store .but seems to be happen.
    getting following error on magento 1.4.0.1

    a:5:{i:0;s:312:”Warning: include(//

    //////////////////ModuleName/Catalog/Model/Resource/Eav/Mysql4/Product/Collection//

    ///////////////.php) [function.include]: failed to open stream: No such file or directory in /home/herbalsu/public_html/ayurvedic_editing_f/lib/Varien/Autoload.php on line 93″;i:1;s:6515:”#0 /home/herbalsu/public_html/ayurvedic_editing_f/lib/Varien/Autoload.php(93): mageCoreErrorHandler(2, ‘include(//??///…’, ‘/home/herbalsu/…’, 93, Array)
    #1 /home/herbalsu/public_html/ayurvedic_editing_f/lib/Varien/Autoload.php(93): Varien_Autoload::autoload()
    #2 [internal function]: Varien_Autoload->autoload(‘ ?? …’)
    #3 [internal function]: spl_autoload_call(‘ ?? …’)
    #4 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Model/Config.php(1204): class_exists(‘ ?? …’)
    #5 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Model/Config.php(1239): Mage_Core_Model_Config->getModelInstance(‘catalog_resourc…’, Array)
    #6 /home/herbalsu/public_html/ayurvedic_editing_f/app/Mage.php(460): Mage_Core_Model_Config->getResourceModelInstance(‘catalog/product…’, Array)
    #7 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Catalog/Model/Product.php(133): Mage::getResourceModel(‘catalog/product…’)
    #8 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Model/Abstract.php(212): Mage_Catalog_Model_Product->getResourceCollection()
    #9 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Sales/Model/Mysql4/Quote/Item/Collection.php(165): Mage_Core_Model_Abstract->getCollection()
    #10 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Sales/Model/Mysql4/Quote/Item/Collection.php(129): Mage_Sales_Model_Mysql4_Quote_Item_Collection->_assignProducts()
    #11 /home/herbalsu/public_html/ayurvedic_editing_f/lib/Varien/Data/Collection/Db.php(594): Mage_Sales_Model_Mysql4_Quote_Item_Collection->_afterLoad()
    #12 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Model/Mysql4/Collection/Abstract.php(195): Varien_Data_Collection_Db->load(false, false)
    #13 /home/herbalsu/public_html/ayurvedic_editing_f/lib/Varien/Data/Collection.php(692): Mage_Core_Model_Mysql4_Collection_Abstract->load()
    #14 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Sales/Model/Quote.php(1093): Varien_Data_Collection->getIterator()
    #15 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Sales/Model/Quote.php(973): Mage_Sales_Model_Quote->isVirtual()
    #16 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Checkout/Block/Cart/Abstract.php(185): Mage_Sales_Model_Quote->getTotals()
    #17 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Checkout/Block/Cart/Abstract.php(179): Mage_Checkout_Block_Cart_Abstract->getTotalsCache()
    #18 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Checkout/Block/Cart/Sidebar.php(121): Mage_Checkout_Block_Cart_Abstract->getTotals()
    #19 /home/herbalsu/public_html/ayurvedic_editing_f/app/design/frontend/default/herbol/template/checkout/cart/sidebar.phtml(46): Mage_Checkout_Block_Cart_Sidebar->getSubtotal()
    #20 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Block/Template.php(189): include(‘/home/herbalsu/…’)
    #21 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Block/Template.php(225): Mage_Core_Block_Template->fetchView(‘frontend/defaul…’)
    #22 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Block/Template.php(242): Mage_Core_Block_Template->renderView()
    #23 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Checkout/Block/Cart/Sidebar.php(236): Mage_Core_Block_Template->_toHtml()
    #24 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Block/Abstract.php(674): Mage_Checkout_Block_Cart_Sidebar->_toHtml()
    #25 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Block/Text/List.php(43): Mage_Core_Block_Abstract->toHtml()
    #26 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Block/Abstract.php(674): Mage_Core_Block_Text_List->_toHtml()
    #27 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Block/Abstract.php(516): Mage_Core_Block_Abstract->toHtml()
    #28 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Block/Abstract.php(467): Mage_Core_Block_Abstract->_getChildHtml(‘right’, true)
    #29 /home/herbalsu/public_html/ayurvedic_editing_f/app/design/frontend/default/herbol/template/page/3columns.phtml(79): Mage_Core_Block_Abstract->getChildHtml(‘right’)
    #30 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Block/Template.php(189): include(‘/home/herbalsu/…’)
    #31 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Block/Template.php(225): Mage_Core_Block_Template->fetchView(‘frontend/defaul…’)
    #32 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Block/Template.php(242): Mage_Core_Block_Template->renderView()
    #33 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Block/Abstract.php(674): Mage_Core_Block_Template->_toHtml()
    #34 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Model/Layout.php(536): Mage_Core_Block_Abstract->toHtml()
    #35 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Controller/Varien/Action.php(389): Mage_Core_Model_Layout->getOutput()
    #36 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Cms/Helper/Page.php(126): Mage_Core_Controller_Varien_Action->renderLayout()
    #37 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Cms/Helper/Page.php(52): Mage_Cms_Helper_Page->_renderPage(Object(Mage_Cms_IndexController), ‘home’)
    #38 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Cms/controllers/IndexController.php(45): Mage_Cms_Helper_Page->renderPage(Object(Mage_Cms_IndexController), ‘home’)
    #39 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Mage_Cms_IndexController->indexAction()
    #40 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch(‘index’)
    #41 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Controller/Varien/Front.php(177): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
    #42 /home/herbalsu/public_html/ayurvedic_editing_f/app/code/core/Mage/Core/Model/App.php(304): Mage_Core_Controller_Varien_Front->dispatch()
    #43 /home/herbalsu/public_html/ayurvedic_editing_f/app/Mage.php(596): Mage_Core_Model_App->run(Array)
    #44 /home/herbalsu/public_html/ayurvedic_editing_f/index.php(78): Mage::run(”, ’store’)
    #45 {main}”;s:3:”url”;s:21:”/ayurvedic_editing_f/”;s:11:”script_name”;s:30:”/ayurvedic_editing_f/index.php”;s:4:”skin”;s:7:”default”;}

    Please give some for this

    Thanks and regards,

    Nagaraja Kharvi
    9019055436

  • nagaraja 08.07.2010 - 10:40

    sorry
    i got problem solved
    thank you

  • nagaraja 08.07.2010 - 10:49

    Hi Dominik ,
    Nagaraja Once again here. Above error problem solved and no products being displyed on cms page .Is there any specific reasons for this. Please reply quickly.

    empty page being displayed
    Regards
    Nagaraja

¿Qué opinas de este tema?