隐藏Magento产品属性标签

时间:2015-05-09 13:34:35

标签: javascript php html magento

我做了一些编码,将产品属性分组到前端,并在主题上显示他们的群组名称,如下所示:

  • attgroup 1
    • 属性1
    • 属性2
    • ...
  • attgroup 2
    • 属性3
    • 属性4
    • ...

这是我的groupview.phtml(atributes.phtml替代)内容:

groupview.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++;
if($_additional['values_exist'] !== true) {
    continue; ?>
        <div class="attributesgroups-title">
            <h3 style="margin:0; color:white;"><?php echo $this->__( $_additional['title'] )?></h3>
            </div>
<?php } ?>
            <table style="border-radius: 0 0 2px 2px;" class="data-table" id="product-attribute-specs-table-<?php echo $i?>">
                <col width="25%" />
                <col />
                <tbody>
                <?php foreach ($_additional['items'] as $_data): ?>
                <?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
        if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
                    <tr>
                        <th class="label"><img src="<?php echo $this->getSkinUrl('images/attributes/'.$_data['code'].'.png')?>" alt="<?php echo $this->htmlEscape($this->__($_data['label'])) ?>" /><span class="separator">|</span><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
                        <td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
                    </tr>
                    <?php } ?>
                <?php endforeach; ?>
                </tbody>
            </table>
            <script type="text/javascript">decorateTable('product-attribute-specs-table-<?php echo $i?>')</script>
        <?php endforeach; ?>

    </div>
    <?php endif;?>

groupview.php:

    <?php 

class Webguys_AttributesAsGroup_Block_Groupview 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() && $attribute->getIsUserDefined() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
        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);
                    }

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

                    // If there has been a value set above, we know this group is worth showing.
                    if(strlen($data[$group]['items'][$attribute->getAttributeCode()]['value'])) {
                        // Set the group to record that it has values
                        $data[$group]['values_exist'] = true;
                    }
                    $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;
    }
}

我自定义了我的代码以隐藏此选项卡中带有空值的属性,并且还隐藏了他们的组标题。隐藏了具有空值的属性。但现在所有组标题都被隐藏。我想隐藏空洞的东西。

1 个答案:

答案 0 :(得分:0)

此模块完全符合您的要求。您遇到的同样问题是在我自己的网站上烦扰我,也可能使客户感到困惑。

Improved Attribute Display

Demo

实际上,它并没有完全按照您的意愿格式化列表,但它会删除空白和非相关性,而不会触及您的模板文件。此外,它也适用于产品比较屏幕。