1

I'm looking at catalog/category/view.phtml

So I suppose I should use getChildren, then loop through the children. How will I get from each child category a single product based on a certain sort order for the collection?

I need to limit the db query with a "LIMIT 1" clause or something like that, so that I do not load a bunch of products into PHP.

Suppose my categories are as such:

root_catalog
    |-T-shirts
        |-Long Sleeve
        |-Short Sleeve
    |-Cell Phones
        |-Smart Phones
        |-Dumb Phones

I would like to show one image for each subcategory. I would like to do this by dynamically getting the list of subcategories and getting the image for the 1st product in each subcategory. When you click on that image, you are taken to the subcategory (and, conveniently, you also see the actual product that was used as an example in the navigation image).

I know I may be going about this the wrong way. If so, please advise.

Thanks.

2

if you are in the category view page you can do the following.

$mainCategory = Mage::registry('current_category');
foreach ($mainCategory->getChildrenCategories() as $category){
    $collection = $category->getProductCollection()->addAttributeToSelect('image');//or thumbnail or small_image
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); 
    $collection->addAttributeToSort('position');//or 'name' or any other attribute
    $product = $collection->setPage(1, 1)->getFirstItem();
    if ($product->getId()){//if the category has at least a valid product
       $imageUrl = Mage::>helper('catalog/image')->init($product, 'image')->resize(60);
       //do something with $imageUrl
    }
}
  • That looks good. But I'm wondering if using getFirstItem still results in a $collection that contains all items. If so, then that is an unnecessary amount of loading. Maybe $collection->getSelect()->limit(1,0) would work faster with less memory/processor usage? I have not tried your solution yet but it looks like it has some good stuff. – Buttle Butkus Jun 26 '13 at 8:17
  • I've updated the answer. Until today I lived with the impression that getFirstItem does not load the collection. But after a search in the code it seams it does. You can add setPage(Page number, Page count) to limit the numer of items. – Marius Jun 26 '13 at 8:25
1
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<div class="services innerservices">
<ul>
<?php foreach($_categories as $_category): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<p><a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"><?php echo $_subcategory->getName() ?></a></p>
<figure class="maincatfig"> 
<img src="<?php $_subcategory->load( $_subcategory->getId()); echo $_subcategory->getImageUrl(); ?>" alt="<?php echo $_subcategory->getName() ?>"  />
<?php $_categorysub = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>
<?php $_subcategoriessub = $_categorysub->getChildrenCategories() ?>
<?php if (count($_subcategoriessub) > 0): ?>
<div class="cat-list">
<ul>
<?php foreach($_subcategoriessub as $_subcategorysub): ?>
<li><a title="View the products for <?php echo $_subcategorysub->getName() ?>" href="<?php echo $_helper->getCategoryUrl($_subcategorysub) ?>"><?php echo $_subcategorysub->getName() ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
</figure>
</li>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
  • Please explain what your code does and what parts (and how) are solving the OP's problem – 7ochem Apr 1 '16 at 8:23

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.