0

I'm pulling my hair out over this, and can't figure why it's not working.

I've got a script that displays all subcategories for the current category. But now I want to change it so I can specify a category ID at the start of the php file, and have the script show all subcategories for that ID.

<?php
/* File Location
* /new/app/design/frontend/base/default/template/catalog/navigation
*/
?>

<div id="categories">
<div class="col_full">
<div class="listing" >

<?php 

function getImageUrl($category)
{
$cur_category=Mage::getModel('catalog/category')->load($category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);

        $url = $this->getCurrentCategory()->getImageUrl();
    return $url;
};

//Get the Current Category
$_maincategorylisting=$this->getCurrentCategory();    

// Iterate all categories in store
foreach ($this->getCurrentChildCategories() as $_category):

    // If category is Active
    if($_category->getIsActive()):

        // Load the actual category object for this category
        $cur_category = Mage::getModel('catalog/category')->load($_category->getId());

        // Load a random product from this category
        $products = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cur_category);
        $products->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(1);

        $products->load();

        // This a bit of a fudge - there's only one element in the collection
        $_product = null;

        foreach ( $products as $_product ) {}

        if(isset($_product)):
        ?>

        <div class="sub-category-listing" style="float: left; margin-left: 10px; margin-right: 10px; padding: 0px; text-align: center; width: 155px; ">
            <div class="linkimage"><p><a href="<?php echo $this->getCategoryUrl($_category)?>" class="product-image">

        <?php
        $layer = Mage::getSingleton('catalog/layer');
        $layer->setCurrentCategory($cur_category);
        ?>      

        <?
        // If there is an image set for the category - Display it
        if($_imgUrl=$this->getCurrentCategory()->getThumbnail()):?>
            <?php $_category  = $this->getCurrentCategory(); ?>
            <img src="<?php echo Mage::getBaseUrl('media').'catalog/category/'.$_category->getThumbnail()  ?>" style="max-width:120px; max-height:120px; padding: 10px;" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
        <?php endif; ?>

        <?
        // If there is not a image set for that Category - Display a random product Image
        if(!$_imgUrl): 

        // Let's load the category Model and grab the product collection of that category

        $product_collection = Mage::getModel('catalog/category')->load($_category->getId())->getProductCollection();
        $product_collection->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(1);

        // Now let's loop through the product collection and print the ID of every product
        foreach($product_collection as $product) {

        // Get the product ID
        $product_id = $product->getId();

        // Load the full product model based on the product ID

        $full_product = Mage::getModel('catalog/product')->load($product_id);

        // Now that we loaded the full product model, let's access all of it's data

        // Let's get the Product Name

        $product_name = $full_product->getName();

        // Let's get the Product URL path

        $product_url = $full_product->getProductUrl();

        // Let's get the Product Image URL

        $product_image_url = $full_product->getImageUrl();

        // Let's print the product information we gathered and continue onto the next one

        } //End For Each
        ?>
            <img src="<?php echo $product_image_url; ?>" width="135" height="135"  style="padding: 10px;" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
        <?php endif; ?>
        </a>
            </div>
        <a href="<?php echo $this->getCategoryUrl($_category)?>">
        <h2 class="product-name" style="margin-bottom: 10px;"><?php echo $_category->getName()?></a></h2>

        </div>

        <?php
        endif;
    endif;
endforeach;
?>

I've been told it's around line 26/27 that I want to change (where $this states what category to use) but nothing I try seems to set the category ID. Most things I try end up in the actual page saying Fatal Error: call to a non-member object

Can someone please help me..? I was thinking it would be as simple as entering a variable with the id right at the start, and then using that variable throughout inplace of 'get current category'

UPDATE: visible link can be seen at www.gadgetclinic.co.uk/home - You can see it starts by saying Repairs, as this is the first category but I just want to show items inside this 'Repairs' category (category id is 19). If you click on Repairs, you can see subcats. These are what I Want to display.

UPDATE: Sorry, visible link wasn't showing block, but it now is. If anyone can help, I'd greatly appreciate it!

  • Can anyone help me please with this? – Lee Collings Dec 4 '13 at 11:55
1

Hope this code will figure out to get n level of subcategory.

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
    <ul id="nav">
        <?php foreach($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a>
                <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                <?php $_subcategories = $_category->getChildrenCategories() ?>
            <?php if (count($_subcategories) > 0): ?>
                <ul>
                    <?php foreach($_subcategories as $_subcategory): ?>
                        <li>
                            <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                <?php echo $_subcategory->getName() ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </li>
    <?php endforeach; ?>
</ul>

For More Details Refer this link for css

| 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.