2

Anyone have any suggestions on how to edit the below to exclude 1 particular category? (ID - 1511)

Any help would be appreciated :) thanks

<?php
class Sinch_Tonerconfigurator_Helper_Category extends Mage_Catalog_Helper_Category
{

    public function canShow($category)
     {
         if (is_int($category)) {
             $category = Mage::getModel('catalog/category')->load($category);
         }

         if (!$category->getId()) {
             return false;
         }

         if (!$category->getIsActive()) {
             return false;
         }
         if (!$category->isInRootCategoryList()) {
 //            return false;
         }

         return true;
     }

}
?>


    public function loadDropdownAction(){
        $this->getResponse()->setHeader('Content-type', 'application/json');
        $catid = $this->getRequest()->getParam('catid', null);
        $lastlevel = $this->getRequest()->getParam('lastlevel', 0);
        if(!$catid){
            $this->getResponse()->setBody('{ "success": false, "subcategories": [] }');
            return;
        }
        $category = Mage::getModel('catalog/category')->load($catid);
        if(!$category->hasChildren()){
            $this->getResponse()->setBody('{ "success": false, "subcategories": [] }');
            return;
        }
        $children = (Mage::getSingleton('tonerconfigurator/category')->isRootCategory($catid) ? Mage::getSingleton('tonerconfigurator/category')->getRootSubcatColl() : $category->getChildrenCategories());
        $response = array();
        $response['success'] = false;
        $response['subcategories'] = array();
        foreach($children as $child){
            $childArray = array();
            $childArray['name'] = $child->getName();
            if($lastlevel){
                $childArray['URL'] = $child->getUrl();
            }else {
                $childArray['id'] = $child->getId();
            }
            $response['subcategories'][] = $childArray;
        }
        if(!empty($response['subcategories'])){
            $response['success'] = true;
        }
        $this->getResponse()->setBody(json_encode($response));
        return;
    }
}


2 Answers 2

1

In context of your code snippet: (with code optimized)

 if (!$category->getId() || !$category->getIsActive() || $category->getId() == 1511 ) {
         return false;
     }

    return true

However, I can see you start with a ->load($category), which is most likely a model being loaded, and preceding that, I'd make the assumption you are using a collection?

If using a collection, you have better performance to simply exclude this id from the collection itself by using a collection filter as such: ->addFieldToFilter('entity_id', array('ne' => 1511))

1
  • Hi, thanks for the answer, I've tried adding this and it doesn't seem to block the specific id from the drop down selection. Commented Feb 21, 2015 at 16:34
0

Please try this

  public function canShow($category)
     {
         if (is_int($category)) {
             $category = Mage::getModel('catalog/category')->load($category);
         }

    if ($category instanceof Mage_Catalog_Model_Category) {
         if (!$category->getId() || !$category->getIsActive() || !$category->isInRootCategoryList()  || ($category->getId() == 1511) ) {
             return false;
        }else{
        return true;
        }
    }

        return false;
     }
10
  • Thanks Amit, no joy though unfortunately, am I perhaps trying to edit the wrong file? my site is here: www.printcart.co.uk Under the cartridge finder drop downs I want to eliminate the option that says 'printers' as it's a dead end if you know what I mean. Could it be in the AjaxController.php file? Commented Feb 21, 2015 at 16:51
  • yes.it AjaxController.php.. But You need find the module
    – Amit Bera
    Commented Feb 21, 2015 at 17:02
  • Thanks, i've edited my original post to include the ajaxcontroller.php code also, not sure what you mean by find the module In the modules file is this: <?xml version="1.0"?> <config> <modules> <Sinch_Tonerconfigurator> <active>true</active> <codePool>local</codePool> </Sinch_Tonerconfigurator> </modules> </config> Commented Feb 21, 2015 at 17:09
  • please check $catid = $this->getRequest()->getParam('catid', null); give cat id or not
    – Amit Bera
    Commented Feb 21, 2015 at 17:31
  • or use Mage::getModel('catalog/category') ->setStoreId(Mage::app()->getStore()->getId()) ->load($catid); instead of Mage::getModel('catalog/category')->load($catid);
    – Amit Bera
    Commented Feb 21, 2015 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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