0

如何在弹出窗口中显示树结构中的所有类别,即。)如果我单击页面中的选择类别按钮,它应该显示带有树结构类别的弹出窗口。我尝试过这样,它将显示所有类别在看起来不太好的下拉列表中

<?php                                                                            

  $categories = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*')->addIsActiveFilter();
  $allcatid = array();
  $k=0;
  foreach ($categories as $c) {
     $allcatid[$k] = $c->getId();
     $k++;
  }
  $finalcat=array_shift($allcatid);
  $root= Mage::app()->getStore()->getRootCategoryId();
?>

<select id="category" class="myinput-text required-entry widthinput" name="category" >
    <?php foreach($allcatid as $keycat){?>
         <option value="<?php echo $keycat;?>"><?php echo Mage::getModel("catalog/category")->load($keycat)->getName(); ?></option>
    <?php } ?>
4

3 回答 3

1

这将帮助您获取类别树

<?php
$rootcatId= Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCategories($rootcatId);
function  get_categories($categories) {
    $array= '<ul>';
    foreach($categories as $category) {
        $cat = Mage::getModel('catalog/category')->load($category->getId());
        $count = $cat->getProductCount();
        $array .= '<li>'.'<a href="'.Mage::getUrl($cat->getUrlPath()). '">'.         $category->getName() . "(".$count.")</a>\n";
        if($category->hasChildren()) {
            $children = Mage::getModel('catalog/category')->getCategories($category->getId());
             $array .=  get_categories($children);
            }
         $array .= '</li>';
    }
    return  $array . '</ul>';
}
echo  get_categories($categories); ?>
于 2013-06-26T15:14:22.513 回答
1

输出视图

此代码包含一个带有产品计数的最终类别。

在此处输入图像描述

第 1 步转到 theme/layout/catalog.xml 将此代码放入文件中。

<reference name="left">
 <block type="catalog/navigation" name="category_list_sidebar" template="catalog/navigation/categorymenu.phtml"/>
</reference>

第 2 步转到 theme/template/catalog/ 并创建导航文件夹,还在文件夹内创建categorymenu.phtml文件并将代码放入文件中。

<?php
$_helper = Mage::helper('catalog/category');
$_categories = $_helper->getStoreCategories();
$currentCategory = Mage::registry('current_category');
?>
<div class="block block-list block-categorys">
    <div class="block-title">
        <strong><span>Category</span></strong>
    </div>
    <div class="block-content">
        <ul class="category_sub">
            <?php
                if (count($_categories) > 0){

                    global $index;
                    global $data;

                    foreach($_categories as $_category){

                        $check_child_class = check_child_par($_category->getId());
                        $collaps = ($check_child_class)? "<span class='show-cat'>+</span>" : "";
                        echo "<li class='".$check_child_class."'>";
                        echo "<a href='".$_helper->getCategoryUrl($_category)."'>".$_category->getName();
                        echo " (".product_count($_category->getId()).")";
                        echo "</a>".$collaps;
                        echo check_child($_category->getId());
                        echo "</li>";

                    }
                }
            ?>
        </ul>
    </div>
</div>

<?php
    function check_child($cid){
        $_helper = Mage::helper('catalog/category');
        $_subcategory = Mage::getModel('catalog/category')->load($cid);
        $_subsubcategories = $_subcategory->getChildrenCategories();

        if (count($_subsubcategories) > 0){
            echo "<ul>";
            foreach($_subsubcategories as $_subcate){

                $check_child_class = check_child_par($_subcate->getId());
                $collaps = ($check_child_class)? "<span class='show-cat'>+</span>" : "";

                echo "<li class='".$check_child_class."'>";
                echo "<a href='".$_helper->getCategoryUrl($_subcate)."'>".$_subcate->getName();
                echo " (".product_count($_subcate->getId()).")";
                echo "</a>".$collaps;
                echo check_child($_subcate->getId());
                echo "</li>";
            }
            echo "</ul>";
        }else{
            return "";
        }
    }


    function check_child_par($cid){

        $_subcat = Mage::getModel('catalog/category')->load($cid);
        $_subsubcats = $_subcat->getChildrenCategories();

        if (count($_subsubcats) > 0){
            return "parent";
        }else{
            return "";
        }
    }

    function product_count($cid){
        $products_count = Mage::getModel('catalog/category')->load($cid)->getProductCount();
        return $products_count;
    }

?>
于 2018-07-20T06:49:24.587 回答
0
    enter code here
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
    <ul>
        <?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>
<?php endif; ?>

这将获取所有类别和子类别,,,希望这会对您有所帮助

于 2013-07-01T10:37:09.957 回答