Add global scope on retrieving products

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!
User avatar
GagDalakyan
Posts: 17
Joined: 03 Dec 2020, 13:13

Add global scope on retrieving products

Post by GagDalakyan » 02 Aug 2021, 07:25

Laravel : 8.4
Aimeos : 2021.04
PHP Version : 7.4
Environment : Windows

Good day. I need to add global scope for products in frontend ( Product lists, detail page, bought together products, etc. ) , for example

Code: Select all

 $productManager->compare( '==', 'product.config', 'visibility":"1' ); 
Can anyone help ?

I see there is a similar global scope on product status in frontend manager ( product.status = 1 ).
I need to add something similar to this to add my scope.
Thanks a lot!

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

Re: Add global scope on retrieving products

Post by aimeos » 03 Aug 2021, 13:32

The easiest solution is to add a mshop_product.visibility column and extend the product manager with a decorator to make the column known to Aimeos:
https://aimeos.org/docs/latest/models/extend-managers/
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
GagDalakyan
Posts: 17
Joined: 03 Dec 2020, 13:13

Re: Add global scope on retrieving products

Post by GagDalakyan » 03 Aug 2021, 15:11

Thanks for reply!
Im sorry.I asked wrong question.
I need to show in frontend for example only products where ( '>', 'product.rating', '2' );
Rating column is already in mshop_product table. How i can add this rule in all queries?

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

Re: Add global scope on retrieving products

Post by aimeos » 04 Aug 2021, 06:01

Code: Select all

filter[>][product.rating]=2
// escaped
filter[%3E][product.rating]=2
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
GagDalakyan
Posts: 17
Joined: 03 Dec 2020, 13:13

Re: Add global scope on retrieving products

Post by GagDalakyan » 04 Aug 2021, 06:09

And where add this filter ?

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

Re: Add global scope on retrieving products

Post by aimeos » 04 Aug 2021, 06:13

Sorry, that was for the JSON API. When using the product manager directly, you should use:

Code: Select all

$filter = $manager->filter( true )->add( 'product.rating', '>', 2 );
$items = $manager->search( $filter );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
GagDalakyan
Posts: 17
Joined: 03 Dec 2020, 13:13

Re: Add global scope on retrieving products

Post by GagDalakyan » 04 Aug 2021, 08:56

Thanks for your reply.
I understand. But i want to know is there a way, to add it global to product manager class( as product status is always set to 1 ), not every time add this filter while using product manager.

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

Re: Add global scope on retrieving products

Post by aimeos » 05 Aug 2021, 05:49

Create a new product manager that extends from the original one, overwrite the filter() method and add your condition there:
https://github.com/aimeos/aimeos-core/b ... #L280-L328
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
GagDalakyan
Posts: 17
Joined: 03 Dec 2020, 13:13

Re: Add global scope on retrieving products

Post by GagDalakyan » 05 Aug 2021, 07:15

Many thanks.
And my last question. ))
This works fine, but does not affect to similar products and products bought together in product detail. Any suggestions ?

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

Re: Add global scope on retrieving products

Post by aimeos » 05 Aug 2021, 15:19

You have to overwrite the isAvailable() method of the product item too:
https://github.com/aimeos/aimeos-core/b ... #L444-L449

See also:
https://aimeos.org/docs/latest/models/e ... custom-way
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply