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.
我需要找到一种方法来显示自定义属性的值,而不是下图中显示的“产品名称”。
Grouped products table http://www.magentocommerce.com/images/uploads/grouped_prod_front.gif
分组产品表http://www.magentocommerce.com/images/uploads/grouped_prod_front.gif
I'm working with /app/design/frontend/default/defaultx/template/catalog/product/view/type/grouped.php
我正在使用/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).
编辑:所以答案结果很简单。你看到我上面没有提到的是确实有输出......但它只是一个数字(例如:52)。原来这是该自定义属性值的ID(它是自定义属性的Dropdown类型)。
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.
如果没有Alan Storm提供的最优秀答案,我就不会发现这一点。谢谢你,先生。
4 个解决方案
#1
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)
所有Magento模型都有一个“getData”方法,它将返回一个关键/值对的php数组。在groups.phtml文件的顶部试试这个(在定义$ _product之后)
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,
因此,您可以获取一系列属性,然后拔出密钥。你也可以使用Magento的方便/魔术getX和setX方法。在所有Magento模型上,您可以通过调用基于名称的驼峰案例版本的方法来访问数据数组中的任何属性,
$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().
因此,无论您的自定义属性名称是什么,您都应该能够使用上面的内容获取它,如果您不确定print_r或var_dump是否由getData()返回的数组内容。
Finally, if the custom attribute is on one of the related products simple product, you'll wants something more like
最后,如果自定义属性是关于其中一个相关产品的简单产品,那么您将需要更多类似的东西
$_associatedProducts[0]->getCreatedAt();
#2
I had the same problem.
我有同样的问题。
-
You must locate grouped.phtml
app/design/frontend/base/default/template/catalog/product/view/type/grouped.phtml
您必须找到grouped.phtml app / design / frontend / base / default / template / catalog / product / view / type / grouped.phtml
-
Get the item, example
$_item[units]
获取项目,例如$ _item [units]
-
Add a cell into the table and paste echo
$_item['units'];
在表格中添加一个单元格并粘贴echo $ _item ['units'];
-
Thats all :)
就这样 :)
#3
From ver. 1.3. and in 1.4 too you must use $_item not $_product, for me this works fine in groupable table too.
来自ver。 1.3。而且在1.4中你也必须使用$ _item而不是$ _product,对我来说这也适用于可分组表。
Example:
<?php echo $_item->getAttributeText('your attribute'); ?>
#4
There are plugins that can solve your solution really easily. You can find the plugin here that takes product options and displays them in a grid: http://www.magemechanics.com/product-grid-options.html
有些插件可以很容易地解决您的解决方案。您可以在此处找到带有产品选项的插件,并将其显示在网格中:http://www.magemechanics.com/product-grid-options.html
#1
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)
所有Magento模型都有一个“getData”方法,它将返回一个关键/值对的php数组。在groups.phtml文件的顶部试试这个(在定义$ _product之后)
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,
因此,您可以获取一系列属性,然后拔出密钥。你也可以使用Magento的方便/魔术getX和setX方法。在所有Magento模型上,您可以通过调用基于名称的驼峰案例版本的方法来访问数据数组中的任何属性,
$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().
因此,无论您的自定义属性名称是什么,您都应该能够使用上面的内容获取它,如果您不确定print_r或var_dump是否由getData()返回的数组内容。
Finally, if the custom attribute is on one of the related products simple product, you'll wants something more like
最后,如果自定义属性是关于其中一个相关产品的简单产品,那么您将需要更多类似的东西
$_associatedProducts[0]->getCreatedAt();
#2
I had the same problem.
我有同样的问题。
-
You must locate grouped.phtml
app/design/frontend/base/default/template/catalog/product/view/type/grouped.phtml
您必须找到grouped.phtml app / design / frontend / base / default / template / catalog / product / view / type / grouped.phtml
-
Get the item, example
$_item[units]
获取项目,例如$ _item [units]
-
Add a cell into the table and paste echo
$_item['units'];
在表格中添加一个单元格并粘贴echo $ _item ['units'];
-
Thats all :)
就这样 :)
#3
From ver. 1.3. and in 1.4 too you must use $_item not $_product, for me this works fine in groupable table too.
来自ver。 1.3。而且在1.4中你也必须使用$ _item而不是$ _product,对我来说这也适用于可分组表。
Example:
<?php echo $_item->getAttributeText('your attribute'); ?>
#4
There are plugins that can solve your solution really easily. You can find the plugin here that takes product options and displays them in a grid: http://www.magemechanics.com/product-grid-options.html
有些插件可以很容易地解决您的解决方案。您可以在此处找到带有产品选项的插件,并将其显示在网格中:http://www.magemechanics.com/product-grid-options.html