1

I have actual the follow code:

    <?php 
$parentCategory = Mage::getModel('catalog/category')->load(3);
$subcategoryCollection = Mage::getModel('catalog/category')
        ->getCollection()
        ->addFieldToFilter('level', ['lteq' => $parentCategory->getLevel() + 2])
        /*->addFieldToFilter('level', $parentCategory->getLevel() + 1)*/
        ->addFieldToFilter('path', ['like' => $parentCategory->getData('path') . '%'])
        ->addAttributeToSelect(['name'])
        ->setOrder('name', Varien_Data_Collection_Db::SORT_ORDER_ASC);
foreach ($subcategoryCollection as $category): ?>
    <a href="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?></a>
<?php endforeach; ?>

And I like to have a sorted list, sorted like this:

A

Adidas Altra

B

Body Glove

D

Dakine etc

I also have the issue, I not need to have the category "Brands" and "View all Brands" in the list.

0

Try this:

<div class="mega-columns row">
<div class="col-sm12">
<div>
<?php $previous = null;?>
<?php foreach ($subcategoryCollection as $category): ?>
    <?php $name = $category->getName();
          $firstLetter = substr($name, 0, 1);
          if($previous !== $firstLetter){
             echo "</div><div class="row"><h3>".$firstLetter."</h3>";
          }
          $previous = $firstLetter;
    ?>

    echo '<div class="col-sw-4"><a href="'.$category->getUrl().'">'.$category->getName().'</a></div>';
<?php endforeach; ?>
</div>
</div>

This is obviously not the best solution but should work.

Reference

| improve this answer | |
  • It makes a funny output: A Acorn A Adidas A Altra A Arc'Teryx A Asics B Body Glove and instead of a, I needed to change to A. – Simon Vetterli Aug 20 '15 at 6:37
  • Check my updated answer. – Adarsh Khatri Aug 20 '15 at 6:50
  • Hi See my answer below, so far looks not that bad. Only need to create columns and put them on the page... Any idea how to do? Kindly Simon – Simon Vetterli Aug 20 '15 at 7:18
  • You just need to wrap it with some div classes. I have updated my answer, few classes might need to change as your css. Give it a try – Adarsh Khatri Aug 20 '15 at 11:45
  • Nearly... mag.outdoorequipped.com/allbrands only need to see, how I get each "letter" in a row... – Simon Vetterli Aug 20 '15 at 12:22
0

I modified it a bit:

    <?php
$parentCategory = Mage::getModel('catalog/category')->load(3);
$subcategoryCollection = Mage::getModel('catalog/category')
        ->getCollection()
        ->addFieldToFilter('level', ['lteq' => $parentCategory->getLevel() + 2])
        /*->addFieldToFilter('level', $parentCategory->getLevel() + 1)*/
        ->addFieldToFilter('path', ['like' => $parentCategory->getData('path') . '%'])
        ->addAttributeToFilter('entity_id', array('neq' => '3'))
        ->addAttributeToFilter('entity_id', array('neq' => '129'))
        ->addAttributeToSelect(['name'])
        ->setOrder('name', Varien_Data_Collection_Db::SORT_ORDER_ASC);
?>

<div class="mega-columns row">
<div class="col-sm12">
<div class="row">

<?php $previous = null;?>
<?php foreach ($subcategoryCollection as $category): ?>

    <?php $name = $category->getName();
          $firstLetter = substr($name, 0, 1);
          if($previous == null){
             echo '<div class="col-sw-4">'."\n";
             echo '<h3>'.$firstLetter.'</h3>'."\n";
             $previous = $firstLetter;
          }
          elseif ($previous !== $firstLetter){
                echo '</div>'."\n";
                echo '<div class="col-sw-4">'."\n";
                echo '<h3>'.$firstLetter.'</h3>'."\n";
                $previous = $firstLetter;
          }
          echo '<a href="'.$category->getUrl().'">'.$category->getName().'</a>'."\n";

    ?>

<?php endforeach; ?>
</div>
</div>
</div>
</div>

Only need to "group" it in columns and on the hole page.

Here is the result so far: https://mag.outdoorequipped.com/allbrands

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.