Skip to content

Commit

Permalink
Fix variant/image export
Browse files Browse the repository at this point in the history
  • Loading branch information
firstred committed Apr 29, 2018
1 parent f3773ed commit 6210808
Showing 1 changed file with 131 additions and 39 deletions.
170 changes: 131 additions & 39 deletions mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,11 @@ public function displayAjaxExportAllProducts()
'error' => 'Count param missing',
]));
}
$success = $this->exportProducts(0, $idShops, $exportRemaining);
if ($exportRemaining) {
$success = $this->exportProducts(0, $idShops, $exportRemaining);
} else {
$success = $this->exportProducts(($count - 1) * static::EXPORT_CHUNK_SIZE, $idShops, $exportRemaining);
}

die(json_encode([
'success' => $success,
Expand Down Expand Up @@ -981,7 +985,11 @@ public function displayAjaxExportAllCarts()
} elseif (Tools::isSubmit('next')) {
$count = (int) Tools::getValue('count');

$success = $this->exportCarts(0, $idShops, $exportRemaining);
if ($exportRemaining) {
$success = $this->exportProducts(0, $idShops, $exportRemaining);
} else {
$success = $this->exportProducts(($count - 1) * static::EXPORT_CHUNK_SIZE, $idShops, $exportRemaining);
}

die(json_encode([
'success' => $success,
Expand Down Expand Up @@ -1020,8 +1028,12 @@ public function displayAjaxExportAllOrders()
'error' => 'Count param missing',
]));
}
$success = $this->exportOrders(0, $idShops, $exportRemaining);

if ($exportRemaining) {
$success = $this->exportProducts(0, $idShops, $exportRemaining);
} else {
$success = $this->exportProducts(($count - 1) * static::EXPORT_CHUNK_SIZE, $idShops, $exportRemaining);
}
die(json_encode([
'success' => $success,
'count' => ++$count,
Expand Down Expand Up @@ -2583,6 +2595,7 @@ protected function exportProducts($offset, $idShops = null, $remaining = false)
$idShop = $product['id_shop'];

$variants = [];
$images = [];
if ($productObj->hasAttributes()) {
$allCombinationImages = $productObj->getCombinationImages($idLang);
$dbCombinations = array_filter($productObj->getAttributeCombinations($idLang), function ($item) {
Expand Down Expand Up @@ -2681,13 +2694,23 @@ protected function exportProducts($offset, $idShops = null, $remaining = false)
]
);
} else {
// Clear the path first, remove old variants and images
if (!empty($images)) {
$payload['images'] = array_values($images);
foreach ($images as $image) {
yield $client->deleteAsync("ecommerce/stores/tbstore_{$idShop}/products/{$product['id_product']}/images/{$image['id']}");
}
}
yield $client->postAsync(
"ecommerce/stores/tbstore_{$idShop}/products",
[
'json' => $payload,
]
);
}
unset($images);
unset($variants);
unset($payload);
}
});

Expand All @@ -2712,35 +2735,57 @@ protected function exportProducts($offset, $idShops = null, $remaining = false)
return;
} catch (TransferException $e) {
// Second attempt failed, this means an error in the request, continue to make it log the error
$reason = $e;
} catch (Exception $e) {
// Second attempt failed, this means an error in the request, continue to make it log the error
$reason = $e;
}
} elseif (strtoupper($reason->getRequest()->getMethod()) === 'PATCH'
&& json_decode((string) $reason->getResponse()->getBody())->title === 'Resource Not Found'
) {
try {
// Clear the path first, remove old images and variants
$product = new Product($idProduct);
$images = $product->getImages($this->context->language->id);
if (!empty($images)) {
foreach ($images as $image) {
yield $client->deleteAsync("ecommerce/stores/tbstore_{$m['id_shop']}/products/{$idProduct}/images/{$image['id_image']}");
}
}
$client->post("ecommerce/stores/tbstore_{$m['id_shop']}/products",
[
'body' => (string) $reason->getRequest()->getBody(),
]
);

return;
} catch (TransferException $e) {
$reason = $e;
// Second attempt failed, this means an error in the request, continue to make it log the error
} catch (Exception $e) {
$reason = $e;
// Second attempt failed, this means an error in the request, continue to make it log the error
}
}

