I want to add the related products block to the compare list, however I am getting a fatal error Call to a member function getRelatedProductCollection() on a non-object
.
I suspect this is because I am trying to view related products on a non-product page, therefore Mage::registry('product')
is null.
How do I add related products for all products in my compare list in one block?
[edit]
I have decided to extend the related products block and create the collection myself.
Doing a var_dump on $this->_itemCollection->count()
is giving me 0.
protected function _prepareData() {
//compare products list
$_products = $this->getLayout()->getBlockSingleton('Mage_Catalog_Block_Product_Compare_List')->getItems();
//get related products for all products in compare list
//more or less copied from @see Mage_Catalog_Block_Product_List_Related::_prepareData()
$this->_itemCollection = Mage::getModel('catalog/product')->getLinkInstance()
->useRelatedLinks()
->setGroupBy()
->getProductCollection()
->setIsStrongMode()
->addAttributeToSelect('required_options')
->setPositionOrder()
->addStoreFilter()
->addProductFilter($_products)
;
var_dump($this->_itemCollection->count());die; //returns 0
$this->_addProductAttributesAndPrices($this->_itemCollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_itemCollection);
$this->_itemCollection->load();
foreach ($this->_itemCollection as $product) {
$product->setDoNotUseCategoryId(true);
}
return $this;
}
By trial and error, when I remove setIsStrongMode
and addProductFilter
the collection count returns my total product count.
For testing purposes, only one of my products has related products so I know the expected output should be '3' (this product has three related products).