简体   繁体   English

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

[英]Getting the last sub-category of a product in magento

I having been searching for days to get the last subcategory of a product in magento. 我一直在寻找几天来获取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 ie 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. 我已经编辑了list.phtml文件,我可以从数组中获取类别ID和名称,但是它们都混在一起了。 So there is no way to figure out which one is the last category. 因此,无法确定哪个是最后一个类别。 Is there any default functionality in magento? magento中有任何默认功能吗? or someone be kind enough to help me out? 还是有人善良地帮助我? Thanks in advance. 提前致谢。

Edit 编辑

Okay, from your description it sounds like you want to get the child categories from the current category you're in. (IE Get cups and plates while in batman category view). 好的,从您的描述看来,您似乎想从当前所在的类别中获取子类别。(即,在batman类别视图中获取cupsplates )。

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. 这是一个回旋处,但这可以使您从产品对象获得父类别ID。

//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. 如果产品仅分配了一个类别ID,则效果更好。 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. 但是只有通过浏览类别才能找到产品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM