Scheduler Job for anonymizing: anonymized adresses are not saved

Questions around the TYPO3 integration and plugins
Forum rules
Always add your TYPO3, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
claude27
Posts: 21
Joined: 25 Jul 2024, 11:24

Scheduler Job for anonymizing: anonymized adresses are not saved

Post by claude27 » 12 May 2025, 12:43

Hi,

perhaps you can help me please.
I have created a scheduler job for anonymizing customer addresses (only payment, as events are sold and so delivery addresses are not needed) after 6 months.

The job is running but the addresses are not saved/ persisted in the database. What could be the reason?
Here is my code:

Code: Select all

<?php

namespace Unterleitner\Sitepackage\Service;

use Aimeos\MShop;
use TYPO3\CMS\Scheduler\Task\AbstractTask;

class AnonymizeOrdersService extends AbstractTask
{
    public function execute(): bool
        {
            try {
                $config = \Aimeos\Aimeos\Base::config();

                $context = \Aimeos\Aimeos\Base::context($config);

                $localeManager = \Aimeos\MShop::create($context, 'locale');
                $locale = $localeManager->bootstrap('default', 'de', 'default'); // site, lang, currency
                $context->setLocale($locale);

                $expiryDate = (new \DateTime())->modify('-6 months')->format('Y-m-d H:i:s');

                $productManager = MShop::create($context, 'product');
                $filter = $productManager->filter();
                $filter->add('product.type', '==', 'event');
                $filter->add('product.datestart', '<', $expiryDate);

                $products = $productManager->search($filter);

                $index_manager = \Aimeos\MShop::create( $context, 'index' );
                $index_manager->begin();

                $orderManager = MShop::create($context, 'order');

                foreach ($products as $product) {
                    $productId = $product->getId();

                    $orderFilter = $orderManager->filter();
                    $orderFilter->add(['order.product.productid' => $productId]);

                    $orders = $orderManager->search($orderFilter, ['order/address', 'order/product', 'order/service']);

                    if (!$orders->isEmpty()) {

                        foreach ($orders as $order) {
                            $order->setCustomerid(null);

                            $addresses = $order->getAddresses();

                            // Nur payment-Adressen anonymisieren
                            if (isset($addresses['payment'])) {
                                foreach ($addresses['payment'] as $address) {
                                    $address->setModified();
                                    $address->setFirstname('Anonym');
                                    $address->setLastname('Anonym');
                                    $address->setEmail('anonym@domain.nic');
                                    $address->setSalutation('');
                                    $address->setVatid('');
                                    $address->setCompany('');
                                    $address->setAddress1('');
                                    $address->setAddress2('');
                                    $address->setAddress3('');
                                    $address->setCity('');
                                    $address->setPostal('');
                                    $address->setState('');
                                    $address->setCountryid('');
                                    $address->setTelephone('');
                                }

                                $order->setAddresses($addresses);

                                $order->setModified();

                                $orderManager->save($order);

                            }

                        }

                    }
                }

                return true;

            } catch (\Exception $e) {
                // Optional: Logging mit TYPO3 Logger
                return false;
            }
        }
}
This is the code of the task class:

Code: Select all

<?php
namespace Unterleitner\Sitepackage\Scheduler\Task;

use TYPO3\CMS\Scheduler\Task\AbstractTask;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use Unterleitner\Sitepackage\Service\AnonymizeOrdersService;

class AnonymizeOrdersSchedulerTask extends AbstractTask
{
    public function execute(): bool
    {
        /** @var AnonymizeOrdersService $anonymizeOrdersService */
        $anonymizeOrdersService = GeneralUtility::makeInstance(AnonymizeOrdersService::class);
        $anonymizeOrdersService->execute();

        return true;
    }
}
TYPO3 12, Aimeos 2024, PHP 8.3

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

Re: Scheduler Job for anonymizing: anonymized adresses are not saved

Post by aimeos » 13 May 2025, 05:10

Don't create the index manager (you don't use it) and open a DB transaction which you don't commit afterwards. All your changes are rolled back at the end of the task.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

claude27
Posts: 21
Joined: 25 Jul 2024, 11:24

Re: Scheduler Job for anonymizing: anonymized adresses are not saved

Post by claude27 » 14 May 2025, 07:24

Thank you very much! That was the solution.
Only I eventually have to extend the script to anonymize orders when the event/product is deleted and so no longer existing (we don't want do delete the events, we will archive them, but nobody knows what can happen...). But I will find a solution, I'm sure.

Post Reply