From 02da6238983bc411a8c8604031237fa150363775 Mon Sep 17 00:00:00 2001 From: O K Date: Tue, 2 Sep 2025 19:30:44 +0300 Subject: [PATCH] add logging, optimize all customer sync, add presync --- config_uk.xml | 4 ++-- mauticconnect.php | 30 ++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/config_uk.xml b/config_uk.xml index 2f103d2..37036d4 100644 --- a/config_uk.xml +++ b/config_uk.xml @@ -2,8 +2,8 @@ mauticconnect - - + + diff --git a/mauticconnect.php b/mauticconnect.php index 3038211..0d738c9 100644 --- a/mauticconnect.php +++ b/mauticconnect.php @@ -310,7 +310,7 @@ class MauticConnect extends Module } $orderId = (int)$params['id_order']; $newStatusId = (int)$params['newOrderStatus']->id; - + $this->log(['orderId' => $orderId, 'newStatusId' => $newStatusId]); $eventHash = md5($newStatusId . '_' . $orderId); if ($this->isAlreadyProcessed($eventHash)) { return; @@ -325,7 +325,9 @@ class MauticConnect extends Module // ...call the processor method defined for this event. if (method_exists($this, $event['processor_method'])) { $this->{$event['processor_method']}($orderId, $event); + $this->markAsProcessed($eventHash); + // We break because an order status change should only trigger one event. break; } @@ -492,6 +494,7 @@ class MauticConnect extends Module */ public function getOauth2RedirectUri() { + return $this->context->link->getModuleLink($this->name, 'oauth2', [], true); } @@ -615,8 +618,10 @@ class MauticConnect extends Module if (!$this->isConnected()) { return false; } + $this->log(['newCustomer' => $params['newCustomer']]); if (isset($params['newCustomer']) && Validate::isLoadedObject($params['newCustomer'])) { + $this->syncCustomer($params['newCustomer']); } } @@ -640,9 +645,12 @@ class MauticConnect extends Module { return (bool)Configuration::get(self::MAUTIC_ACCESS_TOKEN); } - public function syncAllCustomers() + public function syncAllCustomers(bool $full = false) { $customers = new PrestaShopCollection('Customer'); + if (!$full) { + $customers->where('date_add', '>', date("Y-m-d H:i:s", time() - 60 - 60 * 24 * 2)); + } foreach ($customers as $customer) { $this->syncCustomer($customer); } @@ -819,13 +827,14 @@ class MauticConnect extends Module if (!$mauticSegmentId || !$mauticTemplateId) { return; } + // 2. Get all necessary objects $order = new Order($id_order); $customer = new Customer((int)$order->id_customer); $currency = new Currency((int)$order->id_currency); $carrier = new Carrier((int)$order->id_carrier); $link = new Link(); // Needed for generating image URLs - + $this->syncCustomer($customer); // 3. Gather primary data $customer_email = $customer->email; if (!$this->isContactInSegment($customer_email, $mauticSegmentId)) { @@ -927,7 +936,7 @@ class MauticConnect extends Module $currency = new Currency((int)$order->id_currency); $carrier = new Carrier((int)$order->id_carrier); $link = new Link(); // Needed for generating image URLs - + $this->syncCustomer($customer); // 3. Gather primary data $customer_email = $customer->email; if (!$this->isContactInSegment($customer_email, $mauticSegmentId)) { @@ -1087,4 +1096,17 @@ class MauticConnect extends Module 'date_add' => date('Y-m-d H:i:s'), ], false, true, Db::INSERT_IGNORE); // INSERT IGNORE is a safe way to prevent errors on race conditions } + public function log(array $data) + { + + $data = json_encode($data, JSON_UNESCAPED_UNICODE); + + $logdirectory = _PS_ROOT_DIR_ . '/var/modules/' . $this->name . '/logs/' . date("Y") . '/' . date("m") . '/' . date("d") . '/'; + if (!is_dir($logdirectory)) { + mkdir($logdirectory, 0750, true); + } + $logger = new \FileLogger(0); //0 == debug level, logDebug() won’t work without this. + $logger->setFilename($logdirectory . 'dayly.log'); + $logger->logInfo($data); + } }