简体   繁体   English

Magento-如何检查类别是否是另一个类别的子项

[英]Magento - How to check if a category is a child of another

I am retrieving all category IDs that a product belongs to like so in a helper: 我正在像助手一样检索产品所属的所有类别ID:

$productCategories = $this->getProduct()->getCategoryIds();

This gives me an array of category IDs. 这给了我一个类别ID数组。 In my case, each product will only be in one sub-category. 就我而言,每种产品只会属于一个子类别。 From the IDs I need to figure out the logical order based on which categories are children of which. 从ID中,我需要根据哪些类别是哪个子类别来确定逻辑顺序。

The aim is to create a method which I can call and specify the level of category I want for a product and it will return the path to that category. 目的是创建一种方法,我可以调用该方法并指定产品所需的类别级别,它将返回该类别的路径。

I know I can load a category like so: 我知道我可以加载这样的类别:

Mage::getModel('catalog/category')->load($categoryId)

and I know what I'm doing in PHP, but just a little stuck on the logic (and methods) to use here to achieve what I want. 而且我知道我在PHP中正在做什么,但是在这里使用的逻辑(和方法)仅停留了一点,即可实现我想要的功能。

The easiest way is to test if it has a Parent Id associated to it. 最简单的方法是测试它是否具有关联的父ID。 A parent_id that is > 0 means that it is a lower-level category. parent_id> 0表示它是一个较低级别的类别。 If the parent_id equals one it means that it is one level lower than the root category. 如果parent_id等于1,则表示它比根类别低一级。

$category = Mage::getModel('catalog/category')->load($categoryId)
echo $category->getParentId();

I advise against using raw SQL as it is bad practice, the database schema may at some point change. 我建议不要使用原始SQL,因为这是一种不好的做法,数据库架构有时可能会更改。

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

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