Php AddAttributeToFilter未在Magento中加载特定产品
我在app/code/local/myown/layoutmods/catalog/block/product/list中编写了Manufacturer.php文件/ 有功能。现在,当我尝试使用它时,它会加载所有产品。我检查了一下,那个文件被调用了,但它不会被制造商过滤。谢谢大家的建议Php AddAttributeToFilter未在Magento中加载特定产品,php,magento,Php,Magento,我在app/code/local/myown/layoutmods/catalog/block/product/list中编写了Manufacturer.php文件/ 有功能。现在,当我尝试使用它时,它会加载所有产品。我检查了一下,那个文件被调用了,但它不会被制造商过滤。谢谢大家的建议 protected function _getProductCollection() { if (is_null($this->_productCollection)) {
protected function _getProductCollection() {
if (is_null($this->_productCollection)) {
// Get attribute value id:
$manufacturer_name = Mage::registry( 'current_category' )->getName();
$productModel = Mage::getModel('catalog/product');
$attr = $productModel->getResource()->getAttribute('manufacturer');
if ($attr->usesSource()) {
$manufacturer_id = $attr->getSource()->getOptionId($manufacturer_name);
}
// load and filter product collection
$collection = Mage::getModel('catalog/product')->getCollection();
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
$collection->addStoreFilter();
$this->_productCollection = $collection;
$this->_productCollection->addAttributeToSelect( 'manufacturer' )->addAttributeToFilter( 'manufacturer', array( 'eq' => $manufacturer_id ) );
}
return $this->_productCollection;
}
据我所知,问题不在products集合或过滤器中,而是在$manufacturer\u id参数中 如果注释掉//Get attribute value id:和//load and filter product collection comments之间的代码,然后用数据库中的一个id替换$manufacturer\u id,那么它将起作用。我也不得不发表评论
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
在我的例子中,这条线阻止了任何东西进入收藏,所以我不确定它到底是做什么的
我看到的第一个问题是$manufacturer\u id不一定总是被设置,因为
if ($attr->usesSource()) {
我无法让代码运行,因为测试时Mage::registry('current_category')不包含值。我建议您重新测试用于检索制造商id的代码。我尝试在此行末尾添加->加载(),但它阻止了服务器$this->\u productCollection->addAttributeToSelect('manufacturer')->addAttributeToFilter('manufacturer',array('eq'=>$manufacturer\u id));