0

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).

1 Answer 1

0

In order to get this working, I am doing a foreach over each product in the compare list, fetching their related products, and adding each related product to the final collection:

protected function _prepareData() {
    $_products = $this->getLayout()->getBlockSingleton('Mage_Catalog_Block_Product_Compare_List')->getItems();

    foreach($_products as $_product) {
        $collection = $_product->getRelatedProductCollection()
            ->addAttributeToSelect('required_options')
            ->setPositionOrder()
            ->addStoreFilter();

        if(is_null($this->_itemCollection)) {
            $this->_itemCollection = $collection;
            continue;
        }

        foreach($collection as $item) {
            if(!$this->_itemCollection->getItemById($item->getId())) {
                $this->_itemCollection->addItem($item);
            }
        }
    }

    $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;
}

Not ideal as each loop performs another database call...

Still wanting a solution based on the OP.

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.