Kriuchko
1/9/2019 - 8:42 AM

Multi category filter

Magento 1 multi category filter

<?php
  $nav_class = new Mage_Catalog_Block_Navigation; 
  $cur_category = Mage::getModel('catalog/category')->load(Mage::registry('current_category')->getParentId());
  $categories = array();
  
  if(method_exists($cur_category, 'getChildrenCategories')){
              $childrens = $cur_category->getChildrenCategories();
  }
  else{
      $childrens = $cur_category->getChildren();
  }
  
  if($childrens && $childrens->count()){
      foreach ($childrens as $children){
                      if($children->getIsActive()){
                  $children_category = Mage::getModel('catalog/category')->load($children->getId());
                              $categorytype = $children_category->getResource()->getAttribute('type_of_category')->getFrontend()->getValue($children_category);
                              if(!empty($categorytype) && strtolower($categorytype) == $_GET['categorygroup']){
                  $categories[] = $children->getId();
              }
          }
      }
  }
      
  if(!empty($categories)){
      $products = mage::getModel('catalog/product')->getCollection()
          ->addAttributeToSelect('*')
          ->joinField('category_id',
              'catalog/category_product',
              'category_id',
              'product_id=entity_id',
              null,
              'left')
          ->addAttributeToFilter('category_id', array('in' => $categories));
      $products->getSelect()->group('e.entity_id');
  }

  if(count($products)) :
?>
  <div class="menu-category-products">
    <ul class="products-grid">
      <?php   foreach ( $products as $productModel ) : ++$counter; $_product = Mage::getModel('catalog/product')->load($productModel->getId());   ?>
        <li class="item"><h2 class="product-name">
        <a href="<?php echo $_product->getProductUrl() ?>">
        <?php if($_product->getmenutitle()){ echo $_product->getmenutitle();}else{ echo $this->htmlEscape($_product->getName()); } ?>
        </a></h2></li>
      <?php endforeach; ?>
    </ul>
  </div>
<?php endif; ?>