how do I connect product to catalog (category) items

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!
frimi
Posts: 15
Joined: 02 Oct 2020, 15:25

how do I connect product to catalog (category) items

Post by frimi » 17 Nov 2020, 11:04

I'm tring to create products via the product manager. Everything works fine.
=> mgr->create()
=> fill the values with the setter methods
=> addListItem for a catalog node
=> mgr->save()

The product will be stored as espected, and the category reference is also inserted (list items with domain catalog).
If i show the product in the backend, no category is added/displayed

If I load a manually created product in the backend, there is a special section 'catalog' in the bdata array...

Have anyone a hint for me, how to correctly connect catalog items with product items? I searched in forum, documetation and source code without success.
Last edited by frimi on 20 Nov 2020, 09:29, edited 2 times in total.

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

Re: how do I connect product to calalog (category) items

Post by aimeos » 18 Nov 2020, 08:26

The product <-> category relation is stored in the mshop_catalog_list table and you have to use the catalog/lists manager to add the product to a category.

Storing the relation in the mshop_product_lists table doesn't cause an error but isn't used at all.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

frimi
Posts: 15
Joined: 02 Oct 2020, 15:25

Re: how do I connect product to calalog (category) items

Post by frimi » 20 Nov 2020, 09:15

Many thanks, that helps :)
I do the job with

Code: Select all

$catItem->addListItem('product',$newCatListItem->setRefId($productId)) 
on the catalog item and it seems to work. In the backend looks all fine.

To maintain the relations between the catalog items and the product I need to test, if there are existing relations between a given category and product. If I load a linked catalog item, the listItem Array is empty!? Also testing with

Code: Select all

$catItem->getListItem('product','default',$productId) 
return always NULL

How should I do the testing?

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

Re: how do I connect product to catalog (category) items

Post by aimeos » 21 Nov 2020, 10:02

You have to tell the manager to load either load the products associated to the category using:

Code: Select all

$manager->searchItems( $filter, ['product'] )
But this can load a lot of data and can be very slow!

Instead, you can use the catalog manager to check if there already exists a relation to the product using:

Code: Select all

$filter = $manager->createSearch()->setSlice( 0, 1 );
$filter->setConditions( $filter->compare( '!=', $filter->createFunction( 'catalog:has', ['product', 'default', $prodid] ), null ) );
if( $manager->searchItems( $filter )->isEmpty() ) {
    ...
}
The second item in the array for createFunction ("default") is the list type of the relation and can also be "promotion" for top seller products.

In 2020.10, you can also write this more compact using:

Code: Select all

$filter = $manager->filter()->slice( 0, 1 );
$filter->add( [$filter->make( 'catalog:has', ['product', 'default', $prodid] ) => null], '!=' );
if( $manager->search( $filter )->isEmpty() ) {
    ...
}
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

frimi
Posts: 15
Joined: 02 Oct 2020, 15:25

Re: how do I connect product to catalog (category) items

Post by frimi » 04 Dec 2020, 15:26

Huh, it works :) Many thanks!

Now I need to delete catalog<->product relations. How can I do that?

$item->deleteListItem( 'product', $listitem );

How can i get the $listItem? As mentoined, getListItem() returns NULL
Last edited by frimi on 12 Dec 2020, 16:54, edited 2 times in total.

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

Re: how do I connect product to catalog (category) items

Post by aimeos » 06 Dec 2020, 17:58

To use deleteListItems(), all catalog<->product relations must be loaded when retrieving the catalog item. This can be extremely slow, so the better alternative is to use the catalog/lists manager directly and remove the recored based on its ID (2020.10 code):

Code: Select all

\Aimeos\MShop::create( $context, 'catalog/lists' )->deleteItem( $id );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

frimi
Posts: 15
Joined: 02 Oct 2020, 15:25

Re: how do I connect product to catalog (category) items

Post by frimi » 15 Dec 2020, 05:27

I have still problems with the deleting of the relations. If I use a known id, it works fine. But how can I find/get/retrieve the id of the relation to delete dynamicly without loading all the relations first? What I have ist the product id (item) and the catolog item.

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

Re: how do I connect product to catalog (category) items

Post by aimeos » 17 Dec 2020, 14:25

If you have the product ID, you can use this code to retrieve all catalog<->product relationships including their IDs (2020.10 LTS code):

Code: Select all

$manager = \Aimeos\MShop::create( $context, 'catalog/lists' )
$filter = $manager->filter()->add( [
	'catalog.lists.domain' => 'product',
	'catalog.lists.type' => ['default', 'promotion'],
	'catalog.lists.refid => $prodIds
] )->slice( 0, 10000 );
$manager->delete( $manager->search( $filter ) );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

frimi
Posts: 15
Joined: 02 Oct 2020, 15:25

Re: how do I connect product to catalog (category) items

Post by frimi » 26 Oct 2023, 19:06

Hello. We have updatet our Aimeos / Typo3 installation to aimeos Extension 23.10.1 on Typo3 12.4.5 and i have a lot of things not working the same way. Could it be, that the managing of the realtions between product and the catalog has changed? If i add the relation on catalog_list table as learned in the former post, the product has no catalog item in the backend and will be not displayed in the frontent. If i add a category manually, there will be added a product_list entry, that points to the catalog item an all works fine, also in the frontend. So I'm wondering, what happened and how i have to change my code. Thanks for your help and kind regards, Michael

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

Re: how do I connect product to catalog (category) items

Post by aimeos » 27 Oct 2023, 17:45

Yes, product<->category relations have moved from mshop_catalog_lists to mshop_product_lists table since 2022.x (same for suppliers). Check the changelogs when upgrading from older LTS versions: https://aimeos.org/docs/latest/changelog/2023.x/
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply