order Desc in products query

How to configure and adapt Aimeos based shops as developer
Forum rules
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Oscar
Posts: 13
Joined: 16 Jul 2021, 14:51

order Desc in products query

Post by Oscar » 25 Aug 2021, 18:24

Hello plz how can order the products in a descending order like order by product.id "DESC" in this query?

$products = $this->homeTree->getRefItems( 'product', null, 'promotion', )->slice(0,6);

and other question plz:

$this->homeTree is the manager?

thx

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

Re: order Desc in products query

Post by aimeos » 26 Aug 2021, 05:52

Oscar wrote: 25 Aug 2021, 18:24 Hello plz how can order the products in a descending order like order by product.id "DESC" in this query?

Code: Select all

$products = $this->homeTree->getRefItems( 'product', null, 'promotion', )->slice(0,6);
You can't because the referenced products are always ordered by the position you can set in the category panel. If you add a decorator that uses the product controller directly, you can execute a query with a custom order. The disadvantage is that you will fetch the products again and most likely not in an optimized way so your home page will slow down.
Oscar wrote: 25 Aug 2021, 18:24 $this->homeTree is the manager?
No, it's the root tree node of the category tree.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Oscar
Posts: 13
Joined: 16 Jul 2021, 14:51

Re: order Desc in products query

Post by Oscar » 26 Aug 2021, 12:24

Could be a search with filter? And then aply to the filter a -product.id
Thx for help :P

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

Re: order Desc in products query

Post by aimeos » 27 Aug 2021, 06:31

Yes, if you use the product controller, you can do that.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Oscar
Posts: 13
Joined: 16 Jul 2021, 14:51

Re: order Desc in products query

Post by Oscar » 29 Aug 2021, 17:45

Hello, I have tried to do a search ordering them descendingly and I have been able, but how could I filter only the promotional ones?
Thank you very much for the help. This is my code

Code: Select all

$context = app()->make('\Aimeos\Shop\Base\Context')->get();
$manager = \Aimeos\MShop::create( $context, 'product' );				
$filter = $manager->filter();
$filter = $manager->filter( true );
$filter->order( '-product.id' );
$items = $manager->search( $filter);

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

Re: order Desc in products query

Post by aimeos » 31 Aug 2021, 05:53

You should use the product controller instead of the manager:
https://github.com/aimeos/ai-client-htm ... #L317-L321

Code: Select all

$context = app()->make('aimeos.context')->get();
$items = \Aimeos\Controller\Frontend::create($context, 'product')
	->category( $catId, 'promotion' )
	->sort( '-product.id' )->slice( 0, 6 )
	->uses( ['media', 'price', 'text'] )
	->search();
Available methods are:
https://github.com/aimeos/ai-controller ... /Iface.php
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Oscar
Posts: 13
Joined: 16 Jul 2021, 14:51

Re: order Desc in products query

Post by Oscar » 23 Sep 2021, 06:52

Thanks a lot!! runs perfect :)

Post Reply