$responseBody = (string) $reason->getResponse()->getBody();
$requestBody = (string) $reason->getRequest()->getBody();
Logger::addLog(
"MailChimp client error: {$requestBody} -- {$responseBody}",
2,
$reason->getResponse()->getStatusCode(),
'MailChimpProduct',
json_decode((string) $reason->getRequest()->getBody())->id
);
if (method_exists($reason, 'getResponse') && method_exists($reason, 'getRequest')) {
$request = $reason->getRequest();
$response = $reason->getResponse();
if ($request && $response) {
$requestBody = (string) $request->getBody();
$responseBody = (string) $response->getBody();
Logger::addLog(
"MailChimp client error: {$requestBody} -- {$responseBody}",
2,
$reason->getResponse()->getStatusCode(),
'MailChimpProduct',
json_decode((string) $reason->getRequest()->getBody())->id
);

return;
}
}
Logger::addLog("MailChimp connection error: {$reason->getMessage()}", 2);
} elseif ($reason instanceof Exception || $reason instanceof TransferException) {
Logger::addLog("MailChimp connection error: {$reason->getMessage()}", 2);
}
Expand Down Expand Up @@ -2827,6 +2872,7 @@ protected function exportProductRange($range, $idShops = null, $orderDetail = fa
$idShop = $product['id_shop'];

$variants = [];
$images = [];
if ($productObj->hasAttributes()) {
$allCombinationImages = $productObj->getCombinationImages($idLang);
$dbCombinations = array_filter($productObj->getAttributeCombinations($idLang), function ($item) {
Expand Down Expand Up @@ -2966,6 +3012,9 @@ protected function exportProductRange($range, $idShops = null, $orderDetail = fa
]
);
}
unset($variants);
unset($images);
unset($payload);
}
});

Expand All @@ -2991,8 +3040,10 @@ protected function exportProductRange($range, $idShops = null, $orderDetail = fa
return;
} catch (TransferException $e) {
// Second attempt failed, this means an error in the request, continue to make it log the error
$reason = $e;
} catch (Exception $e) {
// Second attempt failed, this means an error in the request, continue to make it log the error
$reason = $e;
}
} elseif (strtoupper($reason->getRequest()->getMethod()) === 'PATCH'
&& json_decode((string) $reason->getResponse()->getBody())->title === 'Resource Not Found'
Expand All @@ -3006,20 +3057,31 @@ protected function exportProductRange($range, $idShops = null, $orderDetail = fa
return;
} catch (TransferException $e) {
// Second attempt failed, this means an error in the request, continue to make it log the error
$reason = $e;
} catch (Exception $e) {
// Second attempt failed, this means an error in the request, continue to make it log the error
$reason = $e;
}
}

$responseBody = (string) $reason->getResponse()->getBody();
$requestBody = (string) $reason->getRequest()->getBody();
Logger::addLog(
"MailChimp client error: {$requestBody} -- {$responseBody}",
2,
$reason->getResponse()->getStatusCode(),
'MailChimpProduct',
json_decode((string) $reason->getRequest()->getBody())->id
);
if (method_exists($reason, 'getResponse') && method_exists($reason, 'getRequest')) {
$request = $reason->getRequest();
$response = $reason->getResponse();
if ($request && $response) {
$requestBody = (string) $request->getBody();
$responseBody = (string) $response->getBody();
Logger::addLog(
"MailChimp client error: {$requestBody} -- {$responseBody}",
2,
$reason->getResponse()->getStatusCode(),
'MailChimpProduct',
json_decode((string) $reason->getRequest()->getBody())->id
);

return;
}
}
Logger::addLog("MailChimp connection error: {$reason->getMessage()}", 2);
} elseif ($reason instanceof Exception || $reason instanceof TransferException) {
Logger::addLog("MailChimp connection error: {$reason->getMessage()}", 2);
}
Expand Down Expand Up @@ -3158,8 +3220,10 @@ protected function exportCarts($offset, $idShops = null, $remaining = false)
return;
} catch (TransferException $e) {
// Second attempt failed, this means an error in the request, continue to make it log the error
$reason = $e;
} catch (Exception $e) {
// Second attempt failed, this means an error in the request, continue to make it log the error
$reason = $e;
}
} elseif (strtoupper($reason->getRequest()->getMethod()) === 'PATCH'
&& json_decode((string) $reason->getResponse()->getBody())->title === 'Resource Not Found'
Expand All @@ -3176,20 +3240,31 @@ protected function exportCarts($offset, $idShops = null, $remaining = false)
return;
} catch (TransferException $e) {
// Second attempt failed, this means an error in the request, continue to make it log the error
$reason = $e;
} catch (Exception $e) {
// Second attempt failed, this means an error in the request, continue to make it log the error
$reason = $e;
}
}

