在magento的主页上禁用带有占位符图像的产品 [英] Disable products with placeholder image on home page in magento
问题描述
我使用随机代码在我们的magento商店的主页上显示不同类别的产品.这完美地工作.现在,我想排除所有仅具有占位符图像的产品,使其不显示在主页上.我用以下代码尝试过:
I use a random code to display products from different categories on the home page on our magento powered shop. This works perfectly. Now I would like to exclude all the products which only have a placeholder image from showing up on the home page. I tried it with the following code:
class Mage_Catalog_Block_Product_List_Random extends Mage_Catalog_Block_Product_List
{
protected function _getProductCollection()
{
if (is_null($this->_productCollection)) {
$categoryID = $this->getCategoryId();
if($categoryID)
{
$category = new Mage_Catalog_Model_Category();
$category->load($categoryID); // this is category id
$collection = $category->getProductCollection();
} else
{
$collection = Mage::getResourceModel('catalog/product_collection');
}
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
$collection->getSelect()->order('rand()');
$collection->addStoreFilter();
$numProducts = $this->getNumProducts() ? $this->getNumProducts() : 3;
$collection->setPage(1, $numProducts)->load();
$collection->addAttributeToFilter(
array('attribute' => 'small_image', 'eq' => ''),
array('attribute' => 'small_image', 'eq' => 'no_selection')
);
$this->_productCollection = $collection;
}
return $this->_productCollection;
}
}
但这不起作用,只有占位符图像的产品仍然会显示.
But this doesn't work and the products with only the placeholder image do still show up.
任何帮助将不胜感激.
谢谢,丹尼尔
推荐答案
您要在已加载收藏集 之后添加"small_image"过滤器,因此您的过滤器不会不再影响收藏了.
You're adding the 'small_image' filter after the collection already has been loaded, so your filter will not affect the collection anymore.
除此之外,您的 OR 过滤器在我看来很奇怪.假设"no_selection"也是一些占位符图像,那么您的过滤器会接受占位符图像,我认为您想在其中拒绝.
Other than that your OR filter looks odd to me. Assumed 'no_selection' is some placeholder image too, then your filter does accept placeholder images, where I think you want to decline them.
尝试改用AND过滤器:
Try to use an AND filter instead:
$collection->addAttributeToFilter(
array('attribute' => 'small_image', 'neq' => '')
);
$collection->addAttributeToFilter(
array('attribute' => 'small_image', 'neq' => 'no_selection')
);
这篇关于在magento的主页上禁用带有占位符图像的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!