Skip to content

Commit

Permalink
Merge pull request #1186 from plentymarkets/feat/multi_tracking_urls
Browse files Browse the repository at this point in the history
copy of #1182, make sure that new function uses its own variable
  • Loading branch information
pfrincu-plenty authored Mar 5, 2024
2 parents ac211be + 0463da9 commit 6d33746
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/Extensions/Constants/ShopUrls.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public function orderDocumentPreview($documentId, $orderId, $orderAccessKey = nu
* Get tracking URL for a specific order id.
*
* @param string|int $orderId Id of the order to get the tracking URL for.
* @return string
* @return array
*/
public function tracking($orderId)
{
Expand All @@ -343,17 +343,17 @@ public function tracking($orderId)
"tracking.{$orderId}",
function () use ($orderId, $lang) {
$authHelper = pluginApp(AuthHelper::class);
$trackingURL = $authHelper->processUnguarded(
$trackingURLs = $authHelper->processUnguarded(
function () use ($orderId, $lang) {
$orderRepository = pluginApp(OrderRepositoryContract::class);
$orderTrackingService = pluginApp(OrderTrackingService::class);

$order = $orderRepository->findOrderById($orderId);
return $orderTrackingService->getTrackingURL($order, $lang);
return $orderTrackingService->getTrackingURLs($order, $lang);
}
);

return $trackingURL;
return $trackingURLs;
}
);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Models/LocalizedOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class LocalizedOrder extends ModelWrapper
public $shippingProfileId = 0;
/** @var string $trackingURL Tracking URL for the order from shipping provider. */
public $trackingURL = "";
/** @var string $trackingURLs List of tracking URLs for the order from shipping provider. */
public $trackingURLs = "";
/** @var string $paymentMethodName Name of the payment method. */
public $paymentMethodName = "";
/** @var string $paymentMethodIcon URL of payment method icon image. */
Expand Down Expand Up @@ -169,6 +171,8 @@ public static function wrap($order, ...$data)
/** @var OrderTrackingService $orderTrackingService */
$orderTrackingService = pluginApp(OrderTrackingService::class);
$instance->trackingURL = $orderTrackingService->getTrackingURL($order, $lang);
$instance->trackingURLs = $orderTrackingService->getTrackingURLs($order, $lang);

} catch (\Exception $e) {
}

Expand Down
1 change: 1 addition & 0 deletions src/Services/Order/Factories/OrderResultFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class OrderResultFactory
'shippingProfileName' => null,
'shippingProfileId' => 0,
'trackingURL' => '',
'trackingURLs' => [],
'paymentMethodName' => null,
'paymentMethodIcon' => null,
'paymentStatus' => 'unpaid',
Expand Down
1 change: 1 addition & 0 deletions src/Services/OrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ public function getOrdersCompact(int $page = 1, int $items = 50)
'creationDate' => $creationDate,
'shippingDate' => $shippingDate,
'trackingURL' => $orderTrackingService->getTrackingURL($order, $lang),
'trackingURLs' => $orderTrackingService->getTrackingURLs($order, $lang),
'confirmationURL' => $shopUrls->orderConfirmation($order->id)
];
}
Expand Down
74 changes: 64 additions & 10 deletions src/Services/OrderTrackingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,52 @@ public function getTrackingURL(Order $order, $lang)
$orderRepo = pluginApp(OrderRepositoryContract::class);
$packageNumber = implode(',', $orderRepo->getPackageNumbers($order->id));

if (strlen($packageNumber)) {
$trackingURL = $parcelService->trackingUrl;
$trackingURL = $this->buildTrackingUrl($order, $parcelService->trackingUrl, $packageNumber, $lang);
}
} catch (\Exception $e) {
$this->getLogger(__CLASS__)->error("IO::Debug.OrderTrackingService_getTrackingURL", [
'code' => $e->getCode(),
'message' => $e->getMessage()
]);
}

return $trackingURL;
}

/**
* Get list of tracking URLs for a specific order
* @param Order $order The order to get the tracking URL for
* @param string $lang The desired language of the tracking URL (ISO-639-1)
* @return array
*/
public function getTrackingURLs(Order $order, $lang): array
{
$trackingURLs = [];

try {
$shippingProfile = $this->parcelServicePresetRepo->getPresetById($order->shippingProfileId);
$parcelService = $shippingProfile->parcelService;
if ($parcelService instanceof ParcelService) {
/** @var OrderRepositoryContract $orderRepo */
$orderRepo = pluginApp(OrderRepositoryContract::class);
$packageNumbers = $orderRepo->getPackageNumbers($order->id);

$zip = $order->deliveryAddress->postalCode;
$splitUrls = $parcelService->toArray()['splitTrackingUrl'] ?? false; // Direct property access doesn't work for some reason
$delimiter = $parcelService->toArray()['splitDelimiter'] ?? null; // Direct property access doesn't work for some reason
if (!$delimiter) {
$delimiter = ',';
}

if (strlen($trackingURL)) {
$trackingURL = $parcelService->trackingUrl;

$trackingURL = str_replace(
['[PaketNr]', '$PaketNr', '[PLZ]', '$PLZ', '[Lang]', '$Lang'],
[urlencode($packageNumber), urlencode($packageNumber), urlencode($zip), urlencode($zip), $lang, $lang],
$trackingURL
);
if ($splitUrls) {
foreach ($packageNumbers as $packageNo) {
$trackingURLs[] = $this->buildTrackingUrl($order, $trackingURL, $packageNo, $lang);
}
} else {
$packageNumber = implode($delimiter, $packageNumbers);

$trackingURLs[] = $this->buildTrackingUrl($order, $trackingURL, $packageNumber, $lang);
}
}
} catch (\Exception $e) {
Expand All @@ -78,6 +110,28 @@ public function getTrackingURL(Order $order, $lang)
]);
}

return $trackingURL;
return $trackingURLs;
}

/**
* @param Order $order
* @param string $trackingURL
* @param string $packageNumber
* @param string $lang
* @return string
*/
private function buildTrackingUrl(Order $order, string $trackingURL, string $packageNumber, string $lang): string
{
$zip = $order->deliveryAddress->postalCode;

if (!strlen($trackingURL) || !strlen($packageNumber)) {
return '';
}

return str_replace(
['[PaketNr]', '$PaketNr', '[PLZ]', '$PLZ', '[Lang]', '$Lang'],
[urlencode($packageNumber), urlencode($packageNumber), urlencode($zip), urlencode($zip), $lang, $lang],
$trackingURL
);
}
}

0 comments on commit 6d33746

Please sign in to comment.