How to automaticly add categories

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!
MaHo
Posts: 6
Joined: 24 Sep 2024, 08:03

How to automaticly add categories

Post by MaHo » 18 Aug 2025, 08:02

I am trying to build a cron job that automatically creates new categories and adds products to them. However, when I try to add a category, it does not get saved. The auto-increment value increases, but nothing appears in the admin UI or the database.

I currently have the following code:

Code: Select all

try {
    // Find the category if it exists
    $authorcategory = $this->catalogManager->find(
        $categoryKey,
        ['text', 'attribute', 'catalog', 'media', 'price', 'product', 'service', 'supplier', 'product/property']
    );
}
catch (\Aimeos\MShop\Exception $e) {
    // Create a new category if it doesn't exist
    $this->log('Creating new category for author ' . $categoryName);

    // Get the parent category for all automaticly created catagories
    $parentCategory = $this->catalogManager->find(
        'authors',
        ['text', 'attribute', 'catalog', 'media', 'price', 'product', 'service', 'supplier', 'product/property']
    );

    // Create the category
    $authorcategory = $this->catalogManager->create([
        'catalog.code' => $categoryKey,
        'catalog.label' => $categoryName,
        'catalog.status' => -1,
        'catalog.parentid' => $parentCategory->getId(),
        'catalog.siteid' => 1,
    ]);

    $authorcategory->setModified(); //als je niet aangeeft dat er wat is aangepast word er niks opgeslagen

    // Save the category to the database
    $authorcategory = $this->catalogManager->insert($authorcategory);
}
I cannot find what I seem to be doing wrong. If I use save() instead of insert(), it throws an error telling me I should use insert(). The documentation also seems to be lacking in this part.

Aimeos version: 2024.10.2
PHP version: 8.3.0
Running on Ubuntu 22.04.5 using docker

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

Re: How to automaticly add categories

Post by aimeos » 19 Aug 2025, 06:12

You have to use the product manager to add categories to products (not the other way round).
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MaHo
Posts: 6
Joined: 24 Sep 2024, 08:03

Re: How to automaticly add categories

Post by MaHo » 19 Aug 2025, 07:10

My apologies i got the semantics wrong. However i have no problem adding categories to products. I have problems with creating the category in the first place.

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

Re: How to automaticly add categories

Post by aimeos » 19 Aug 2025, 07:30

MaHo wrote: 18 Aug 2025, 08:02

Code: Select all

    // Create the category
    $authorcategory = $this->catalogManager->create([
        'catalog.code' => $categoryKey,
        'catalog.label' => $categoryName,
        'catalog.status' => -1,
        'catalog.parentid' => $parentCategory->getId(),
        'catalog.siteid' => 1,
    ]);

    $authorcategory->setModified(); //als je niet aangeeft dat er wat is aangepast word er niks opgeslagen

    // Save the category to the database
    $authorcategory = $this->catalogManager->insert($authorcategory);
Use instead:

Code: Select all

    $authorcategory = $this->catalogManager->create()
        ->setLabel($categoryName)
        ->setCode($categoryKey)
        ->setStatus(-1);

    $authorcategory = $this->catalogManager->insert($authorcategory, $parentCategory->getId());
Make sure you've deleted all the categories that have been created during testing because they may have a wrong siteid.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply