【问题标题】:How to hide categories which is not having any products in magento?如何隐藏在magento中没有任何产品的类别?
【发布时间】:2015-08-07 14:58:37
【问题描述】:

我在点击任何品牌后显示产品类别,但我想隐藏那些没有任何产品的类别。我从过去 6 小时开始尝试找到解决方案,但我没有找到解决方案 下面是我的代码:

<?php

//Image Adjustment
$config = Mage::getModel('manufacturers/image_config');
$image = Mage::getModel('media/image');
$image->setConfig($config);
//Get Manfacturer Info
 $mId  = $this->getRequest()->getParam('id');

/****************Category Collection**********************/
$manufacturersTable = Mage::getSingleton('core/resource')->getTableName('manufacturers');
        $manufacturersProductsTable = Mage::getSingleton('core/resource')->getTableName('manufacturers_products');

        $sqry = "SELECT mp.product_id,mp.manufacturers_id FROM ".$manufacturersTable." m 
                INNER JOIN ".$manufacturersProductsTable." AS mp ON m.manufacturers_id = mp.manufacturers_id
                WHERE m.manufacturers_id = ".$mId;
        $connection = Mage::getSingleton('core/resource')->getConnection('core_read');
        $select = $connection->query($sqry);
        $prds = $select->fetchAll();    
        $categoryIds=array();
        $i=0;
        foreach ($prds as $_manufacturer) {

             $productIds= $_manufacturer["product_id"];
                $_product=Mage::getModel('catalog/product')->load($productIds);
             $categoryIds = array_merge($_product->getCategoryIds(), $categoryIds);

            $i++;
        }

$product=Mage::getModel('catalog/product')->load($prds[0]['product_id']);

       $manufacturer = $_product["manufacturer"];
       if(empty($manufacturer))
       {
       $manufacturer = $product->getManufacturer();
       }
?>
    <?php if ($mId != 0 ) { ?>
    <?php
        $model  = Mage::getModel('manufacturers/manufacturers')->load($mId);

        $x = Mage::helper('manufacturers')->getLogoWidth();  
        $y = Mage::helper('manufacturers')->getLogoHeight(); 
        $color = Mage::helper('manufacturers')->getLogoBackgroundColor();

        if($model["m_logo"] != "") {
            $imgPath = Mage::helper('manufacturers')->getResizedUrl($model["m_logo"],$x,$y,$color); 
        } else {            
            $imageFile =  "manufacturers/files/n/i/no_image_available.jpg";
            $imgPath = Mage::helper('manufacturers')->getResizedUrl($imageFile,$x,$y,$color);
        }

    ?>
$rootCatId = 257; // haikal 257
$catids=array();
$cat='';
function getTreeCategories($parentId, $isChild){
$allCats = Mage::getModel('catalog/category')->getCollection()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('is_active','1')
            ->addAttributeToFilter('include_in_menu','1')

            ->addAttributeToFilter('parent_id',array('eq' => $parentId))
            ->addAttributeToSort('position');

$class = ($isChild) ? "sub-cat-list" : "cat-list";


foreach ($allCats as $category) 
{

    $html .= $category->getID().',';
    $subcats = $category->getChildren();
    if($subcats != ''){

        $html .= getTreeCategories($category->getId(), true);

    }

}

return $html;
}
$catlistHtml = getTreeCategories($rootCatId, true);
//echo $catlistHtml;
($catlistHtml=explode(',', $catlistHtml));

if(count($_GET)) {

?>
<script>
function abc()
{



window.location = window.location.href.split("?")[0];


}
</script>
<div id="whole-cat" class="whole-cat" style="display:none;">
<?php
}
else{
?>


<div id="whole-cat" class="whole-cat" style="display:block">
<?php
}

global $showleftNav;

if(!$showleftNav)
{



$o = null;

$o .= '<div class="content">';

$_columnCount = 5; 
$i = 0;
$j = 0;


foreach ($catlistHtml as $_category) {


    // if the category is not match the product categories

    if(!in_array($_category, $categoryIds))  continue; 

    $i++;

    $_category = Mage::getModel('catalog/category')->load($_category);

    //$_category = $_category->load($catId);
   $imgurl=$_category->getImageUrl();

     $products_count = Mage::getModel('catalog/category')->load($_category->getId())->getProductCount();

     if($products_count==0) continue;

   if(empty($imgurl))
   {
   $imgurl=$this->getSkinUrl('images/small_image.jpg');
   }
    if ($j++%$_columnCount==0):
        $o .= '<ul class="pro-cat">';
    endif;
    $o .= '<li id="m'.$manufacturer.'" class="categoryName row'.$i.'"><a href="'.$_category->getUrl().'?manufacturer='.$manufacturer.'"><h4>'.$_category->getName().'</h4><img src="'.$imgurl.'" ></a></li class="totalNumberOfProducts">';



    if ($j%$_columnCount==0): 
    $o .= '</ul>';
    endif;
    }



echo $o;
?>


<?php 

$showleftNav==true;

}

?>
</div>
</div>
</div>

在输出中我看到没有隐藏类别

我已经尝试了几乎所有尝试按名称获取类别 id 的方法,以便我可以搜索类别并获取其 id 并应用过滤器。

My page link

【问题讨论】:

  • 有谁可以解决这个问题吗??
  • 这就是我从主菜单中隐藏空类别的方法。如果不完全是您想要的,它可能会为您指明正确的方向。 stackoverflow.com/questions/22822997/…

标签: php magento


【解决方案1】:

【讨论】:

  • 这只是隐藏类别??我的过程有点复杂,我先展示品牌,然后类别对我也有用吗??
  • 我尝试过但不适合我My Website 这是页面,您可以看到没有产品的类别仍然存在。 . :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多