Get products by category id
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!
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Get products by category id
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:
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
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();
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
Re: Get products by category id
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/
https://aimeos.org/docs/latest/frontend ... omponents/
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos,
give us a star
If you like Aimeos,

Re: Get products by category id
I added the following decorator: packages/my-theme/src/Client/Html/Common/Decorator/ProductsCategory.php
This is the code for ProductsCategory.php:
Then to test I added to packages/my-theme/templates/templates/client/html/catalog/home/body.php.
But in the frontend it shows NULL.
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 );
}
}
Code: Select all
var_dump($this->get( 'customParam'));
But in the frontend it shows NULL.
Re: Get products by category id
Don't forget the configuration:
https://aimeos.org/docs/latest/frontend ... figuration
https://aimeos.org/docs/latest/frontend ... figuration
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos,
give us a star
If you like Aimeos,

Re: Get products by category id
I was able to implement it!
Thank you very much!
Thank you very much!
Re: Get products by category id
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:
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();
Re: Get products by category id
Use the product() method of the product frontend controller:
https://github.com/aimeos/ai-controller ... #L129-L136
https://github.com/aimeos/ai-controller ... #L129-L136
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos,
give us a star
If you like Aimeos,
