dpvomqeu396484 2013-08-29 19:03
浏览 14

以magento获取产品的最后一个子类别

I having been searching for days to get the last subcategory of a product in magento.

Actually what i have to do is display the last subcategory the product is placed in. for example i have plastic and glass as products. I want to display the last subcategory i.e cups or plates.

|Party
--|boys
----|batman
--------|cups
-----------|plastic
-----------|glass
--------|plates
----|Superman

i have edited the list.phtml file, i can get the category id's and name from the array but they are all mixed up. So there is no way to figure out which one is the last category. Is there any default functionality in magento? or someone be kind enough to help me out? Thanks in advance.

  • 写回答

1条回答 默认 最新

  • double0201 2013-08-29 19:50
    关注

    Edit

    Okay, from your description it sounds like you want to get the child categories from the current category you're in. (I.E. Get cups and plates while in batman category view).

    The following should be just about as little as you need to get the current children.

    <?php
        $_helper = Mage::helper('catalog/category');
        $children = Mage::registry( 'current_category' )->getChildrenCategories();
    ?>
    <ul>
        <?php foreach( $children as $child ): ?>
            <li><a href="<?php echo $_helper->getCategoryUrl($child); ?>"><?php echo $child->getName() ?></a></li>
        <?php endforeach; ?>
    </ul>
    

    Previous Answer(s)

    It's a little roundabout, but this can get you the parent category id from a product object.

    //If you don't have a product to start with, load by ID.
    $_product = Mage::getModel( 'catalog/product' )->load($id);
    
    //Assign Category Model
    $categoryModel = Mage::getModel( 'catalog/category' );
    
    //Get Array of Category Id's with Last as First (Reversed)
    $_categories = array_reverse( $_product->getCategoryIds() );
    
    //Get Parent Category Id
    $_parentId = $categoryModel->load($_categories[0])->getParentId();
    
    //Load Parent Category
    $_category = $categoryModel->load($_parentId);
    
    //Do something with $_category
    echo $_category->getName();
    

    It works better when the product only has one category Id assigned to it. You may not get the category you want if multiple categories have been assigned to that one product.

    Also, you can get it a slightly quicker way, but this method doesn't work if the product was searched for:

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

    I didn't test the line above, but it should work. Only if the product was reached by browsing through the categories though.

    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入