Skip to content

Commit

Permalink
Fix tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael committed Jul 30, 2024
1 parent 012a6ea commit 3bb26a7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
29 changes: 19 additions & 10 deletions ecomailemailmarketing/ecomailemailmarketing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_];
Expand Down Expand Up @@ -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 = [];

Expand All @@ -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')) {
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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');
}
Expand Down Expand Up @@ -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 = [];

Expand All @@ -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) {
Expand All @@ -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');
}
Expand Down
6 changes: 5 additions & 1 deletion ecomailemailmarketing/lib/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function bulkOrders(array $data)
*
* @return array
*/
public function buildTransaction($order): array
public function buildTransaction($order): ?array
{
$addressData = [];

Expand All @@ -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'],
Expand Down

0 comments on commit 3bb26a7

Please sign in to comment.