magento layered navigation on home page - filtered items have wrong urls, causes 404

StackOverflow https://stackoverflow.com/questions/17044823

  •  31-05-2022
  •  | 
  •  

質問

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>
役に立ちましたか?

解決

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;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top