Magento - 在分组产品表中显示自定义属性

Magento - Show Custom Attributes in Grouped Product table(Magento - 在分组产品表中显示自定义属性)
本文介绍了Magento - 在分组产品表中显示自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要找到一种方法来显示自定义属性的值,而不是下图中显示的产品名称".


(来源: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>

任何帮助将不胜感激.

所以答案很简单.你看我上面没有提到的是 确实有输出......但它只是一个数字(例如:52).原来这是该自定义属性值的 ID(这是自定义属性的下拉类型).

总结一下
这适用于文本类型的自定义属性:

echo $this->htmlEscape($_item->getYearmade())

但是对于所有其他类型的自定义属性(我认为),应该使用以下内容:

echo $this->htmlEscape($_item->getAttributeText('yearmade'))

如果没有下面 Alan Storm 提供的最出色的答案,我就不会发现这一点.谢谢楼主.

解决方案

所有 Magento 模型都有一个可用的getData"方法,该方法将返回一个键/值对的 php 数组.在 grouped.phtml 文件的顶部尝试这个(在 $_product 被定义之后)

print('

');print_r($_product->getData());print('

');

您应该会看到类似于以下内容的输出.

数组([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 模型上,您可以通过调用基于名称的驼峰大小写版本的方法来访问数据数组中的任何属性,

$created_at = $_product->getCreatedAt();$_product->setCreatedAt($date);

因此,无论您的自定义属性名称是什么,您都应该能够使用上面的方法获得它,如果您不确定是否只是 print_r 或 var_dump 由 getData() 返回的数组的内容.

最后,如果自定义属性位于相关产品之一的简单产品上,您将需要更多类似的东西

$_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();

这篇关于Magento - 在分组产品表中显示自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)