From 3bb26a767585be0e567ee4aa938e88095338d4e3 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 30 Jul 2024 10:58:56 +0200 Subject: [PATCH] Fix tracking --- .../ecomailemailmarketing.php | 29 ++++++++++++------- ecomailemailmarketing/lib/api.php | 6 +++- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/ecomailemailmarketing/ecomailemailmarketing.php b/ecomailemailmarketing/ecomailemailmarketing.php index 44e5766..53b4d88 100644 --- a/ecomailemailmarketing/ecomailemailmarketing.php +++ b/ecomailemailmarketing/ecomailemailmarketing.php @@ -30,7 +30,7 @@ public function __construct() $this->module_key = '3c90ebaffe6722aece11c7a66bc18bec'; $this->name = 'ecomailemailmarketing'; $this->tab = 'emailing'; - $this->version = '2.0.18'; + $this->version = '2.0.19'; $this->author = 'Ecomail'; $this->need_instance = 0; $this->ps_versions_compliancy = ['min' => '1.7.0.0', 'max' => _PS_VERSION_]; @@ -806,7 +806,7 @@ public function createWebserviceKey(): bool public function syncCustomers(string $listId, int $offset = 0, bool $forceHttp = false): void { - $allCustomers = $this->requestGet(sprintf('%s%sapi/', $forceHttp ? Tools::getShopDomain(true) : Tools::getShopDomainSsl(true), __PS_BASE_URI__), 'customers', $offset, 3000); + $allCustomers = $this->requestGet(sprintf('%s%sapi/', $forceHttp ? Tools::getShopDomain(true) : Tools::getShopDomainSsl(true), __PS_BASE_URI__), 'customers', $offset, 100); $customersToImport = []; @@ -825,6 +825,10 @@ public function syncCustomers(string $listId, int $offset = 0, bool $forceHttp = PrestaShopLogger::addLog(sprintf('Customers count: %s', count($allCustomers['customers']))); foreach ($allCustomers['customers'] as $customer) { + if (!isset($customer['email']) || !$customer['email']) { + continue; + } + $groupTags = []; if (Configuration::get('ECOMAIL_LOAD_GROUP')) { @@ -881,7 +885,7 @@ public function syncCustomers(string $listId, int $offset = 0, bool $forceHttp = $result = $this->getAPI()->subscribeToList($listId, $firstCustomer); if (isset($result->errors)) { - PrestaShopLogger::addLog('Ecomail failed: ' . json_encode($result), 1, null, 'subscribeToList', null, true); + PrestaShopLogger::addLog('Ecomail failed: ' . json_encode($result->errors), 1, null, 'subscribeToList', null, true); } } @@ -891,12 +895,12 @@ public function syncCustomers(string $listId, int $offset = 0, bool $forceHttp = $result = $this->getAPI()->bulkSubscribeToList($listId, $customersToImport); if (isset($result->errors)) { - PrestaShopLogger::addLog('Ecomail failed: ' . json_encode($result), 1, null, 'bulkSubscribeToList', null, true); + PrestaShopLogger::addLog('Ecomail failed: ' . json_encode($result->errors), 1, null, 'bulkSubscribeToList', null, true); } } - if (count($allCustomers['customers']) === 3000) { - $this->syncCustomers($listId, $offset + 3000, $forceHttp); + if (count($allCustomers['customers']) === 100) { + $this->syncCustomers($listId, $offset + 100, $forceHttp); } else { PrestaShopLogger::addLog('Customers imported'); } @@ -924,7 +928,7 @@ public function requestGet(string $url, string $event, int $offset, int $limit): public function syncOrders(int $offset = 0, bool $forceHttp = false): void { - $allOrders = $this->requestGet(sprintf('%s%sapi/', $forceHttp ? Tools::getShopDomain(true) : Tools::getShopDomainSsl(true), __PS_BASE_URI__), 'orders', $offset, 1000); + $allOrders = $this->requestGet(sprintf('%s%sapi/', $forceHttp ? Tools::getShopDomain(true) : Tools::getShopDomainSsl(true), __PS_BASE_URI__), 'orders', $offset, 100); $ordersToImport = []; @@ -948,6 +952,11 @@ public function syncOrders(int $offset = 0, bool $forceHttp = false): void } $transaction = $this->getAPI()->buildTransaction($order); + + if (!$transaction) { + continue; + } + $transactionItems = []; foreach ($order['associations']['order_rows'] as $item) { @@ -966,12 +975,12 @@ public function syncOrders(int $offset = 0, bool $forceHttp = false): void $result = $this->getAPI()->bulkOrders($ordersToImport); if (isset($result->errors)) { - PrestaShopLogger::addLog('Ecomail failed: ' . json_encode($result), 1, null, 'bulkOrders', null, true); + PrestaShopLogger::addLog('Ecomail failed: ' . json_encode($result->errors), 1, null, 'bulkOrders', null, true); } } - if (count($allOrders['orders']) === 1000) { - $this->syncOrders($offset + 1000, $forceHttp); + if (count($allOrders['orders']) === 100) { + $this->syncOrders($offset + 100, $forceHttp); } else { PrestaShopLogger::addLog('Orders imported'); } diff --git a/ecomailemailmarketing/lib/api.php b/ecomailemailmarketing/lib/api.php index 7988119..a51e2ee 100644 --- a/ecomailemailmarketing/lib/api.php +++ b/ecomailemailmarketing/lib/api.php @@ -184,7 +184,7 @@ public function bulkOrders(array $data) * * @return array */ - public function buildTransaction($order): array + public function buildTransaction($order): ?array { $addressData = []; @@ -210,6 +210,10 @@ public function buildTransaction($order): array $order = (array) $order; } + if (!isset($customer->email) || !$customer->email) { + return null; + } + return array_merge( [ 'order_id' => 'presta_' . $order['id'],