【发布时间】:2013-10-17 19:43:46
【问题描述】:
我创建了 Attribute Sets,其中包含更多 Grouped 属性。在管理中,自定义属性显示在组中(选项卡),就像我创建它们一样。但是,在产品页面上,所有属性一起列出,在列出该组中的属性之前不显示属性组名称。
我怎样才能同时显示属性组名称,而不仅仅是属性?如果您可以在默认模板上显示,我将在我的自定义模板中相应地显示。
谢谢!
【问题讨论】:
标签: magento
我创建了 Attribute Sets,其中包含更多 Grouped 属性。在管理中,自定义属性显示在组中(选项卡),就像我创建它们一样。但是,在产品页面上,所有属性一起列出,在列出该组中的属性之前不显示属性组名称。
我怎样才能同时显示属性组名称,而不仅仅是属性?如果您可以在默认模板上显示,我将在我的自定义模板中相应地显示。
谢谢!
【问题讨论】:
标签: magento
好的,我找到了答案,我希望它对寻找相同内容的其他人有用。 首先,我使用的是 Magento 1.5.0。 其次,我找到了德语here的答案,已经创建了扩展,但安装失败。 因此,我使用以下代码添加了 /app/code/local/Mage/Catalog/Block/Product/View/Attributesgroups.php:
<?php
class Mage_Catalog_Block_Product_View_Attributesgroups extends Mage_Core_Block_Template
{
protected $_product = null;
function getProduct()
{
if (!$this->_product) {
$this->_product = Mage::registry('product');
}
return $this->_product;
}
public function getAdditionalData(array $excludeAttr = array())
{
$data = array();
$product = $this->getProduct();
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
$value = $attribute->getFrontend()->getValue($product);
// TODO this is temporary skipping eco taxes
if (is_string($value)) {
if (strlen($value) && $product->hasData($attribute->getAttributeCode())) {
if ($attribute->getFrontendInput() == 'price') {
$value = Mage::app()->getStore()->convertPrice($value,true);
} elseif (!$attribute->getIsHtmlAllowedOnFront()) {
$value = $this->htmlEscape($value);
}
$group = 0;
if( $tmp = $attribute->getData('attribute_group_id') ) {
$group = $tmp;
}
$data[$group]['items'][ $attribute->getAttributeCode()] = array(
'label' => $attribute->getFrontend()->getLabel(),
'value' => $value,
'code' => $attribute->getAttributeCode()
);
$data[$group]['attrid'] = $attribute->getId();
}
}
}
}
// Noch Titel lesen
foreach( $data AS $groupId => &$group ) {
$groupModel = Mage::getModel('eav/entity_attribute_group')->load( $groupId );
$group['title'] = $groupModel->getAttributeGroupName();
}
return $data;
}
}
然后,我创建了 /app/design/frontend/default/YOUR_TEMPLATE/template/catalog/product/view/attributesgroups.phtml 文件,其内容如下:
<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct()
?>
<?php if($_additionalgroup = $this->getAdditionalData()): ?>
<div class="box-collateral box-additional">
<h2><?php echo $this->__('Additional Information') ?></h2>
<?php $i=0; foreach ($_additionalgroup as $_additional): $i++; ?>
<h3><?php echo $this->__( $_additional['title'] )?></h3>
<table class="data-table" id="product-attribute-specs-table-<?php echo $i?>">
<col width="25%" />
<col />
<tbody>
<?php foreach ($_additional['items'] as $_data): ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table-<?php echo $i?>')</script>
<?php endforeach; ?>
</div>
<?php endif;?>
最后一步是在第223行修改/app/design/frontend/default/YOUR_TEMPLATE/layout/catalog.xml,并替换
<block type="catalog/product_view_attributes" name="product.attributes" as="additional" template="catalog/product/view/attributes.phtml">
与
<block type="catalog/product_view_attributesgroups" name="product.attributes" as="additional" template="catalog/product/view/attributesgroups.phtml">
我再说一遍,这个答案不属于我,我只是翻译了我发现的内容。 非常感谢漂亮的人们,他们以简洁明了的回答结束了我三天的搜索:WebGuys.DE 另外,感谢@rpSetzer 的帮助!
【讨论】:
遍历所有属性并创建一个组数组。在每个组中,您放置属于它的属性。然后以您想要的方式显示它们很简单。
Here 是一个非常接近您需要的实现。
【讨论】:
$attributes = $_product->getAttributes(); 时,attributes.phtml 会中断。如果有人有一个完整的工作示例,那就太好了,因为我认为其他没有 php 经验的人想知道如何做到这一点。
感谢翻译:为可能需要的人提供补充信息:
对于第一步,如果文件夹 /app/code/local/Mage/Catalog/Block/Product/View/ 不存在,请创建它!具有正确的权限并将文件 Attributesgroups.php 放在那里
如果您有不同的商店视图(针对不同的语言),并且您想为每个属性标签使用正确的翻译,您需要执行以下操作:
在文件 Attributesgroups.php
将'label' => $attribute->getFrontend()->getLabel(), 替换为'label' => $attribute->getStoreLabel(),
【讨论】: