In Magento how to add class in list depending on the attributes to be displayed?

Asked

Viewed 401 times

7

I have a foreach that brings me all the children of an X attribute, and I need to add a specific class to the list <ul> to be able to format by CSS according to the Y attribute. However, I am unable to add the attribute name in the class.

Below is an example taken from app/design/frontend/base/default/tempplate/Catalog/layer/filter.phtml

<ol class="filters filter-type">
<?php foreach ($this->getItems() as $_item): ?>
    <li>
        <?php if ($_item->getCount() > 0): ?>
        <a href="<?php echo $this->urlEscape($_item->getUrl()) ?>"><?php echo $_item->getLabel() ?></a>
        <?php else: echo $_item->getLabel() ?>
        <?php endif; ?>
        <?php if ($this->shouldDisplayProductCount()): ?>
        (<?php echo $_item->getCount() ?>)
        <?php endif; ?>
    </li>
<?php endforeach ?>
</ol>

On a filter category page, I want that when loading product attributes these blocks have a specific class that identifies.

Example:

Size

  • 38
  • 39
  • 40

Sex

  • Masculine
  • Feminine

Price

  • From 30 to 40
  • From 50 to 80
  • From 100 to 200

Where each block receives a class: filter-size, filter-sex, filter-price, only I don’t know what attribute it will bring and I need it to be dynamic.

  • 1

    Friend, give us some code to better illustrate what you need.

  • 1

    Friend, if you pass what is the exact condition to enter the class and into which element, we can pass what is the best code to do this.

2 answers

3


Well, let’s take a few guesses so we can show a viable code.

<?php
    $produtos['tipo1'] = [
        // Produtos tipo um
    ];

    $produtos['tipo2'] = [
        // Produtos tipo dois
    ];

    $produtos['tipo3'] = [
        // Produtos tipo três
    ];

    foreach ($produtos as $tipo => $listaProdutos): ?>

    <ol class="filters filter-<?= $tipo ?>">

        <?php foreach ($listaProdutos as $produto): ?>

            <li><?= $produto->nome ?></li>
            <li><?= $produto->descricao ?></li>
            <li><?= $produto->valor ?></li>

        <?php endforeach; ?>

    </ol>
<?php
    endforeach;
?>

That’s what gets me in the head now, helps you?

Att

-3

If I understand correctly, what you want is to add a class in the <li> in case this filter has any product to be brought, right?

I think I’d look like this:

<li<?php echo ($_item->getCount() > 0):?' class="temitens"':'';?>>

That way, if you have items it adds a class temitens, otherwise does nothing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.