skip to Main Content

i’m taking over a project and saw that the previous developer added a custom related products association. So he implemented a function to get the associated collection looking like this

/**
 * Retrieve collection CustomRelated product
 *
 * @return Mage_Catalog_Model_Resource_Product_Link_Product_Collection
 */
public function getCustomRelatedProductCollection()
{
    $collection = $this->getLinkInstance()->useCustomRelatedLinks()
        ->getProductCollection()
        ->setIsStrongMode();
    $collection->setProduct($this);
    return $collection;
}

Then in phtml file, he’s calling it out like this

$upsell_products = $_product->getCustomRelatedProductCollection();

And then he uses that collection in a foreach, and each element in the collection is using model ‘catalog/product’, but somehow it’s not loading enough attributes like prices and name

It will load all of the attributes only when i call load function again like this

Mage::getModel('catalog/product')->load($p->getId())

Which i don’t want to do because it’s pointless to reload the model, i’m still new to Magento so i’m not sure how to make the get collection above to fully load the product model, any ideas?

2

Answers


  1. You can load require attributes (name, price) like below.

    public function getCustomRelatedProductCollection()
    {
        $collection = $this->getLinkInstance()->useCustomRelatedLinks()
            ->getProductCollection()
            ->addAttributeToSelect(array("name", "price")) 
            ->setIsStrongMode();
        $collection->setProduct($this);
        return $collection;
    }
    
    Login or Signup to reply.
  2. //I have added new line in your code. please check now.

    public function getCustomRelatedProductCollection()
    {
        $collection = $this->getLinkInstance()->useCustomRelatedLinks()
            ->getProductCollection()
            ->setIsStrongMode();
        $collection->setProduct($this);
        $collection->addAttributeToSelect('*'); //New line added by me.
        return $collection;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search