Get category tree in TYPO3 context

Questions around the TYPO3 integration and plugins
Forum rules
Always add your TYPO3, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
pla
Posts: 4
Joined: 06 Nov 2024, 11:05

Get category tree in TYPO3 context

Post by pla » 06 Nov 2024, 12:55

Hello there,

I try to get the AIMEOS category tree inside of TYPO3 but I become desperate because I don't find any post nor a helping documentation about this.

The only thing I've found was this documentation to get the AIMEOS context inside TYPO3:

https://aimeos.org/docs/latest/typo3/extend/

But then the interesting part is missing.

I try to get the category tree inside a custom MenuProcessor to add the categories to the main menu and I can't believe that I am the first person who tries to do this.

I already have this code snippet to get the base category (or any other category by code/id) but unfortunately I can't get the child categories from here.

Code: Select all

        $context = \Aimeos\Aimeos\Scheduler\Base::context();
        $locale = Base::locale($context);
        $context->setLocale($locale);

        $manager = \Aimeos\MShop::create($context, 'catalog');
        $filter = $manager->filter()->add('catalog.id', '==', '1');
        $category = $manager->search($filter);
Can anybody help me how to get the whole tree in a MenuProcessor?

I am using TYPO3 12.4.20 and aimeos/aimeos-typo3 2023.10.7 in a ddev environment with PHP 8.2

abeli887
Posts: 1
Joined: 04 Nov 2024, 12:47

Re: Get category tree in TYPO3 context

Post by abeli887 » 07 Nov 2024, 01:29

You can try using the getTree() method after fetching the base category. Something like this should work:

Code: Select all

$context = \Aimeos\Aimeos\Scheduler\Base::context();
$locale = \Aimeos\Base::locale($context);
$context->setLocale($locale);

$manager = \Aimeos\MShop::create($context, 'catalog');
$filter = $manager->filter()->add('catalog.id', '==', '1');
$category = $manager->search($filter);

$tree = $manager->getTree($category->getId()); // Gets all child categories

// Loop through $tree to build your menu
It will give you the full tree of categories and let you loop through it to make the menu. Hope that helps!

User avatar
aimeos
Administrator
Posts: 8318
Joined: 01 Jan 1970, 00:00

Re: Get category tree in TYPO3 context

Post by aimeos » 07 Nov 2024, 08:09

To get the category tree, you only need:

Code: Select all

$tree = \Aimeos\MShop::create($context, 'catalog')->getTree();
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

pla
Posts: 4
Joined: 06 Nov 2024, 11:05

Re: Get category tree in TYPO3 context

Post by pla » 07 Nov 2024, 11:29

Thank you so much. I've searched in the code and googled so much and then it's so easy. I have already tried something with a getTree() function but from the Frontend::create method:

Code: Select all

\Aimeos\Controller\Frontend::create($context, 'catalog')->getTree()
But that did not work. When using your code it works and it is that simple as well. Thanks

technoxprt
Posts: 8
Joined: 07 Mar 2018, 11:33

Re: Get category tree in TYPO3 context

Post by technoxprt » 08 Nov 2024, 06:21

To get the full tree, you can try modifying the $filter part. Instead of filtering by a specific catalog.id, set the filter to grab all categories by removing or adjusting that line. Then use $manager->getTree() to pull the full category structure.

Post Reply