Get products by category id

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
peter69
Posts: 95
Joined: 09 Jun 2022, 19:31

Get products by category id

Post by peter69 » 12 Jul 2022, 20:13

I want to add to the home page a carrousel of products of a specific category.

I have added the following code to packages/my-theme/templates/client/html/catalog/home/body.php:

Code: Select all

$context = $this->context();
$products = \Aimeos_ControllerFrontend::create( $context, 'product' )
->sort( 'relevance' ) // prioritize user sorting over the sorting through relevance and category
->category( [67], 'default', 1 )
->uses( ['text'] )
->search();
However it doesn't work, I get the following error: Class "\Aimeos\Base\View\Helper\Context\Standard" not available

I made the code based on some answers I have seen in topics.

What I need is to get a list of products (with their respective image, name, price) of a certain category.

Also, is it possible to get a list of categories with their respective data?

Again thank you very much for your help,

Regards

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

Re: Get products by category id

Post by aimeos » 14 Jul 2022, 06:26

The context isn't available in templates for good reasons (e.g. caching) and you are not allowed to fetch data in templates. Instead, you have to create a decorator where you add additional data to the view in the data() method:
https://aimeos.org/docs/latest/frontend ... omponents/
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
peter69
Posts: 95
Joined: 09 Jun 2022, 19:31

Re: Get products by category id

Post by peter69 » 21 Jul 2022, 05:12

I added the following decorator: packages/my-theme/src/Client/Html/Common/Decorator/ProductsCategory.php

This is the code for ProductsCategory.php:

Code: Select all

<?php

namespace Aimeos namespace Aimeos "Aimeos";

class ProductsCategory
    extends AimeosClientHtmlCatalogBase
    implements AimeosClientHtmlCommonClientFactoryIface
{

    /**
     * Sets the values of the required parameters in the view.
     *
     * @param \AimeosBaseViewFace $view The view object that generates the HTML output.
     * @param array &$tags Resulting array for the list of tags that are associated with the output.
     * @param string|null &$expire Resulting variable for the expiration date of the output (null for no expiration)
     * @return \Aimeos "View object modified".
     */
    public function data(\AimeosBaseViewIface $view, array &$tags = [], string &$expire = null ) : \AimeosBaseViewIface.
    {
        $view->customParam = 'this is a test';
        return parent::data( $view, $tags, $expire );
    }
}
Then to test I added

Code: Select all

var_dump($this->get( 'customParam'));
to packages/my-theme/templates/templates/client/html/catalog/home/body.php.

But in the frontend it shows NULL.

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

Re: Get products by category id

Post by aimeos » 21 Jul 2022, 06:03

Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
peter69
Posts: 95
Joined: 09 Jun 2022, 19:31

Re: Get products by category id

Post by peter69 » 22 Jul 2022, 05:18

I was able to implement it!

Thank you very much!

TGergo
Posts: 41
Joined: 24 Nov 2022, 14:35

Re: Get products by category id

Post by TGergo » 27 Jan 2023, 11:12

I have a simple route, controller and view on the main page.
How can I get products by category id?
I want to make carousel.
Or I can partial render CMS Cataloglist?

update:

Code: Select all

$products = \Aimeos\Controller\Frontend::create( app( 'aimeos.context' )->get(), 'product' )
                ->category( [10] )
                ->slice( 0, 3 )
                ->search();

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

Re: Get products by category id

Post by aimeos » 30 Jan 2023, 08:46

Use the product() method of the product frontend controller:
https://github.com/aimeos/ai-controller ... #L129-L136
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply