Scheduler Job for anonymizing: anonymized adresses are not saved
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!
Always add your TYPO3, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Scheduler Job for anonymizing: anonymized adresses are not saved
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:
This is the code of the task class:
TYPO3 12, Aimeos 2024, PHP 8.3
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;
}
}
}
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;
}
}
Re: Scheduler Job for anonymizing: anonymized adresses are not saved
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,
give us a star
If you like Aimeos,

Re: Scheduler Job for anonymizing: anonymized adresses are not saved
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.
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.