![](/img/trans.png)
[英]Get magento header description in schema Product description in view.phtml
[英]Magento - Get product items attribute in view.phtml
我正在嘗試在catalog / category / view.phtml中加載自定義類別來存檔我使用的:
<?php
$_category = Mage::getModel('catalog/category')->load(47);
$_productCollection = $_category->getProductCollection();
if($_productCollection->count()) {
foreach( $_productCollection as $_product ):
echo $_product->getProductUrl();
echo $this->getPriceHtml($_product, true);
echo $this->htmlEscape($_product->getName());
endforeach;
}
?>
我可以加載URL,例如,現在我想加載一個自定義屬性,例如顏色:
$_product->getResource()->getAttribute('color')->getFrontend()->getValue($_product)
此代碼不起作用,我100%確定顏色屬性設置為在類別列表中顯示,並且此類別中的項目已填寫此字段。 我知道這個,因為這段代碼適用於list.html 。
我做錯了什么? 我正在使用1.7.0.2。
預期的結果是顯示來自自定義類別的所有COLOR屬性
目錄/類別/ view.phtml
我簡直不敢相信我找到了答案。 因為我們不在常規類別列表中,所以我們需要將自定義屬性添加到集合中。
這是代碼:
$_productCollection = $_category->getProductCollection()
->addAttributeToSelect('color');
如果平面表中的“顏色”你應該能夠
$_product->getColor();
如果此屬性不在集合中,您可以通過使屬性可過濾將其添加到平面表中,將其添加到PHP集合調用中
$_productCollection = $_category->getProductCollection()
->addAttributeToSelect('color');
或者加載產品模型以獲取所有屬性
$_product = Mage::getModel('catalog/product')->load($_product->getId());
echo $_product->getColor();
在listing / frontend中顯示此屬性
運行重建索引
foreach( $_productCollection as $_product ): echo $_product->getProductUrl();
在此代碼中var_dump $_product->getData();
檢查此var_dump
導致顯示該特定屬性值。
注意:
$_product->getResource()->getAttribute('color')->getFrontend()->getValue($_product)
不是一種非常有效的通話方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.