根据主类别 id 列出所有子类别

List all subcategories based on main category id

我需要获取属于特定类别的所有类别 ID。是否可以根据主类别 ID 列出所有子类别类别?

如果您查看 Category class,您会看到一个名为 getSubCategories():

的方法
/**
  * Return current category childs
  *
  * @param integer $id_lang Language ID
  * @param boolean $active return only active categories
  * @return array Categories
  */
public function getSubCategories($id_lang, $active = true)
{
    [...]
}

因此,如果您有一个 $category 对象并希望所有活动的子类别打印在前面,您可以这样做:

if (isset($this->name[Context::getContext()->language->id]))
    $id_lang = Context::getContext()->language->id;
else
    $id_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$subcategories = $category->getSubCategories($id_lang, true);
from prestapyt import PrestaShopWebServiceDict
from xml.etree import ElementTree

URL = ''
API_KEY = ''

cat_list = []

def get_categories(category_id):

    prestashop = PrestaShopWebServiceDict( URL, API_KEY )

    try:
        # get first childrens
        categories = prestashop.get('categories', category_id)['category']['associations']['categories']['category']

        for category in categories:
            print "main cat: " + category['id']
            cat_list.append(category['id'])

            try:
                # get second childrens
                categories = prestashop.get('categories', category['id'])['category']['associations']['categories']['category']

                for category in categories:
                    print category['id']
                    cat_list.append(category['id'])

            except:
                print "Category " + str(category['id']) + " has not children."

    except:
        print "Category " + str(category_id) + " does not exist."


get_categories(98);

此外,我通过 api 创建了类似的东西,但它只显示了一个 children 但我必须请求两次(我不需要较低的级别)。