如何在任何magento模板中隐藏空属性?


12

我想在magento模板中隐藏自定义属性。我的magento版本是1.8.1

我们为产品添加了自定义属性,例如品牌,尺寸,产品类型等,但是有时我们没有在这些属性中添加值。magento在产品查看页面上显示No或N / A。

因此,我们要隐藏模板中为空或无值的属性。


我们将需要更多的信息来提供帮助(哪个属性?在哪里?)
Benmarks 2014年

Answers:


7

快速修复:

进入app/[mypackage]/[mytheme]/template/catalog/product/view/attributes.phtml(或从基本主题或默认自定义主题复制此文件到主题中):

<?php foreach ($_additional as $_data):
// Add these 2 lines
$_test_data_value = trim($_data['value']);
if ((empty($_test_data_value) || in_array($_test_data_value, array(Mage::helper('catalog')->__('N/A'), Mage::helper('catalog')->__('No'))))) continue;?>

以下不是实现您所要求的内容的必要条件:

这些属性仍然被加载。要对此进行优化(如果属性集中有大量属性),请执行以下操作:

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)) {

            // Fix:
            //$value = $attribute->getFrontend()->getValue($product);

            if (!$product->hasData($attribute->getAttributeCode())) {
                $value = Mage::helper('catalog')->__('N/A');
            } 
            // Fix:
            elseif ((string) ($value = $attribute->getFrontend()->getValue($product)) == '') {
                $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;
}

请注意两个// Fix:注释。

此修改的函数来自Mage_Catalog_Block_Product_View_Attributes。您需要从模块中将上述函数复制到您的块类中。您的块类将重写核心块类。应用此功能将大大改善前端的产品视图页面加载。

如果您不知道如何在本地目录中创建自定义模块,则可以搜索有关如何创建Magento模块以及如何重写核心块类的教程。或尝试http://www.magentocommerce.com/magento-connect/ultimate-module-creator.html


第一个解决方案是更改模板文件,但是有两个问题。首先,如果属性类型为“是/否”且其值设置为“否”,它将被隐藏在前端中,这是不正常的。其次,如果没有属性,您将获得标题“附加信息”,这是不正确的。如果没有属性,则标题不应该出现。
ADDISON74 '17

6

查找并打开attributes.phtml文件。该文件可以在这里找到: /app/design/frontend/[YOUR PACKAGE]/[YOUR THEME]/template/catalog/product/view/attribute.phtml

打开文件并搜索以下行:

<?php foreach ($_additional 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; ?>

用以下代码行替换整个foreach循环:

<?php foreach ($_additional 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"><?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; ?>

来源:http//codingbasics.net/hide-magento-attributes-value/

来源:http//www.magthemes.com/magento-blog/empty-attributes-showing-na-fix/


4

我不知道确切的信息,但是我已经在某处阅读了。

通过仅编辑名为“ attributes.phtml”的模板文件来隐藏空属性。

在您的代码中,找到以下几行:

<?php foreach ($_additional 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; ?>

并将这些行替换为:

<?php foreach ($_additional as $_data): ?>
    <?php if ((string)$_data['value'] != '' and $_data['value'] != 'N/A'): ?>
        <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 endif; ?>
<?php endforeach; ?>

1
您的解决方案仅隐藏属性类型datetime,这是唯一具有N / A值的属性。文本字段,文本区域,多选,下拉列表没有值。如果attribyte类型为datetime,并且值设置为No,则应显示它而不是隐藏。
ADDISON74 '17

1

app / design / frontend / base / default / template / catalog / product / view / attributes.phtml中更改以下代码:

从:

<?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; ?>

至:

<?php foreach ($_additional as $_data): ?>
<?php if ($_product->getAttributeText($_data['code']) == '') continue; ?>
<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; ?>

2
不要更改基本模板...
Jelle Siderius's

1

在您的自定义主题中,导航到:catalog\product\view\attributes.phtml。您的PHP代码应检查所有语言的属性值是“否”还是“ N / A”。这将不会使用这些值来呈现属性。

该代码将如下所示:

<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct();
$emptyValues = array($this->__('N/A'), $this->__('No'));
?>
<?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): ?>
            <?php if(!in_array($_data['value'], $emptyValues)): ?>
                <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 endif; ?>
        <?php endforeach; ?>
        </tbody>
    </table>
    <script type="text/javascript">decorateTable('product-attribute-specs-table')</script>
<?php endif;?>

变量$emptyValues被添加,并且检查它是否在数组中已被添加到代码中。

对前端进行更改后,请确保清空缓存。


对我来说,上面的代码不起作用
Gem

1

只需一小段代码即可完成。查找并打开attributes.phtml文件。该文件可以在这里找到:/app/design/frontend/[theme name]/[package name]/template/catalog/product/view/attribute.phtml

打开文件并搜索以下行:

<?php foreach ($_additional 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; ?>

用以下代码行替换整个foreach循环:

<?php foreach ($_additional 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"><?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; ?>


0

简单的方法,但没有必要比别人更好。

更新您的翻译文件Mage_Catalog.csv。如下设置空值。

N/A,""
No,""

否或不适用时,前端属性将被忽略。


0

有时我们遇到一家想要拥有许多不同商品属性的商店,但他们只想要默认属性集。这意味着每个产品都会有10个以上的选项,这些选项有时不适用于某些产品。例如,一件衣服可能需要size属性,而一件家具则不需要。因为商店对每种产品使用相同的属性集,所以空尺寸属性将显示如下:

当然这对于客户来说非常令人困惑,因此更好的选择是隐藏为空的属性值。只需一小段代码即可完成。查找并打开attributes.phtml文件。该文件可以在这里找到:app/design/frontend/default/[theme name]/template/catalog/product/view/attribute.phtml

打开文件并搜索以下行:

<?php foreach ($_additional 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"><?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; ?>

用以下代码行替换整个foreach循环:

<?php foreach ($_additional 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"><?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; ?>

而已!空属性现在将从您的产品页面中隐藏。不要忘记刷新缓存以查看更改。

来源https : //tejabhagavan.blogspot.in/2016/03/hide-magento-attributes-with-no-value-2.html


无法使用上述代码
-Gem
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.