0Day Forums
Magento: How do I separate the display of multiple attributes? - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: CMS (https://zeroday.vip/Forum-CMS)
+--- Thread: Magento: How do I separate the display of multiple attributes? (/Thread-Magento-How-do-I-separate-the-display-of-multiple-attributes)



Magento: How do I separate the display of multiple attributes? - tetroxide983 - 07-20-2023

I am using this code to display (on the frontend) all assigned attributes of a product in Magento:

<?php if ($_product->getData('metal')): ?>
<?php echo nl2br($_product->getResource()->getAttribute('metal')->getFrontend()->getValue($_product )) ?>
<?php endif; ?>

I need to separate each item with a bullet point `•` and a break `<br/>`. How would I edit this code to reflect those changes?
Thanks in advance.

_Sam


RE: Magento: How do I separate the display of multiple attributes? - klystron825875 - 07-20-2023

Assuming that the above code is "working"... make this change:

<?php echo "•".nl2br($_product->getResource()->getAttribute('metal')->getFrontend()->getValue($_product ))."<br/>" ?>

the `.` is a string concatenation operator.

Or are you asking how to do it if the above is the HTML of a listing of products? Then this should work...


<?php
$somehtml=nl2br($_product->getResource()->getAttribute('metal')->getFrontend()->getValue($_product ));
$somehtml=explode("<br />",$somehtml); // This ASSumes nl2br inserts <br /> between each line and this
// would lead to the behaviour you want.
// see:

[To see links please register here]

$somehtml=implode("<br/>\n•",$somehtml);
$somehtml="•".$somehtml."<br/>\n";
echo $somehtml;
unset($somehtml);
?>

`explode` makes an array from a string by chopping up the string via the `"<br />"`. `implode` then rejoins the the string by putting `<br/>\n•` between each element. And finally, I add the bullet point to the front and br at the end. When imploding, you still must consider the "outside" of the string.


RE: Magento: How do I separate the display of multiple attributes? - snood622876 - 07-20-2023

Solved.
It is easy: copy attributes.phtml from base if you do not have one in you default package.

Put in app/design/frontend/default/default/template/catalog/product/view/ folder

Change line 46 from:

<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>

to show separate product attributes:

<td class="data"><?php echo $_helper->productAttribute($_product, str_replace(",", "<br />", $_data['value']), $_data['code']) ?></td>





RE: Magento: How do I separate the display of multiple attributes? - coniophorayxjz - 07-20-2023

If you want more control over the output (maybe you want to style the individual value), use explode();

<?php $attr = explode("," , $_helper->productAttribute($_product, $_data['value'], $_data['code']));
echo "<ul>";
?>
<?php foreach ($attr as $attrValue) {
echo "<li>" . $attrValue . "</li>";
}
echo "</ul>";
?>