0

I'm still learning Magento, I want to list subcategories instead of products, and I've done it, but I'm wondering if i've done it in the best way that can be done, and if I can move the logic from phtml to another file.

First I created a phtml file

<?php
    function getCurrentChildCategories()
    {
        $layer = Mage::getSingleton('catalog/layer');
        $category   = Mage::registry('current_category');
        /* @var $category Mage_Catalog_Model_Category */
        $collection = Mage::getModel('catalog/category')->getCollection();
        /* @var $collection 
           Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
        $collection->addAttributeToSelect('url_key')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('is_anchor')
            ->addAttributeToSelect('image')
            ->addAttributeToSelect('show_on_summary')
            ->addAttributeToFilter('is_active', 1)
            ->addAttributeToFilter('show_on_summary', 1)
            ->addIdFilter($category->getChildren())
            ->setOrder('position', 'ASC')
            ->joinUrlRewrite()
            ->load();

        $productCollection = Mage::getResourceModel('catalog/product_collection');
        $layer->prepareProductCollection($productCollection);
        $productCollection->addCountToCategories($collection);
        return $collection;
    }

    $_columnCount = 4;

?>

<div class="category-products">
    <?php echo $this->getToolbarHtml() ?>
    <ul class="products-grid products-grid--max-<?php echo $_columnCount; ?>-col">
        <?php
            $_categories= getCurrentChildCategories();
            $count = $_categories->count();
            if($count):
                $i=0; foreach ($_categories as $_category):
                if($_category->getIsActive() && $_category->getShowOnSummary()):
                    $cur_category=Mage::getModel('catalog/category')->load($_category->getId());
                    $layer = Mage::getSingleton('catalog/layer');
                    $layer->setCurrentCategory($cur_category);
                    $catName = Mage::registry('current_category')->getName();
         ?>
                    <li class="item <?php if($i == 0): ?> first<?php elseif($i == $_columnCount-1): ?> last<?php endif; ?>">
                        <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>">
                            <img src="<?php echo $_category->getImageUrl() ?>" width="100" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
                            <h4><?php echo $this->htmlEscape($_category->getName()) ?></h4>
                        </a>
                    </li>
            <?php
            endif;
            if ($i == $_columnCount)
            {
                $i = 0;
            } else
            {
                $i++;
            }
        endforeach;
    else: ?>
        <p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
    <?
    endif;
    ?>
    <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
    </ul>
</div>

Second I created a static block in admin-menu->CMS->static block

{{block type="core/template" template="mycompany/mymodule/categories.phtml"}}

Third I went to admin-menu->Catalog->Manage-Categories->selected-category->Display-Settings and changed the display mode from products only to static block only and the I select the CMS Block that I just created.

Doing this I see the subcategories with pictures listed instead of products, but I think there is so much logic in the phtml file, I'm wondering if it's posible to move it to another place.

When I developed my own modules I moved this logic into the php files inside the block folder, and when I want to render any of them I call the block like this

<block type="mycompany_mymodule/topcategories" name="topcategories" template="mycompany/mymodule/topcategories.phtml" />

The type is where the php block file is with the functions that I call from phtml.

But when I make a static block in the CMS admin menu, I can't make reference to my php block file with the logic in one of my modules (it do not works), I am forced to use a app/code/core/mage route like core/template, and obviously my functions with my logic aren't there. And I shoudn't touch the core files with my own code.

Is there any way to move the logic from the phtml to another place in this case? Is there a better way to list the subcategories instead of products?

Thanks ;)

0

I found an easy and fast solution to use my own custom blocks from my modules in the CMS static blocks. It seems that they have to be whitelisted.

In the admin panel, you have to go to System->Permissions->Blocks and there click on + add new block, now you only have to write the route of you custom block (for example mycompany_mymodule/topcategories) and set the status to allowed.

Now you can call your templates with that custom blocks containing your own logic. And the views (phtml) will be free of logic.

{{block type="mycompany_mymodule/topcategories" name="topcategories" template="mycompany/mymodule/topcategories.phtml" }}
|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.