Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change tracking url #1182

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Extensions/Constants/ShopUrls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions src/Models/LocalizedOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class LocalizedOrder extends ModelWrapper
public $shippingProfileName = "";
/** @var int $shippingProfileId The ID of the shipping profile (Default: 0). */
public $shippingProfileId = 0;
/** @var string $trackingURL Tracking URL for the order from shipping provider. */
public $trackingURL = "";
/** @var array $trackingURLs 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 @@ -168,7 +168,7 @@ 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 Expand Up @@ -214,7 +214,7 @@ public static function wrap($order, ...$data)
unset($amount['purchasePrice']);
}
unset($amount);

if (in_array((int)$orderItem->typeId, $wrappedOrderitemTypes)) {
if ($orderItem->itemVariationId !== 0) {
$orderVariationIds[] = $orderItem->itemVariationId;
Expand Down
6 changes: 3 additions & 3 deletions src/Services/Order/Factories/OrderResultFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class OrderResultFactory
'shippingProvider' => null,
'shippingProfileName' => null,
'shippingProfileId' => 0,
'trackingURL' => '',
'trackingURLs' => [],
'paymentMethodName' => null,
'paymentMethodIcon' => null,
'paymentStatus' => 'unpaid',
Expand Down Expand Up @@ -123,11 +123,11 @@ public function fillOrderResult()
$orderResult[$key] = $value;
}
}

/** @var LocalizedOrderFaker $localizedOrderFaker */
$localizedOrderFaker = pluginApp(LocalizedOrderFaker::class);
$orderResult = $localizedOrderFaker->fill($orderResult, $variations);

return $orderResult;
}
}
6 changes: 3 additions & 3 deletions src/Services/OrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public function getOrdersCompact(int $page = 1, int $items = 50)
'status' => $orderStatusName,
'creationDate' => $creationDate,
'shippingDate' => $shippingDate,
'trackingURL' => $orderTrackingService->getTrackingURL($order, $lang),
'trackingURLs' => $orderTrackingService->getTrackingURLs($order, $lang),
'confirmationURL' => $shopUrls->orderConfirmation($order->id)
];
}
Expand Down Expand Up @@ -783,7 +783,7 @@ public function complete($order)
/**
* Remove the bundle prefix from the order item name.
* Default prefix is "[BUNDLE] "
*
*
* @param string $name
* @return string
*/
Expand All @@ -795,7 +795,7 @@ public function removeBundlePrefix(string $name): string
/**
* Remove the bundle component prefix from the order item name.
* Default prefix is "[-] "
*
*
* @param string $name
* @return string
*/
Expand Down
56 changes: 40 additions & 16 deletions src/Services/OrderTrackingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,36 @@ public function __construct(OrderRepositoryContract $orderRepo, ParcelServicePre
* Get the tracking URL 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 string
* @return array
*/
public function getTrackingURL(Order $order, $lang)
public function getTrackingURLs(Order $order, $lang): array
{
$trackingURL = '';
$trackingURLs = [];

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

if (strlen($packageNumber)) {
$trackingURL = $parcelService->trackingUrl;
$packageNumbers = $orderRepo->getPackageNumbers($order->id);

$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 = ',';
}

$zip = $order->deliveryAddress->postalCode;

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 +80,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
);
}
}