Catalog Manager - Get child catalog items ?

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
kdim95
Advanced
Posts: 208
Joined: 26 Aug 2022, 12:17

Catalog Manager - Get child catalog items ?

Post by kdim95 » 13 Jan 2023, 10:16

Laravel framework version: 9.47.0
Aimeos Laravel version: ~2022.10
PHP Version: 8.2.1
Environment: Linux

Hello,

What's the best way to get the child catalog items in Catalog Manager search results?

My code:

Code: Select all

$catalog_manager = \Aimeos\MShop::create( $this->context, 'catalog' );
$catalog_items = $catalog_manager->search( $catalog_filter ); // Catalog children missing here
I want to be able to use getChildren() on each item inside $catalog_items, but the result is always empty.

kdim95
Advanced
Posts: 208
Joined: 26 Aug 2022, 12:17

Re: Catalog Manager - Get child catalog items ?

Post by kdim95 » 13 Jan 2023, 11:09

I came up with this, since the items have a catalog.parentid.
The function gets the catalog items which have a matching catalog.parentid.

Code: Select all

$catalog_manager = \Aimeos\MShop::create( $this->context, 'catalog' );
$catalog_items = $catalog_manager->search( $catalog_filter );

$catalog_item_children = getCatalogChildren( $catalog_items, $catalog_items->first() );

function getCatalogChildren( $catalog_items, $catalog_item )
{
	return $catalog_items->filter( function($value, $key) use($catalog_item) {
		return $value->getParentId() == $catalog_item->getId();
	} );
}

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

Re: Catalog Manager - Get child catalog items ?

Post by aimeos » 13 Jan 2023, 14:59

The getTree() method is the only one that fetches the children and returns a tree structure:
https://github.com/aimeos/aimeos-core/b ... hp#L33-L43

Use the frontend controller if you need to fetch categories for the frontend:
https://github.com/aimeos/ai-controller ... hp#L73-L80
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply