2017-08-22 108 views
0

我接管了一個項目,並看到之前的開發人員添加了一個自定義相關產品關聯。於是,他實現了一個函數來獲取相關的集合看起來像這樣Magento獲取沒有加載所有屬性的相關產品

/** 
* 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; 
} 

然後在PHTML文件,他叫出來這樣

$upsell_products = $_product->getCustomRelatedProductCollection(); 

然後,他使用該集合中的foreach,每個元素在使用模型'目錄/產品',但不知何故它沒有加載足夠的屬性,如價格和名稱

它將加載所有的屬性,只有當我再次調用加載函數像這樣

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

我不想這樣做,因爲重新加載模型沒有意義,我還是Magento的新手,所以我不確定如何使上面的獲取集合完全加載產品模型,任何想法?

+0

你想獲得相關產品嗎?或者你想獲得相關產品?哪一個? – Nickool

+0

@Nickool你能幫忙解釋2之間的區別嗎?之前的開發人員實現了產品之間的自定義關聯,因此我不知道它是否應該被稱爲相關或關聯,例如,如果您購買此產品,您也會喜歡這樣的產品 –

回答

2

您可以像下面那樣加載require屬性(名稱,價格)。

public function getCustomRelatedProductCollection() 
{ 
    $collection = $this->getLinkInstance()->useCustomRelatedLinks() 
     ->getProductCollection() 
     ->addAttributeToSelect(array("name", "price")) 
     ->setIsStrongMode(); 
    $collection->setProduct($this); 
    return $collection; 
} 
+0

謝謝,此作品,但與Jitendra的答案相同我只能標記1答案作爲解決方案 –

+1

它的確定。沒有任何問題。只是爲了您的信息,這是一種僅以所需屬性加載收集的最佳方式。如果您選擇使用'*',則它是具有所有屬性的裝載收集。所以如果我們在數據庫中有很多屬性,就會出現查詢性能問題。 –

1

//我在代碼中添加了新行。請立即檢查。

public function getCustomRelatedProductCollection() 
{ 
    $collection = $this->getLinkInstance()->useCustomRelatedLinks() 
     ->getProductCollection() 
     ->setIsStrongMode(); 
    $collection->setProduct($this); 
    $collection->addAttributeToSelect('*'); //New line added by me. 
    return $collection; 
} 
+0

這工作,謝謝!爲什麼不默認「選擇*」壽? –

相關問題