$responseBody = (string) $reason->getResponse()->getBody();
$requestBody = (string) $reason->getRequest()->getBody();
Logger::addLog(
"MailChimp client error: {$requestBody} -- {$responseBody}",
2,
$reason->getResponse()->getStatusCode(),
'MailChimpCart',
json_decode((string) $reason->getRequest()->getBody())->id
);
if (method_exists($reason, 'getResponse') && method_exists($reason, 'getRequest')) {
$request = $reason->getRequest();
$response = $reason->getResponse();
if ($request && $response) {
$requestBody = (string) $request->getBody();
$responseBody = (string) $response->getBody();
Logger::addLog(
"MailChimp client error: {$requestBody} -- {$responseBody}",
2,
$reason->getResponse()->getStatusCode(),
'MailChimpProduct',
json_decode((string) $reason->getRequest()->getBody())->id
);

return;
}
}
Logger::addLog("MailChimp connection error: {$reason->getMessage()}", 2);
} elseif ($reason instanceof Exception || $reason instanceof TransferException) {
Logger::addLog("MailChimp connection error: {$reason->getMessage()}", 2);
}
Expand Down Expand Up @@ -3344,7 +3419,11 @@ protected function exportOrders($offset, $idShops = null, $exportRemaining = fal
);
return;
} catch (TransferException $e) {
// Second attempt failed, this means an error in the request, continue to make it log the error
$reason = $e;
} catch (Exception $e) {
// Second attempt failed, this means an error in the request, continue to make it log the error
$reason = $e;
}
} elseif (strtoupper($reason->getRequest()->getMethod()) === 'PATCH'
&& json_decode((string) $reason->getResponse()->getBody())->title === 'Resource Not Found'
Expand All @@ -3357,19 +3436,32 @@ protected function exportOrders($offset, $idShops = null, $exportRemaining = fal
);
return;
} catch (TransferException $e) {
// Second attempt failed, this means an error in the request, continue to make it log the error
$reason = $e;
} catch (Exception $e) {
// Second attempt failed, this means an error in the request, continue to make it log the error
$reason = $e;
}
}

$responseBody = (string) $reason->getResponse()->getBody();
$requestBody = (string) $reason->getRequest()->getBody();
Logger::addLog(
"MailChimp client error: {$requestBody} -- {$responseBody}",
2,
$reason->getResponse()->getStatusCode(),
'MailChimpOrder',
json_decode((string) $reason->getRequest()->getBody())->id
);
if (method_exists($reason, 'getResponse') && method_exists($reason, 'getRequest')) {
$request = $reason->getRequest();
$response = $reason->getResponse();
if ($request && $response) {
$requestBody = (string) $request->getBody();
$responseBody = (string) $response->getBody();
Logger::addLog(
"MailChimp client error: {$requestBody} -- {$responseBody}",
2,
$reason->getResponse()->getStatusCode(),
'MailChimpProduct',
json_decode((string) $reason->getRequest()->getBody())->id
);

return;
}
}
Logger::addLog("MailChimp connection error: {$reason->getMessage()}", 2);
} elseif ($reason instanceof Exception || $reason instanceof TransferException) {
Logger::addLog("MailChimp connection error: {$reason->getMessage()}", 2);
}
Expand Down

0 comments on commit 6210808

Please sign in to comment.