简体   繁体   中英

Magento - Single Custom Product Attribute with Array of Values

I have a need in Magento to provide an array custom product attribute that outputs all of its values as list items. The basic premise is a list of product ingredients... on a per product basis we need to enter (for example) water, salt, colourings - and for those to be rendered as a list on the front end.

My logic so far has been to use the standard text field attribute, entering comma separated values in the back-end and then to try and use that string as an array from which I can use foreach to create the unordered list.

So far I can echo the entire string as just one list item, but rendering the string as an array of its individual values has so far stumped me! See below...

The Ingredients text field attribute has a value of "water", "salt", "colourings". - the addition of quote marks and commas is only the assumption that this would pre-format the list ready to be an array.

<?php
$ingredientsArrayContent = $this->getProduct()->getSpa_productingredients();
$ingredientsArray = array($ingredientsArrayContent);
?>
<ul>
    <?php
    reset($ingredientsArray);
    foreach ($ingredientsArray as $ingredientsValue) {
        echo "<li>$ingredientsValue</li>\n";
    }
    ?>
</ul>

So on the front end this is outputting:

<ul>
    <li>"water", "salt", "colourings"</li>
</ul>

What of course I am looking to achieve is:

<ul>
    <li>water</li>
    <li>salt</li>
    <li>colourings</li>
</ul>

Am I over complicating this and missing something really obvious even in Magento? Any pointers greatly appreciated!!

Perhaps instead of:

$ingredientsArray = array($ingredientsArrayContent);

try using:

$ingredientsArray = array(explode(",",$ingredientsArrayContent));

Depending on whether your attribute is set as: "water,salt,colourings" or "water, salt, colourings" your delimiter might need to change or how you set your attribute values might need to change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM