1

我试图通过为我的单独类别提供带有子级链接的选项卡选择来使我的大目录有点平板电脑友好。因此,如果用户单击(第 1 级)头部类别,它需要显示 nr 个块,其中包含每个直接子项的图片、描述和 url,以及所显示的(第 2 级)的所有底层(第 3 级)子类别的列表级别)类别。你们会这么好心检查我的代码吗?

<?php
   $layer = Mage::getSingleton('catalog/layer');
   $_category   = $layer->getCurrentCategory();
   $_categories = $_category->getCollection()

->addAttributeToSelect(array('url_key','name','image','all_children','is_anchor','description'))
     ->addAttributeToFilter('is_active', 1)
     ->addIdFilter($_category->getChildren())
     ->setOrder('position', 'ASC')
     ->joinUrlRewrite();
?>

             <?php $children = explode( ",", $this->getCurrentCategory()->getChildren() ); ?>

    <ul class="category-grid">
    <div class="category-list">
        <?php foreach( $children as $child ): ?>
            <?php $_child = Mage::getModel( 'catalog/category' )->load( $child ); ?>
            <li class="item">
            <a href="<?php echo $_child->getURL() ?>" target="_self"><img title="<?php echo $this->htmlEscape($_child->getName()) ?>" src="<?php echo $this->htmlEscape($_child->getImageUrl()) ?>" alt="<?php echo $this->htmlEscape($_child->getName()) ?>" /></a>
            <div class="subcategory-title">
        <a href="<?php echo $_child->getURL() ?>" title="<?php echo $this->htmlEscape($_child->getName()) ?>"><?php echo $this->htmlEscape($_child->getName()) ?></a>
        </div>
           <div class="description-block"> <?php echo $_child->getDescription(); ?></div>
           <div class="children-links"><?php
           $_helper = Mage::helper("catalog/category"); 
             $rootCat = Mage::app()->getStore()->getRootCategoryId();
             $current = Mage::registry('current_category');

                 if ($child){
                    //look for anchestor 
                    $parentid = $child->getParentId();
                    $parent = Mage::getModel("catalog/category")->load($parentid); 
                    if($parentid != $rootCat)
                    {   
                        //find the anchestor
                        show_cat($parent,$_helper,$rootCat);
                    }else{
                        //is root 
            $_subcategories = $child->getChildrenCategories();
                echo $_child->getAll_Children(); 
                 if(count($_subcategories)>0){
                            echo '<ul>';
                                    foreach($_subcategories as $_category){
                                        echo '<li>';
                                        echo '<a href="'.$_helper->getCategoryUrl($_category).'">'.$_category->getName().'</a>';

                                            if($child->getId() == $_category->getId()){
                                                $current = Mage::registry('current_category');
                                                if ($current){
                                                    //handle current
                                                    $_current_subcategories = $current->getChildrenCategories();
                                                        if(count($_current_subcategories)>0){
                                                            //the current cat has childrens
                                                            echo '<ul>';
                                                            foreach($_current_subcategories as $_sub_category){
                                                                echo '<li>';
                                                                echo '<a href="'.$_helper->getCategoryUrl($_sub_category).'">'.$_sub_category->getName().'</a>';
                                                                echo '</li>';
                                                            }
                                                            echo '</ul>';
                                                        }else{
                                                            //the current cat has no childrens
                                                            $current_parent = $current->getParentId();
                                                            $current_parent  = Mage::getModel("catalog/category")->load($current_parent ); 
                                                            $_current_subcategories = $current_parent ->getChildrenCategories();
                                                            echo '<ul>';
                                                                foreach($_current_subcategories as $_sub_category){
                                                                    echo '<li>';
                                                                    echo '<a href="'.$_helper->getCategoryUrl($_sub_category).'">'.$_sub_category->getName().'</a>';
                                                                    echo '</li>';
                                                                }
                                                            echo '</ul>';
                                                            }
                                                    }
                                                }
                                        echo '</li>';
                                        }
                                echo '</ul>'; 
                            }
                        }
                    }

                                ?>
                </div>  
          </li>
       <?php endforeach ?>
        </div>
    </ul>
