0

After a lot of searching, I finally found a solution to add Layered navigation to the Magento Home Page. At first glance, it was working properly with filtered results as expected. However, there is a catch as the URLs for the filtered results all have an added 'root-catalog' in their urls. This causes a 404 - however, if I take out the 'root-catalog' the urls are working fine.

What am I missing? Please help! Help is appreciated in advance!

Code to add layered navigation to home page:

<reference name="left">
<block type="catalog/navigation" name="catalog.cat.leftnav" before="sidenav.left" template="catalog/navigation/left.phtml"/>
<block type="catalog/layer_view" name="catalog.leftnav" after="catalog.cat.leftnav" template="catalog/layer/view.phtml"/>
<action method="unsetChild"><alias>right.reports.product.viewed</alias></action>
<action method="unsetChild"><alias>right.reports.product.compared</alias></action>
</reference>
<reference name="content">
<block type="catalog/product_list" name="product_home" template="catalog/product/list.phtml">
<action method="setCategoryId">[b]<category_id>3</category_id>[/b]</action>
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager" />
</block>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
</block>
</reference>
Acer
  • 75
  • 1
  • 11

1 Answers1

0

Layered navigation needs a category on which to base to get products, sub categories on which to filter, etc. On the homepage then, it takes the root category by default, and the corresponding product collection acts like it would do on other "common" category pages : it uses URL rewrites linking to the current category.

To prevent current category to be used in product URL, you can rewrite the layer model by copying the file app/code/core/Mage/Catalog/Model/Layer.php to app/code/local/Mage/Catalog/Model/, and changing the prepareProductCollection($collection) in it with something like this :

public function prepareProductCollection($collection)
{
    $collection
        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents();

    if ($this->getCurrentCategory()->getId() == $this->getCurrentStore()->getRootCategoryId()) {
        $collection->addUrlRewrite(0);
    } else {
        $collection->addUrlRewrite($this->getCurrentCategory()->getId());
    }

    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

    return $this;
}
blmage
  • 4,214
  • 1
  • 23
  • 25
  • Thank you - where do I add this in list.phtml? – Acer Jun 11 '13 at 13:30
  • You can put it below the `foreach ($_productCollection as $_product):` – blmage Jun 11 '13 at 13:33
  • My bad, in your case URL rewrites are already present in the collection so Magento does not take care of the result of `getDoNotUseCategoryId()`. You could use `$product->unsUrlDataObject()` but this is not a really clean solution. The other way is to change the method `prepareProductCollection()` in the layer model (`Mage_Catalog_Model_Layer`) to make it use an empty value instead of the category ID when it adds URL rewrites for the root category. – blmage Jun 11 '13 at 13:49
  • Thank you - where can I find the layer model Mage_Catalog_Model_Layer (file) so I can modify it? – Acer Jun 11 '13 at 13:52
  • Found the file (Layer.php). What do I change in prepareProductCollection? – Acer Jun 11 '13 at 13:58
  • Updated my answer accordingly. This way, you won't need to use `$product->setDoNotUseCategoryId(true)` anymore – blmage Jun 11 '13 at 14:03
  • I implemented your update to prepareProductCollection and the /root-catalog/ is now gone from the url. However, when I click on the product, an exception is thrown.... ----- exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1' in C:\xampp\htdocs\magentolab-17\lib\Zend\Db\Statement\Pdo.php:228 Stack trace: #0 C:\xampp\htdocs\magentolab-17\lib\Zend\Db\Statement\Pdo.php(228): PDOStatement->execute(Array) – Acer Jun 11 '13 at 14:15
  • Can you paste the complete stack trace somewhere ? – blmage Jun 11 '13 at 14:26
  • There is an error when it tries to load a category, what do the product URLs look like now ? – blmage Jun 11 '13 at 14:48
  • They seem to only contain the product page and not the related category i.e. condenser-for-alfa-mito.html instead of cooling/condensers/condenser-for-alfa-mito.html – Acer Jun 11 '13 at 14:54
  • Can you debug the content of `$this->getCategoryId()` in the file `app/code/core/Mage/Catalog/Block/Product/List.php` on line 80, when the exception is thrown ? – blmage Jun 11 '13 at 15:09
  • The problem is that the related category is missing from the url. Whereas before the url contained the related category but also contained /root-catalog/. If I removed /root-catalog/ from the url manually, then the product was displayed correctly. Now, the code removes all categories in the url. Is it possible to modify the code to just remove /root-catalog/ from the url string? – Acer Jun 11 '13 at 15:14
  • No it's not really possible, but direct access to a product without a related category should not be a problem at all. From what the stack trace looks like, the problem is that `getCategoryId()` returns a category whereas it should return an ID in the product list block – blmage Jun 11 '13 at 15:24
  • Thank you for your time with this. Can you modify it to return an ID instead of a category? – Acer Jun 11 '13 at 15:28
  • It seems to be a Magento bug, which is quite strange since it was already reported in 2010 (with a solution to prevent it, you can find it here : http://www.magentocommerce.com/boards/v/viewthread/201874/). Or maybe it isn't fixed yet because the base design does not include any products list based on `Mage_Catalog_Block_Product_List` on the product page, so that it rarely occurs. – blmage Jun 11 '13 at 15:35
  • I've tried the solution (link) and it's working! Nearly went insane with Magento Frustration!! Thank you! I hugely appreciate your time, patience and help! – Acer Jun 12 '13 at 06:43
  • If any one has the same problem: follow the initial answer by bImage (update to Layer.php), then implement the solution to the bug on this link: http://www.magentocommerce.com/boards/v/viewthread/201874/ – Acer Jun 12 '13 at 06:47