want to list child categories

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!
User avatar
mahammadareef
Posts: 54
Joined: 14 Oct 2022, 11:54

want to list child categories

Post by mahammadareef » 15 Dec 2022, 10:00

I wanted to show the next child categories lists of parent categories in the catalog List page when the parent category is active in catalog List page .............
please explain to me with steps to fulfill this ...

for checking, I am uploading an image ...

Thanks in advance ...
Screenshot 2022-12-15 152246.jpg
Screenshot 2022-12-15 152246.jpg (172.51 KiB) Viewed 1677 times

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

Re: want to list child categories

Post by aimeos » 19 Dec 2022, 08:24

You can use code similar to this one.

The first is a decorator for the catalog list component you have to place in ./src/Client/Html/Common/Decorator/ directory:

Code: Select all

<?php

namespace Aimeos\Client\Html\Common\Decorator;

class ListCategories
	extends \Aimeos\Client\Html\Common\Decorator\Base
	implements \Aimeos\Client\Html\Common\Decorator\Iface
{
	public function addData( \Aimeos\Base\View\Iface $view, array &$tags = array(), string &$expire = null ) : \Aimeos\Base\View\Iface
	{
		$view = parent::addData( $view, $tags, $expire );

		$view->listNode = \Aimeos\Controller\Frontend::create( $this->context(), 'catalog' )
			->uses( ['media', 'text'] )->root( $view->param( 'f_catid' ) )
			->getTree( \Aimeos\Controller\Frontend\Catalog\Iface::LIST );

		return $view;
	}
}
Then, you have to configure the decorator in the ./config/client.php file of your extension:

Code: Select all

    'html' => [
        'catalog' => [
            'lists' => [
                'decorators' => [
                    'global' => ['ListCategories']
                ],
            ],
        ],
    ],
Finally, render the categories in the catalog/list/body.php template.

Code: Select all

<?php if( isset( $this->listNode ) ) : ?>
        <?php foreach( $this->listNode->getChildren() as $item ) : ?>
                <a class="cat-item" href="<?= $enc->attr( $this->url( ( $item->getTarget() ?: $target ), $controller, $action, $params, [], $config ) ); ?>">
                        <?php if( $mediaItem = $item->getRefItems( 'media', 'default', 'default' )->first() ) : ?>
                                <img src="<?= $enc->attr( $mediaItem->getPreview() ) ?>" >
                        <?php endif ?>
                        <span class="cat-name"><?= $enc->html( $item->getName(), $enc::TRUST ); ?></span>
                </a>
        <?php endforeach; ?>
<?php endif ?>
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
PedroLópezAndradas
Posts: 22
Joined: 16 Jan 2023, 11:30

Re: want to list child categories

Post by PedroLópezAndradas » 25 Jan 2023, 11:00

Thank you for your help and support in this forum :)

I'm trying to get same functionallity,but in common/partials/products.php

Where do I have to register my decorator?

Code: Select all

	'html' => [
		'common' => [
			'partials' => [
				'decorators' => [
					'global' => ['ListCategories']
				],
			],
		],
	],

User avatar
PedroLópezAndradas
Posts: 22
Joined: 16 Jan 2023, 11:30

Re: want to list child categories

Post by PedroLópezAndradas » 26 Jan 2023, 12:46

I have tried to replicate this example in catalog/lists, and I'm unable to achieve it.

It is like if the decorator is not being used. Do we need to register somewhere else than in client.php config file on our extension?

Code: Select all

<?php

return [
	'html' => [
		'themes' => [
			'xxxx' => 'xxxx',
		],
		'theme-presets' => [
			'xxxx' => [
				'--ai-product-image-ratio' => '3/4',
				'--ai-bg' => '#FFFFFF',
				'--ai-bg-alt' => '#F6F6F6',
				'--ai-primary' => '#FFF000',
				'--ai-primary-alt' => '#282828',
				'--ai-secondary' => '#555555',
				'--ai-secondary-alt' => '#555555',
				'--ai-tertiary' => '#CCCCCC',
				'--ai-tertiary-alt' => '#F6F6F6',
				'--ai-danger' => '#A00000',
				'--ai-success' => '#006000',
				'--ai-warning' => '#FFA500',
				'--ai-radius' => '0',
			]
		],
		'catalog' => [
            		'lists' => [
                		'decorators' => [
                    			'global' => ['ListCategories']
                		],
            		],
        	],
	],
	'jsonapi' => [
	],	
];


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

Re: want to list child categories

Post by aimeos » 26 Jan 2023, 13:26

You still have to register the decorator for the catalog/lists component but you want to use the data in a partial so you have to pass the new data to the partial too:
https://github.com/aimeos/ai-client-htm ... #L148-L157
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
PedroLópezAndradas
Posts: 22
Joined: 16 Jan 2023, 11:30

Re: want to list child categories

Post by PedroLópezAndradas » 30 Jan 2023, 08:34

Yes, but I'm having problems getting params from the view in the first example, directly in templates/client/html/catalog/lists/body.

My decorator:

Code: Select all

<?php

namespace Aimeos\Client\Html\Common\Decorator;

class ListCategories
	extends \Aimeos\Client\Html\Common\Decorator\Base
	implements \Aimeos\Client\Html\Common\Decorator\Iface
{
	public function addData( \Aimeos\Base\View\Iface $view, array &$tags = array(), string &$expire = null ) : \Aimeos\Base\View\Iface
	{
		$view = parent::addData( $view, $tags, $expire );
		$view->set('test', 'hello');
		return $view;
	}

	public function getCat(){
		return 'ey!';
	}
}
My config:

Code: Select all

<?php

return [
	'html' => [
		[...]
       	 	'catalog' => [
            		'lists' => [
                		'decorators' => [
                    			'global' => ['ListCategories'],
                		],
            		],
        	],		
	],
	'jsonapi' => [
	],	
];

My template client/html/catalog/lists/body.php:

Code: Select all

		<?php 
			//Testing decorator
			var_dump( $this->get( 'test' ) ); 	//Returns NULL
			var_dump( $this->getCat() ); 	//Returns ERROR		
		?>

I think I'm missing something important, because it is like if decorator is not being detected.

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

Re: want to list child categories

Post by aimeos » 31 Jan 2023, 09:13

The example code for ListCategories is for Aimeos 2021.x, in 2022.x the method has been renamed from "addData()" to "data()" only.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
PedroLópezAndradas
Posts: 22
Joined: 16 Jan 2023, 11:30

Re: want to list child categories

Post by PedroLópezAndradas » 31 Jan 2023, 09:26

Yes! It works!

Thank you!

Now I have an error taking the url of the children categories, but I'll be able to figure out why.

Thanks!

Post Reply