繁体   English   中英

添加自定义属性以过滤magento产品集合中的内容

[英]Add custom attribute to filter in product collection in magento

我需要添加自定义属性以过滤产品集合

我尝试了以下代码,但过滤器无法正常工作

$_productCollections=$this->getLoadedProductCollection();
$_productCollection = $_productCollections->addAttributeToFilter('weight', array('lt' => 100));

下面的代码正在工作

$collection = Mage::getModel('catalog/product')->getCollection();
$_productCollection = $collection->addAttributeToFilter('weight', array('lt' => 100));

我需要适当的方法为权重属性添加过滤范围,例如

->addAttributeToFilter('weight', array('lt' => 100));

在默认产品集合中( $this->getLoadedProductCollection();

对于这个问题,我有一个解决方案

第1步:

列表的重复块代码

\app\code\core\Mage\Catalog\Block\Product\List.php to 
\app\code\local\Mage\Catalog\Block\Product\List.php

第2步:

从以下代码更改

$this->_productCollection = $layer->getProductCollection();

$this->_productCollection = $layer->getProductCollection()->addAttributeToFilter('weight', array('lt' => 100));

这是一种可以正确解决问题的简单解决方案。

尝试通过以下方式显式选择属性:

$_productCollection->addAttributeToSelect('weight');

然后进行过滤:

$_productCollections->addAttributeToFilter('weight', array('lt' => 100));

另一种方法是在产品集合中默认选择属性,方法是将其放置在模块的config.xml中:

<config>
<frontend>
     <product>
          <collection>
               <attributes>
                  <weight />
               </attributes>
          </collection>
      </product>
</frontend>

然后,您应该能够照常进行过滤:

$_productCollections->addAttributeToFilter('weight', array('lt' => 100));

也许,收藏已经加载。 加载收藏之前添加过滤器

($this->_getProductCollection()->load())

暂无
暂无

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

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