【问题标题】:Magento list categories via position and html output by number of sub-categoriesMagento 通过位置和 html 按子类别数量列出类别
【发布时间】:2016-04-19 13:04:46
【问题描述】:

我正在尝试使用以下代码做两件事。
1)目前输出是根据类别 ID 对类别进行排序,但我希望它与后端位置结构相匹配。 2) 我希望能够根据类别具有的直接子项的数量来更改 html 输出,因此我可以输出没有具有不同 css 样式的直接子项的类别。

到目前为止我的代码是

<?php 
$parentCategoryId = 486;
$cat = Mage::getModel('catalog/category')->load($parentCategoryId);
$subcats = $cat->getChildren();
?> 
<?php 
foreach(explode(',',$subcats) as $subCatid)
{
  $_category = Mage::getModel('catalog/category')->load($subCatid);
  if($_category->getIsActive()) {
    echo '<ul><a href="'.$_category->getURL().'" title="View the products for the "'.$_category->getName().'" category">'.$_category->getName().'</a>';
    $sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
    $sub_subcats = $sub_cat->getChildren();
    foreach(explode(',',$sub_subcats) as $sub_subCatid)
    {
          $_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
          if($_sub_category->getIsActive()) {
              echo '<li class="sub_cat1"><a href="'.$_sub_category->getURL().'" title="View the products for the "'.$_sub_category->getName().'" category">'.$_sub_category->getName().'</a></li>';
              $sub_sub_cat = Mage::getModel('catalog/category')->load($sub_subCatid);
              $sub_sub_subcats = $sub_sub_cat->getChildren();
              foreach(explode(',',$sub_sub_subcats) as $sub_sub_subCatid)
          {
                $_sub_sub_category = Mage::getModel('catalog/category')->load($sub_sub_subCatid);
                if($_sub_sub_category->getIsActive()) {
                    echo '<li class="sub_cat2"><a href="'.$_sub_sub_category->getURL().'" title="View the products for the "'.$_sub_sub_category->getName().'" category">'.$_sub_sub_category->getName().'</a></li>';
                    $sub_sub_sub_cat = Mage::getModel('catalog/category')->load($sub_sub_subCatid);
              $sub_sub_sub_subcats = $sub_sub_sub_cat->getChildren();
              foreach(explode(',',$sub_sub_sub_subcats) as $sub_sub_sub_subCatid)
              {
                $_sub_sub_sub_category = Mage::getModel('catalog/category')->load($sub_sub_sub_subCatid);
                if($_sub_sub_sub_category->getIsActive()) {
                    echo '<li class="sub_cat3"><a href="'.$_sub_sub_sub_category->getURL().'" title="View the products for the "'.$_sub_sub_sub_category->getName().'" category">'.$_sub_sub_sub_category->getName().'</a></li>';
                    }
                  }
                }
              }
           }
     }
     echo '</ul>';
  }
}
?>

非常感谢您提前提供的帮助。

【问题讨论】:

    标签: php html css magento


    【解决方案1】:

    试试这个,

            <?php 
            $category = Mage::getModel('catalog/category')->load(486);
            $children = Mage::getModel('catalog/category')->getCollection()->setStoreId(Mage::app()->getStore()->getId());
            $children->addAttributeToSelect('*')
                    ->addAttributeToFilter('parent_id', $category->getId())
                    ->addAttributeToFilter('is_active', 1)//get only active categories if you want
                    ->addAttributeToSort('position');//sort by position
    
            foreach ($children as $sub_subCatid){
                echo $sub_subCatid->getName();
                $children = Mage::getModel('catalog/category')->getCollection()->setStoreId(Mage::app()->getStore()->getId());
                $children->addAttributeToSelect('*')
                    ->addAttributeToFilter('parent_id', $sub_subCatid->getId())
                    ->addAttributeToFilter('is_active', 1)//get only active categories if you want
                    ->addAttributeToSort('position');//sort by position
                //repeat the loop   foreach ($children as $sub_subCatid){ //repeat the loop }
            }
            ?>
    

    【讨论】:

    • 你能告诉我如何将链接和html标签注入这段代码吗?事实上,它只输出没有标记的名称。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多