Javascript 如何在Magento产品页属性选项卡上显示产品属性组名称?
我在前端对产品属性进行了一些编码,并在主题上方显示它们的组名称,如下所示:Javascript 如何在Magento产品页属性选项卡上显示产品属性组名称?,javascript,html,magento,php,Javascript,Html,Magento,Php,我在前端对产品属性进行了一些编码,并在主题上方显示它们的组名称,如下所示: 第1组 属性1 属性2 第2组 属性3 属性4 我用以下代码添加了/app/code/local/Mage/Catalog/Block/Product/View/Attributesgroups.php: <?php class Mage_Catalog_Block_Product_View_Attributesgroups extends Mage_Core_Block_Template {
- 第1组
- 属性1
- 属性2
- 第2组
- 属性3
- 属性4
<?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;
}
}
首先尝试简单的方法
foreach($product->getAttributes() as $att){
$group_id = $att->getData('attribute_group_id');
$group = Mage::getModel('eav/entity_attribute_group')->load($group_id); var_dump($group);
}
或者请尝试使用下面的代码
以编程方式获取属性集ID
$sDefaultAttributeSetId = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)
->getDefaultAttributeSetId();
$attributeSetId = 10;
$groups = Mage::getModel('eav/entity_attribute_group')
->getResourceCollection()
->setAttributeSetFilter($attributeSetId)
->setSortOrder()
->load();
$attributeCodes = array();
foreach ($groups as $group) {
echo $groupName = $group->getAttributeGroupName();
$groupId = $group->getAttributeGroupId();
}
以编程方式获取组名
$sDefaultAttributeSetId = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)
->getDefaultAttributeSetId();
$attributeSetId = 10;
$groups = Mage::getModel('eav/entity_attribute_group')
->getResourceCollection()
->setAttributeSetFilter($attributeSetId)
->setSortOrder()
->load();
$attributeCodes = array();
foreach ($groups as $group) {
echo $groupName = $group->getAttributeGroupName();
$groupId = $group->getAttributeGroupId();
}
我是马根托的新手。你能详细解释一下吗。我的意思是在哪里放置这些代码或者如何更改我的代码?感谢您必须在产品详细信息/列表页面(.phtml)中编写代码。您的意思是在attributesgroups.phtml
中吗?如果是的话,你能给我和其他像我这样正在编码的人一个现成的代码吗?谢谢你想要在那里显示组名的地方写代码…例如你需要在产品详细信息页面中显示组名,写代码(catalog->product->view.phtml)。我想要在产品页面的产品属性选项卡中显示它们
$attributeSetId = 10;
$groups = Mage::getModel('eav/entity_attribute_group')
->getResourceCollection()
->setAttributeSetFilter($attributeSetId)
->setSortOrder()
->load();
$attributeCodes = array();
foreach ($groups as $group) {
echo $groupName = $group->getAttributeGroupName();
$groupId = $group->getAttributeGroupId();
}