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的新手,所以我不确定如何使上面的get集合完全加载产品模型,有什么想法吗?

您可以加载所需的属性(名称、价格(,如下所示。

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

//我在您的代码中添加了新行。 请立即检查。

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

最新更新