I've been playing with some code that will display random products from the same category in the related products block.
The code works, however I would only like to display products that contain images. There are two ways I can do this, by either specifying a filter for images or I have an attribute that has a 0 or 1 value. 1 meaning it contains an image.
Here is the entire code:
{
public function getRelatedProducts($limit = false) {
$products = $this->getData('related_products');
if (!$products) {
$product = Mage::registry('current_product');
if ($category = Mage::registry('current_category')) {
} elseif ($product) {
$ids = $product->getCategoryIds();
if (!empty($ids)) {
$category = Mage::getModel('catalog/category')->load($ids[0]);
}
}
if ($category) {
if ($limit === false) {
$limit = Mage::getStoreConfig('autorelated/general/limit');
}
$products = Mage::getResourceModel('reports/product_collection')
->addAttributeToFilter('visibility', array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
))
->addAttributeToFilter('status', 1)
->addCategoryFilter($category)
->addAttributeToSelect('*')
->setPageSize($limit);
if ($product) {
$products->addAttributeToFilter('entity_id', array(
'neq' => Mage::registry('current_product')->getId())
);
}
$products->getSelect()->order(new Zend_Db_Expr('RAND()'));
Mage::getModel('cataloginventory/stock')->addInStockFilterToCollection($products);
$this->setData('related_products', $products);
} else {
return false;
}
}
return $products;
}
}
Now, here is the section I've been focusing on:
$products = Mage::getResourceModel('reports/product_collection')
->addAttributeToFilter('visibility', array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
))
->addAttributeToFilter('status', 1)
->addCategoryFilter($category)
->addAttributeToSelect('*')
->setPageSize($limit);
if ($product) {
$products->addAttributeToFilter('entity_id', array(
'neq' => Mage::registry('current_product')->getId())
);
}
I've tried the following
->addAttributeToFilter('image_updated',array('eq' => 1))
and
->addAttributeToFilter('image', array('neq' => 'no_selection'))
directly bellow:
*->addAttributeToFilter('status', 1)*
But products without images are still appearing in the random related products.
I believe it may be because the loaded resource model doesn't contain the attributes I'm looking to filter.
Can someone see what I am missing to use either piece of code to filter out products that do not contain images?