简体   繁体   English

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

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

I am displaying listing page with products. 我正在显示产品列表页。 And I applied custom filter to filter out the products. 我应用了自定义过滤器来过滤出产品。

Code in list.phtml : 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');

To show count or products I used: 显示我使用过的数量或产品:

echo $_productCollection->getSize()

Here initially through $this->getLoadedProductCollection() , I get All products and then I am filter it using addAttributeToFilter() . 这里最初是通过$this->getLoadedProductCollection()获得所有产品,然后使用addAttributeToFilter()对其进行过滤。 It shows limited products actually. 它实际上显示了有限的产品。 By this I mean it filters out to show limited products related to criteria. 我的意思是,它过滤掉以显示与标准相关的有限产品。

在此处输入图片说明

But instead of showing filtered count, it shows whole count of products, it doesn't show count after filtering by criteria. 但是,它没有显示过滤的计数,而是显示了产品的总数,但没有显示按条件过滤后的计数。

I am getting limited products in list, but count of them is old (before filter). 我在列表中得到的产品有限,但是它们的数量很旧(在过滤器之前)。

The problem is you are filtering on the template level , and the count is rendered in the toolbar before that. 问题是您在模板级别进行过滤,并且计数在此之前在工具栏中呈现。 so you have to put your filter in the 所以您必须将过滤器放入

function getLoadedProductCollection() 

which further uses the function 进一步使用该功能

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

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

you can add your filter here . 您可以在此处添加过滤器。

i will suggest you to not to edit this core file and switch it to your local folder . 我建议您不要编辑此核心文件并将其切换到本地文件夹。

let me know if there is any doubt. 让我知道是否有任何疑问。

thanks hope it will do your work. 希望能完成您的工作。

you can modify the function like below according to your needs Just replace this function as below and you will all done. 您可以根据需要修改以下功能,只需按以下所示替换此功能即可。 thanks 谢谢

    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