Vinai / groupscatalog2

Magento extension to enable you to hide categories and products from customers depending on their customer group. This is a Magento 1.6 and newer compatible version of the Netzarbeiter Customer Groups Catalog extension.
139 stars 60 forks source link

groupscatalog2 and phtml template #70

Closed godowsky closed 10 years ago

godowsky commented 10 years ago

Hello Vinai,

First of all thank you for your contribution. Everything is working fine with magento. I just got blocked with a phtml template that should show categories on the left. It does it but it shows hidden and visible categories at the same time, without filtering. (even if the customer does not click the hidden one, he gets the message is trying to access a private content, as setup in configuration). What should i add to this code to filter them correctly? I'm a php beginner. Thank you for your help

<?php
    $currentCat = Mage::registry('current_category');
    echo '<div class="block-title"><h2>'.$currentCat->getName().'</h2></div>';

    if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() ){
        $loadCategory = $currentCat;
    }else{
        $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getId());
    }
    $subCategories = explode(',', $loadCategory->getChildren());  
    if(count($subCategories) > 1){
        echo '<div class="block-content left-categorys-container">';
        foreach ( $subCategories as $subCategoryId ){
            $cat = Mage::getModel('catalog/category')->load($subCategoryId);
            if($cat->getIsActive()){
                echo '<a href="'.$cat->getURL().'">'.$cat->getName().'</a>';
            }
        }
        echo '</div>';
    }
?>
Vinai commented 10 years ago

Thanks for the notice! What version of Magento and the GroupsCatalog2 extension are you using?

godowsky commented 10 years ago

Magento CE 1.8 and groups catalogue 0.3.1.

Vinai commented 10 years ago

Individual loading of hidden categories should result in the customer being redirected to whatever is configured in the system configuration for the extension. The categories will only be hidden if they are loaded as a collection.

On a side note, the code you posted is working very inefficient while loading the categories. It would be much faster to issue a single query for all child categories, like in

$subCategories = $loadCategory->getChildrenCategories();
foreach ($subCategories as $cat) {
    // No need to check getIsActive() because getChildrenCategories() only returns active children
    echo '<a href="'.$cat->getURL().'">'.$cat->getName().'</a>';
}

Also, $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getId()); is the same as $loadCategory = $currentCat. I'm not sure what you are trying to achieve with that if...else condition at the beginning of the file.

And finally, the method getURL() should be spelled getUrl(). Casing is very important within the Magento framework, and depending on the situation both methods could return completely different results.

Vinai commented 10 years ago

Closing since there is no follow up from the original poster.