How to automaticly add categories
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!
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
How to automaticly add categories
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:
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
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);
}
Aimeos version: 2024.10.2
PHP version: 8.3.0
Running on Ubuntu 22.04.5 using docker
Re: How to automaticly add categories
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,
give us a star
If you like Aimeos,

Re: How to automaticly add categories
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.
Re: How to automaticly add categories
Use instead:MaHo wrote: ↑18 Aug 2025, 08:02Code: 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);
Code: Select all
$authorcategory = $this->catalogManager->create()
->setLabel($categoryName)
->setCode($categoryKey)
->setStatus(-1);
$authorcategory = $this->catalogManager->insert($authorcategory, $parentCategory->getId());
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos,
give us a star
If you like Aimeos,
