cm_w89
cm_w89

Reputation: 167

Prestashop - How to get a specific sub category using parent category id

I am new to PrestaShop. I am trying to get an array of sub-categories using parent category id, so I can refer to the different sub-category in different circumstances. Actually, I want to place an if statement in a foreach loop and check if its the first iteration of the loop then grab the link of 1st sub-category enlisted and if it's 2nd iteration then grab the link of 2nd sub-category and so on. Can anybody help me?

Thanks in advance! and sorry for my bad English.

Upvotes: 1

Views: 6984

Answers (2)

Marcin Jaworski
Marcin Jaworski

Reputation: 340

Prestashop 1.6 has getSubCategories function in category class. You should make new category object and use this function on it.

/**
 * Return current category childs
 *
 * @param int $id_lang Language ID
 * @param bool $active return only active categories
 * @return array Categories
 */

//public function getSubCategories($id_lang, $active = true)

$this->category = new Category($id_category, $this->context->language->id);

$subcategories = $this->category->getSubCategories($this->context->language->id);

Upvotes: 0

Serge P
Serge P

Reputation: 1863

to get first level children:

$subcategories = Category::getChildren($id_parent, $id_lang);

to loop through:

foreach($subcategories as $category) {
  echo $category['name'];
}

Upvotes: 2

Related Questions