Jump to content

Call category list in custom module


Crezzur.com

Recommended Posts

Hey all,

 

I want to display the category list in my module like presta shop does.

 

Example:

YzB9KKO.png

 

After searching i have found the code to call the menu should be in the function getParentsCategories();

But I'm not able to call the list like i marked above.

 

Code i tried:

// Define id_category
$id_category = $this->category->id;

// Initialize object Category
$category = new Category((int)$id_category);

// Get array parent from the initialized object Category
$array_parent = $category->getParentsCategories();

So i need the php code to display the category menu in my module.

 

Thanks in advance!

Edited by Crezzur (see edit history)
Link to comment
Share on other sites

With getParentsCategories, you only get the direct parents of a specific category, which is one branch of the tree. I expect you want the whole tree, so maybe better use the code they use in back office, catalog->products->edit product (associations tab)

 

from controllers/admin/AdminProductsController.php

 

 

$tree = new HelperTreeCategories('associated-categories-tree', 'Associated categories');
$tree->setTemplate('tree_associated_categories.tpl')
->setHeaderTemplate('tree_associated_header.tpl')
->setRootCategory($root->id)
->setUseCheckBox(true)
->setUseSearch(true)
->setSelectedCategories($categories);
 
$data->assign(array(
    'category_tree' => $tree->render()
));
 

 

For full use how to implement it, look in the file.

 

 

 

 

and then in your tpl file something like:

 

<div id="category_block">
{$category_tree}
</div>
 
 
 
My 2 cents,
pascal.
  • Thanks 1
Link to comment
Share on other sites

@PascalVG

 

Indeed i looked at the wrong function

 

i do not use a .tpl file for this part of back-office so i tried to put it into a variable which results in nothing.

 

using the following code in my datasheetpro.php also results in a empty variable.

$helper = new HelperTreeCategories('categories-treeview');
$category_tree = $helper->render();

echo 'TEST: '.$category_tree;

any idea ?

Link to comment
Share on other sites

i found the following code:

 

Function:

public static function getCategoryTree($id_product,$id_lang){
	$root = Category::getRootCategory();
	$selected_cat = Product::getProductCategoriesFull($id_product, $id_lang);
	$tab_root = array('id_category' => $root->id, 'name' => $root->name);
	$helper = new Helper();
	$category_tree = $helper->renderCategoryTree($tab_root, $selected_cat, 'categoryBox', false, true, array(), false, true);
	return $category_tree;
}
$id_test = Configuration::get('PS_ROOT_CATEGORY');
$txt .= '<div> TEST: '.$this->getCategoryTree($id_test, $id_lang).' </div>

This gives me this as result:

71QGUmT.png

 

but not a full tree for some reason ?

Edited by Crezzur (see edit history)
Link to comment
Share on other sites

  • 1 year later...

Hello!

 

Sorry for reopening this thread. I try to list category tree list, I use the code below, but this code list with radio buttons, and i need checkbox buttons, like in picture from first post. I dont want to use any .tpl file.

 

array(
           'type'  => 'categories',
           'label' => $this->l('Product Category'),
           'desc'    => $this->l('Product Category.'),  
           'name'  => 'category',
           'tree'  => array(
                'id' => 'category',
                'selected_categories' => array((int)Configuration::get('category')),
            )
        ),

 

Does anyone know how to do that?

Thank you!

Link to comment
Share on other sites

  • 2 years later...
  • 7 months later...
On 5/30/2015 at 10:03 PM, Crezzur said:

i found the following code:

 

Function:


public static function getCategoryTree($id_product,$id_lang){
	$root = Category::getRootCategory();
	$selected_cat = Product::getProductCategoriesFull($id_product, $id_lang);
	$tab_root = array('id_category' => $root->id, 'name' => $root->name);
	$helper = new Helper();
	$category_tree = $helper->renderCategoryTree($tab_root, $selected_cat, 'categoryBox', false, true, array(), false, true);
	return $category_tree;
}

$id_test = Configuration::get('PS_ROOT_CATEGORY');
$txt .= '<div> TEST: '.$this->getCategoryTree($id_test, $id_lang).' </div>

This gives me this as result:

71QGUmT.png

 

but not a full tree for some reason ?

Hello friends,

did you find the solution for this problem?

Thanks.

Link to comment
Share on other sites

Can you please try this one.

$selected_cat = Product::getProductCategoriesFull($id_product, $id_lang);
$root = Category::getRootCategory();
$tree = new HelperTreeCategories('kbmp-categories-tree');
$tree->setRootCategory($root->id)
    ->setInputName('kbmp_allowed_categories')
    ->setUseCheckBox(true)
    ->setUseSearch(false)
    ->setSelectedCategories((array) $selected_cat );
return $tree->render();

 

  • Like 1
Link to comment
Share on other sites

4 hours ago, Knowband Plugins said:

Can you please try this one.


$selected_cat = Product::getProductCategoriesFull($id_product, $id_lang);
$root = Category::getRootCategory();
$tree = new HelperTreeCategories('kbmp-categories-tree');
$tree->setRootCategory($root->id)
    ->setInputName('kbmp_allowed_categories')
    ->setUseCheckBox(true)
    ->setUseSearch(false)
    ->setSelectedCategories((array) $selected_cat );
return $tree->render();

 

You're really really great man.

It works but it brings out this errors, do you know what are they?

Notice en línea 138 en archivo C:\\xampp\\htdocs\\PS\\classes\\helper\\HelperTreeCategories.php
[8] Array to string conversion

Notice en línea 138 en archivo C:\\xampp\\htdocs\\PS\\classes\\helper\\HelperTreeCategories.php
[8] Array to string conversion

Notice en línea 138 en archivo C:\\xampp\\htdocs\\PS\\classes\\helper\\HelperTreeCategories.php
[8] Array to string conversion

Warning en línea 142 en archivo C:\\xampp\\htdocs\\PS\\classes\\helper\\HelperTreeCategories.php
[2] Illegal offset type

 

 

Now, I only need to capture the selected value.
Could you give me any tip?

 

Thanks for your time.

Edited by zenerry (see edit history)
Link to comment
Share on other sites

  • 3 months later...

Hello there,

I would like to create a custom module where I can display a list of available categories and be able to select multiple ones to then use them on front.

I used @ovy79ro code to display the list in back-office but I'll be glad to have some help to go further:

1. List is collapse by default, how can I expand it by default?

2. I used the checkbox property but I've got radio button and can select only one category instead of multiple ones

array(
  'type'  => 'categories',
  'label' => $this->l('Product Category'),
  'name'  => 'FEATURED_CATEGORIES',
  'tree'  => array(
    'id' => 'category',
    'selected_categories' => array((int)Configuration::get('category')),
  ),
  'use_checkbox' => true
),

3. I'm not sure how I'm suppose to save the selected categories, how to get selected categories in getConfigFormValues()?

protected function postProcess()
{
  $form_values = $this->getConfigFormValues();

  foreach (array_keys($form_values) as $key) {
    Configuration::updateValue($key, Tools::getValue($key));
  }
}

Thanks a lot for any help!

Edited by Minsky_ae (see edit history)
Link to comment
Share on other sites

I have the answer for question 2, I didn't put the use_checkbox at the right place! 😓

array(
  'type'  => 'categories',
  'label' => $this->l('Featured categories'),
  'name'  => 'FEATURED_CATEGORIES',
  'tree'  => array(
    'id' => 'category',
    'selected_categories' => array((int)Configuration::get('category')),
    'use_checkbox' => true
  )
),

But it still collapse by default and I still don't have any clues on how to save the selected_categories.

Edited by Minsky_ae
Edit code (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...