繁体   English   中英

列表页面上的Magento自定义过滤器提供了错误的产品计数

[英]Magento custom filter on listing page gives incorrect product count

我正在显示产品列表页。 我应用了自定义过滤器来过滤出产品。

list.phtml代码:

$postCodeQueryStr = $this->getRequest()->getParam('post_code');

$_productCollection = $this->getLoadedProductCollection()
        ->addAttributeToFilter(
                array(
                    array('attribute'=>'town', 'like' => ''.$postCodeQueryStr.'%'),
                    array('attribute'=>'post_code', 'like' => ''.$postCodeQueryStr.'%')
                )
        )

        ->clear()->addAttributeToSort('created_at', 'DESC');

显示我使用过的数量或产品:

echo $_productCollection->getSize()

这里最初是通过$this->getLoadedProductCollection()获得所有产品,然后使用addAttributeToFilter()对其进行过滤。 它实际上显示了有限的产品。 我的意思是,它过滤掉以显示与标准相关的有限产品。

在此处输入图片说明

但是,它没有显示过滤的计数,而是显示了产品的总数,但没有显示按条件过滤后的计数。

我在列表中得到的产品有限,但是它们的数量很旧(在过滤器之前)。

问题是您在模板级别进行过滤,并且计数在此之前在工具栏中呈现。 所以您必须将过滤器放入

function getLoadedProductCollection() 

进一步使用该功能

public function getProductCollection()
{
    $collection = Mage::getResourceModel('catalog/product_collection')
        ->setStoreId($this->getStoreId())
        ->addCategoryFilter($this);
    return $collection;
}

位于app / code / core / Mage / Catalog / Model / Category.php

您可以在此处添加过滤器。

我建议您不要编辑此核心文件并将其切换到本地文件夹。

让我知道是否有任何疑问。

希望能完成您的工作。

您可以根据需要修改以下功能,只需按以下所示替换此功能即可。 谢谢

    public function getProductCollection()
    {
        $postCodeQueryStr = $_POST['post_code'];
        $collection = Mage::getResourceModel('catalog/product_collection')
            ->setStoreId($this->getStoreId())
            ->addCategoryFilter($this);
        if($postCodeQueryStr!=''){
$collection->addAttributeToFilter(
                array(
                    array('attribute'=>'town', 'like' => ''.$postCodeQueryStr.'%'),
                    array('attribute'=>'post_code', 'like' => ''.$postCodeQueryStr.'%')
                )
        )
        ->clear()->addAttributeToSort('created_at', 'DESC');
            }
        return $collection;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM