如何仅从类别列表中删除可配置产品

时间:2013-02-01 15:39:00

标签: php magento catalog

我只需在类别页面中显示简单的产品,但我无法将“不可见”设置为可配置,因为我需要配置中的活动产品页面。

我发现此代码可以从列表中删除配置:

  $_productCollection=$this->getLoadedProductCollection();
  $_productCollection = clone $this->getLoadedProductCollection();
  $_productCollection->clear()
                     ->addAttributeToFilter('type_id', 'simple')
                     ->load();

  $_helper = $this->helper('catalog/output');

它可以工作,但是,在分层导航中,可配置产品仍在计算中。它像“颜色:红色(2)”但我只有1个红色(简单)。 如何完全删除可配置产品?

1 个答案:

答案 0 :(得分:2)

分层导航使用单独加载的集合对象。

确保导航过滤器旁边的正确计数的一种可能方法是覆盖模型Mage_Catalog_Model_Layer并将过滤器添加到其函数Mage_Catalog_Model_Layer::prepareProductCollection

public function prepareProductCollection($collection)
    {
        $collection
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addUrlRewrite($this->getCurrentCategory()->getId())
            ->addAttributeToFilter('type_id', 'simple');

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

        return $this;
    }

要实现此目的,请在本地代码池中创建一个模块。在config.xml文件中,将以下节点添加到global节点

<models>
        <catalog>
            <rewrite>
                <layer>YourPackage_YourModule_Model_Rewrite_Layer</layer>
            </rewrite>
        </catalog>
</models>

在你的模块中,在'Model'文件夹下添加一个目录'Rewrite',并在其中创建一个文件Layer.php。在创建的文件Model/Rewrite/Layer.php中添加一个具有以下定义的类:

class YourPackage_YourModule_Model_Rewrite_Layer extends Mage_Catalog_Model_Layer {
}

将上述函数添加到此类中,清除缓存。

相关问题