random products instead of related products in magento

Posted by abnab on Stack Overflow See other posts from Stack Overflow or by abnab
Published on 2012-03-22T23:25:35Z Indexed on 2012/03/22 23:29 UTC
Read the original article Hit count: 314

Filed under:
|

BElow is the function that selects related product of a single product. I wanted it to work in such a way that if there are no related products other random products will be added to the array. Random could be other products of the same category and if there is no procut in the same category we could fetch from other categories.

    protected function _prepareData()
    {
        $product = Mage::registry('product');
        /* @var $product Mage_Catalog_Model_Product */

        $this->_itemCollection = $product->getRelatedProductCollection()
            ->addAttributeToSelect('required_options')
            ->addAttributeToSort('position', Varien_Db_Select::SQL_ASC)
            ->addStoreFilter()
        ;

        if (Mage::helper('catalog')->isModuleEnabled('Mage_Checkout')) {
            Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($this->_itemCollection,
                Mage::getSingleton('checkout/session')->getQuoteId()
            );
            $this->_addProductAttributesAndPrices($this->_itemCollection);
        }
//        Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this->_itemCollection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_itemCollection);

        $this->_itemCollection->load();

        foreach ($this->_itemCollection as $product) {
            $product->setDoNotUseCategoryId(true);
        }

        return $this;
    }

Thanks Abnab

© Stack Overflow or respective owner

Related posts about php

Related posts about magento