【发布时间】:2016-01-06 08:50:18
【问题描述】:
我正在显示产品列表页面。我应用了自定义过滤器来过滤掉产品。
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() 过滤它。它实际上显示了有限的产品。我的意思是它过滤掉显示与标准相关的有限产品。
但不是显示过滤后的计数,而是显示整个产品计数,按标准过滤后不显示计数。
我在列表中获得了有限的产品,但它们的数量是旧的(过滤之前)。
【问题讨论】:
-
clear() 的作用是什么?尝试删除它。
-
我已经试过删除了,还是不行
-
你知道如何从集合中提取sql查询吗?手动检查结果集。
-
打印效果很好,但问题是它没有正确显示计数。而且我们不能使用常规的 sql 查询,因为它使用 OOP 来设置许多属性等。您是否在 magento 中使用过滤列表?
-
评估这个:
echo $_productCollection->getSelect()->__toString();(注意有两个_),将查询复制并粘贴到mysql(最好用https://dev.mysql.com/downloads/workbench/),并检查结果集计数是否正确。
标签: php mysql magento filter product