隐藏特定属性 - Magento
Hide a Specific Attribute - Magento
我决定在视图产品的顶部插入一些属性。我希望他们在有信息时出现。当为空时,我希望标签和信息消失。
问题是当当前属性为空时,即使它没有任何信息,它也会出现在标签上。
这是我的代码:
<div class="short-information">
<a><span class="label"><?php echo $_product->getResource()->getAttribute('autor')->getStoreLabel(); ?></span><span class="titles"><?php echo $this->htmlEscape($_product->getData('autor')); ?></span></a>
<a><span class="label"><?php echo $_product->getResource()->getAttribute('editorial')->getStoreLabel(); ?></span><span class="titles"><?php echo $this->htmlEscape($_product->getData('editorial'));?></span></a>
</div>
请问如何才能使这段代码正常工作?当属性为空时使标签消失。
请记住,这是我在页面中插入的属性代码,而不是页面视图底部的代码。
您没有指定属性是哪个,但您可以使用如下 if 语句来指定:
<?php if($_product->getResource()->getAttribute('autor')->getStoreLabel()): ?>
<a><span class="label"><?php echo $_product->getResource()->getAttribute('autor')->getStoreLabel(); ?></span><span class="titles"><?php echo $this->htmlEscape($_product->getData('autor')); ?></span></a>
<?php endif ?>
其他属性同上
好的,为此你需要先检查你的属性是否有一些值然后你只允许打印标签和相应的值。
<div class="short-information">
<?php if($this->htmlEscape($_product->getData('autor'))): //checks if author attribute has some value or not ?>
<a><span class="label"><?php echo $_product->getResource()->getAttribute('autor')->getStoreLabel(); ?></span><span class="titles"><?php echo $this->htmlEscape($_product->getData('autor')); ?></span></a>
<?php endif; ?>
</div>
这里我检查了 author
属性值,你可以用其他的做同样的事情。
尝试使用空函数或 trim 函数,如下所示:
if(!empty($attributeValue)) { //replace $attributeValue with your variable name..
//do your thing
}
你也可以检查这个 - https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/
更新:尝试更改此行
if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
到
if (!is_null($_product->getData($_attribute->getAttributeCode())) && (trim((string)$_attribute->getFrontend()->getValue($_product)) != '')) { ?>
希望对您有所帮助。
我决定在视图产品的顶部插入一些属性。我希望他们在有信息时出现。当为空时,我希望标签和信息消失。 问题是当当前属性为空时,即使它没有任何信息,它也会出现在标签上。
这是我的代码:
<div class="short-information">
<a><span class="label"><?php echo $_product->getResource()->getAttribute('autor')->getStoreLabel(); ?></span><span class="titles"><?php echo $this->htmlEscape($_product->getData('autor')); ?></span></a>
<a><span class="label"><?php echo $_product->getResource()->getAttribute('editorial')->getStoreLabel(); ?></span><span class="titles"><?php echo $this->htmlEscape($_product->getData('editorial'));?></span></a>
</div>
请问如何才能使这段代码正常工作?当属性为空时使标签消失。 请记住,这是我在页面中插入的属性代码,而不是页面视图底部的代码。
您没有指定属性是哪个,但您可以使用如下 if 语句来指定:
<?php if($_product->getResource()->getAttribute('autor')->getStoreLabel()): ?>
<a><span class="label"><?php echo $_product->getResource()->getAttribute('autor')->getStoreLabel(); ?></span><span class="titles"><?php echo $this->htmlEscape($_product->getData('autor')); ?></span></a>
<?php endif ?>
其他属性同上
好的,为此你需要先检查你的属性是否有一些值然后你只允许打印标签和相应的值。
<div class="short-information">
<?php if($this->htmlEscape($_product->getData('autor'))): //checks if author attribute has some value or not ?>
<a><span class="label"><?php echo $_product->getResource()->getAttribute('autor')->getStoreLabel(); ?></span><span class="titles"><?php echo $this->htmlEscape($_product->getData('autor')); ?></span></a>
<?php endif; ?>
</div>
这里我检查了 author
属性值,你可以用其他的做同样的事情。
尝试使用空函数或 trim 函数,如下所示:
if(!empty($attributeValue)) { //replace $attributeValue with your variable name..
//do your thing
}
你也可以检查这个 - https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/
更新:尝试更改此行
if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
到
if (!is_null($_product->getData($_attribute->getAttributeCode())) && (trim((string)$_attribute->getFrontend()->getValue($_product)) != '')) { ?>
希望对您有所帮助。