問(wèn)題描述
我需要找到一種方法來(lái)顯示自定義屬性的值,而不是下圖中顯示的產(chǎn)品名稱(chēng)".
(來(lái)源:magentocommerce.com) >
我正在使用/app/design/frontend/default/defaultx/template/catalog/product/view/type/grouped.php
下面的代碼不起作用(自定義屬性是 yearmade):
<?php foreach ($_relatedProducts as $_item): ?><tr><td><?php echo $this->htmlEscape($_item->getYearmade()) ?></td>
任何幫助將不勝感激.
所以答案很簡(jiǎn)單.你看我上面沒(méi)有提到的是 確實(shí)有輸出......但它只是一個(gè)數(shù)字(例如:52).原來(lái)這是該自定義屬性值的 ID(這是自定義屬性的下拉類(lèi)型).
總結(jié)一下
這適用于文本類(lèi)型的自定義屬性:
echo $this->htmlEscape($_item->getYearmade())
但是對(duì)于所有其他類(lèi)型的自定義屬性(我認(rèn)為),應(yīng)該使用以下內(nèi)容:
echo $this->htmlEscape($_item->getAttributeText('yearmade'))
如果沒(méi)有下面 Alan Storm 提供的最出色的答案,我就不會(huì)發(fā)現(xiàn)這一點(diǎn).謝謝樓主.
所有 Magento 模型都有一個(gè)可用的getData"方法,該方法將返回一個(gè)鍵/值對(duì)的 php 數(shù)組.在 grouped.phtml 文件的頂部嘗試這個(gè)(在 $_product 被定義之后)
print('');print_r($_product->getData());print('
');
您應(yīng)該會(huì)看到類(lèi)似于以下內(nèi)容的輸出.
數(shù)組([store_id] =>1[entity_id] =>3437[entity_type_id] =>4[attribute_set_id] =>27[type_id] =>分組[sku] =>[category_ids] =>[created_at] =>2009-04-16 03:37:51...
因此,您可以獲取一組屬性,然后將密鑰拉出.您還可以使用 Magento 的方便/神奇的 getX 和 setX 方法.在所有 Magento 模型上,您可以通過(guò)調(diào)用基于名稱(chēng)的駝峰大小寫(xiě)版本的方法來(lái)訪問(wèn)數(shù)據(jù)數(shù)組中的任何屬性,
$created_at = $_product->getCreatedAt();$_product->setCreatedAt($date);
因此,無(wú)論您的自定義屬性名稱(chēng)是什么,您都應(yīng)該能夠使用上面的方法獲得它,如果您不確定是否只是 print_r 或 var_dump 由 getData() 返回的數(shù)組的內(nèi)容.
最后,如果自定義屬性位于相關(guān)產(chǎn)品之一的簡(jiǎn)單產(chǎn)品上,您將需要更多類(lèi)似的東西
$_relatedProducts[0]->getCreatedAt();
I need to find a way to show the value of a custom attribute in place of the "Product Name" shown in the image below.
(source: magentocommerce.com)
I'm working with /app/design/frontend/default/defaultx/template/catalog/product/view/type/grouped.php
The code below doesn't work(the custom attribute is yearmade):
<?php if (count($_associatedProducts)): ?>
<?php foreach ($_associatedProducts as $_item): ?>
<tr>
<td><?php echo $this->htmlEscape($_item->getYearmade()) ?></td>
Any help would be appreciated.
EDIT: So the answer turned out to be quite simple. You see what I failed to mention above was that there was indeed output... but that it was just a number (eg: 52). Turns out this was the ID for that custom attribute value (It was a Dropdown type of custom attribute).
So in summary
This works for custom attributes of type text:
echo $this->htmlEscape($_item->getYearmade())
But for all other types of custom attribute (I think), the following should be used:
echo $this->htmlEscape($_item->getAttributeText('yearmade'))
I would not have discovered this without the most excellent answer provided by Alan Storm, below. Thank you sir.
All Magento models have a "getData" method available, which will return an php-array of key/value pairs. Try this at the top of your grouped.phtml file (after $_product is defined)
print('<pre>');print_r($_product->getData());print('</pre>');
You should see output that looks something like the following.
Array
(
[store_id] => 1
[entity_id] => 3437
[entity_type_id] => 4
[attribute_set_id] => 27
[type_id] => grouped
[sku] =>
[category_ids] =>
[created_at] => 2009-04-16 03:37:51
...
So, you can grab an array of properties and just pull the key out. You could also use Magento's convenience/magic getX and setX methods. On all Magento models, you can access any property in the data array by calling a method based on the camel case version of the name,
$created_at = $_product->getCreatedAt();
$_product->setCreatedAt($date);
So, whatever your custom attribute name is, you should be able to get at it using the above, and if you're not sure just print_r or var_dump the contents of the array returned by getData().
Finally, if the custom attribute is on one of the related products simple product, you'll wants something more like
$_associatedProducts[0]->getCreatedAt();
這篇關(guān)于Magento - 在分組產(chǎn)品表中顯示自定義屬性的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!