0

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?

1 Answer 1

1

The Mage_Reports product collection is not the right collection to use here. Its definition points to some manual joins and attribute selection which I suspect (and you surmise) is interfering with your image logic.

Switch to catalog/product_collection and see what happens.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.