在“描述”选项卡中显示产品的所有属性(Magento 1.9.2.4)

时间:2017-01-04 17:54:52

标签: php magento magento-1.9

Magento版本:1.9.2.4 我有文件/app/design/frontend/MY_THEME_NAME/default/template/catalog/product/view.phtml

代码:

<?php if ($detailedInfoGroup = $this->getChildGroup('detailed_info', 'getChildHtml')):?>
    <!-- fix full size backround start-->
    </div>
    </div>
    </div>
    <!-- fix full size backround start-->
<div class="specific">
    <div class="container">
    <div class="row">
        <div class="col-sm-12">
            <div class="specific__tab">
                <div class="tabs">
                    <ul class="tabs-ctrl">
                        <?php
                            reset($detailedInfoGroup);
                            $first_key = key($detailedInfoGroup);
                        ?>
                        <?php foreach ($detailedInfoGroup as $alias => $html):?>
                        <li class="tabs-ctrl-item <?php  if($alias == $first_key): ?>active<?php endif; ?>" data-class="<?php echo $alias?>"><?php echo $this->escapeHtml($this->getChildData($alias, 'title')) ?></li>
                        <?php endforeach;?>
                    </ul>
                    <ul class="tabs-list">
                        <?php foreach ($detailedInfoGroup as $alias => $html):?>
                        <li class="tabs-list-item list-<?php echo $alias?> <?php  if($alias == $first_key): ?>active<?php endif; ?>">
                            <?php echo $html ?>
                        </li>
                        <?php endforeach;?>


                    </ul>
                </div>
            </div><!-- specific__tab-->
        </div>
    </div>
    </div>
</div><!-- specific-->
    <!-- fix full size backround end-->
    <div class="container">
    <div class="row">
    <div class="col-xs-12">
    <!-- fix full size backround end-->
<?php endif; ?>

显示说明和附加标签。

在档案/app/design/frontend/MY_THEME_NAME/default/template/catalog/product/view/description.phtml

我有以下代码:

<?php $_description = $this->getProduct()->getDescription(); ?>
<?php if ($_description): ?>
    <h2><?php echo $this->__('Details') ?></h2>
    <div class="std">
        <?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_description, 'description') ?>
    </div>
<?php endif; ?>

在文件/app/design/frontend/MY_THEME_NAME/default/template/catalog/product/view/attributes.phtml

我有以下代码:

<?php
    $_helper = $this->helper('catalog/output');
    $_product = $this->getProduct()
?>
<?php if($_additional = $this->getAdditionalData()): ?>
    <h2><?php echo $this->__('Additional Information') ?></h2>
    <table class="data-table" id="product-attribute-specs-table">
        <col width="25%" />
        <col />
        <tbody>
        <?php foreach ($_additional as $_data): ?>
            <tr>
                <th class="label"><?php echo $this->escapeHtml($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')</script>
<?php endif;?>

如果我只是将此代码复制并粘贴到description.phtml - 这不起作用,$ _additional为空或不存在。

那么,我如何在描述选项卡上显示所有属性?我在这里阅读了很多主题但仍未找到解决方案。非常感谢!

2 个答案:

答案 0 :(得分:1)

找到问题的解决方案:

创建文件/app/code/local/Mage/Catalog/Block/Product/View/Description.php

使用followind代码:

class Mage_Catalog_Block_Product_View_Description extends Mage_Core_Block_Template
{
    protected $_product = null;

    function getProduct()
    {
        if (!$this->_product) {
            $this->_product = Mage::registry('product');
        }
        return $this->_product;
    }

    /**
     * $excludeAttr is optional array of attribute codes to
     * exclude them from additional data array
     *
     * @param array $excludeAttr
     * @return array
     */
    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);

                if (!$product->hasData($attribute->getAttributeCode())) {
                    $value = Mage::helper('catalog')->__('N/A');
                } elseif ((string)$value == '') {
                    $value = Mage::helper('catalog')->__('No');
                } elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
                    $value = Mage::app()->getStore()->convertPrice($value, true);
                }

                if (is_string($value) && strlen($value)) {
                    $data[$attribute->getAttributeCode()] = array(
                        'label' => $attribute->getStoreLabel(),
                        'value' => $value,
                        'code'  => $attribute->getAttributeCode()
                    );
                }
            }
        }
        return $data;
    }

}

现在我可以使用$ _additional = $ this-&gt; getAdditionalData();在描述选项卡中。

答案 1 :(得分:0)

这适用于tabs.phtml Magento ver。 1.9.3.4

$_product = Mage::registry('current_product');