4

3 回答 3

1

您可以通过以下代码进行此操作,也可以参考底部的链接

你可以用这个

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
    <ul class="category">
        <?php foreach($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a>
                <?php if ($currentCategory->getId() && $currentCategory->getId() == $_category->getId()): ?>
                    <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                    <?php $_subcategories = $_category->getChildrenCategories() ?>
                    <?php if (count($_subcategories) > 0): ?>
                        <ul class="subcategory">
                            <?php foreach($_subcategories as $_subcategory): ?>
                                <li>
                                    <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                        <?php echo $_subcategory->getName() ?>
                                    </a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

编辑

<ul class="subcategory">
            <? foreach ($_categories as $_category):?>
                 <? if($_category->getIsActive()):
                      $cur_subcategory=Mage::getModel('catalog/category')->load($_category->getId());
                       $layer = Mage::getSingleton('catalog/layer');
                       $layer->setCurrentCategory($cur_subcategory);
                      ?>

                     <li><a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $_category->getName()?></a></li>
                <? endif;?>

         <?endforeach?>

      </ul>

或者你可以扔掉这个 Detail 文档,我相信这对你很有帮助。

于 2013-08-30T16:11:30.147 回答
0
// get current category
$current_category   = $layer->getCurrentCategory(); 

// get sub categories of current category
$parent_categories = Mage::getModel('catalog/category')->getCategories($current_category->getId());

// go through each sub category and get their sub categories.
foreach($parent_categories as $child_category)
{
     $child_category_id = $child_category->getId();
     $grandchild_categories = Mage::getModel('catalog/category')->getCategories($child_category_id);

}
于 2013-08-30T18:01:00.150 回答
0

这是显示子类别的测试代码。只需将此代码放在自定义主题“Magento_catalog/templet/product/list.phtml”中的“工具栏”代码上方

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
$subcats = $category->getChildrenCategories();
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$imageHelper = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Catalog\Helper\Image::class);


if(count ($subcats) > 0)
{
?>
<div class = "sub-cat-div">
    <ul class = "sub-cat-main">
        <?php
        foreach ($subcats as $subcat) {
            if ($subcat->getIsActive()) {
                $_category = $objectManager->create('Magento\Catalog\Model\Category')->load($subcat->getId());
                $_outputhelper = $this->helper('Magento\Catalog\Helper\Output');
                $subcaturl = $_category->getUrl();

                $_imgHtml = '';
                if ($_imgUrl = $_category->getImageUrl()) {

                    $_imgHtml = '<img src="' . $_imgUrl . '" />';
                    $_imgHtml = $_outputhelper->categoryAttribute($_category, $_imgHtml, 'image');


                    echo '<li class="sub-cat-image"><a href="' . $subcaturl . '" class="block-promo" title="' . $_category->getName() . '">' . $_imgHtml . '<span style="background-color: rgba(255,255,255,0.9)" class="content bg-white"><strong>' . $_category->getName() . '</strong></span></a></li>';
                }
                else{
                    $_imgUrl = $imageHelper->getDefaultPlaceholderUrl('image');
                    $_imgHtml = '<img src="' . $_imgUrl . '" />';
                    $_imgHtml = $_outputhelper->categoryAttribute($_category, $_imgHtml, 'image');


                    echo '<li class="sub-cat-image"><a href="' . $subcaturl . '" class="block-promo" title="' . $_category->getName() . '">' . $_imgHtml . '<span style="background-color: rgba(255,255,255,0.9)" class="content bg-white"><strong>' . $_category->getName() . '</strong></span></a></li>';
                }
            }
        } ?>
    </ul>
</div>
<?php 
}
?>
于 2019-07-01T10:38:09.163 回答