nopSolutions/nopCommerce · error · NopException

Order cannot be loaded

Error message

Order cannot be loaded

What it means

Thrown inside SetTrackingAsync when the nopCommerce order associated with the shipment cannot be loaded. The order ID is taken from shipment.OrderId (or 0 if the shipment is null) and passed to GetOrderByIdAsync. A null return means no order exists with that ID, making it impossible to look up the PayPal order reference or verify the payment method.

Source

Thrown at src/Plugins/Nop.Plugin.Payments.PayPalCommerce/Services/PayPalCommerceServiceManager.cs:2916

    /// <param name="settings">Plugin settings</param>
    /// <param name="shipment">Shipment</param>
    /// <returns>
    /// A task that represents the asynchronous operation
    /// The task result contains the operation result; error message if exists
    /// </returns>
    public async Task<(bool Result, string Error)> SetTrackingAsync(PayPalCommerceSettings settings, Shipment shipment)
    {
        return await HandleFunctionAsync(async () =>
        {
            if (!IsConfigured(settings))
                throw new NopException("Plugin not configured");

            var carrier = await _genericAttributeService.GetAttributeAsync<string>(shipment, PayPalCommerceDefaults.ShipmentCarrierAttribute);
            if (string.IsNullOrEmpty(carrier))
                return false;

            var nopOrder = await _orderService.GetOrderByIdAsync(shipment?.OrderId ?? 0)
                ?? throw new NopException("Order cannot be loaded");

            if (!string.Equals(nopOrder.PaymentMethodSystemName, PayPalCommerceDefaults.SystemName, StringComparison.InvariantCultureIgnoreCase))
                return false;

            var customValues = new CustomValues();
            customValues.FillByXml(nopOrder.CustomValuesXml);
            var orderIdKey = await _localizationService.GetResourceAsync("Plugins.Payments.PayPalCommerce.Order.Id");
            if (!customValues.TryGetValue(orderIdKey, out var orderIdValue))
                throw new NopException("Failed to get PayPal order info");

            var order = await _httpClient
                .RequestAsync<GetOrderRequest, GetOrderResponse>(new GetOrderRequest { OrderId = orderIdValue.Value }, settings) as Order;
            if (order.Status?.ToUpper() != OrderStatusType.COMPLETED.ToString())
                throw new NopException($"Unable to assign tracking information to orders in {order.Status} status");

            if (order.PurchaseUnits?.FirstOrDefault() is not PurchaseUnit unit || unit.Shipping is null)
                throw new NopException("No shipping info found for PayPal order");

View on GitHub (pinned to 64bdf2ff08)

Solutions

  1. Verify the shipment's OrderId references a valid order: SELECT * FROM [Order] WHERE Id = <shipment.OrderId>
  2. If the order was deleted, remove the orphaned shipment or mark it as processed without PayPal sync
  3. Add a null-check on the shipment parameter before calling SetTrackingAsync
  4. Ensure shipment creation and order creation are in the same transaction to prevent orphans

Example fix

// before
var (result, error) = await _serviceManager.SetTrackingAsync(settings, shipment);

// after — verify order exists before calling
if (shipment is null)
    return;
var order = await _orderService.GetOrderByIdAsync(shipment.OrderId);
if (order is null)
{
    _logger.Warning($"Shipment {shipment.Id} references missing order {shipment.OrderId}");
    return;
}
var (result, error) = await _serviceManager.SetTrackingAsync(settings, shipment);
Defensive patterns

Strategy: validation

Validate before calling

// Verify shipment and its order exist before calling SetTrackingAsync
if (shipment is null)
    return;
var order = await _orderService.GetOrderByIdAsync(shipment.OrderId);
if (order is null)
{
    _logger.Warning($"Shipment {shipment.Id} has no associated order; skipping tracking sync");
    return;
}

Try / catch

// HandleFunctionAsync catches internally; check the returned error tuple
var (result, error) = await _serviceManager.SetTrackingAsync(settings, shipment);
if (!string.IsNullOrEmpty(error))
    _logger.Warning($"Tracking sync failed for shipment {shipment.Id}: {error}");

Prevention

When it happens

Trigger: SetTrackingAsync is called with a shipment whose OrderId does not correspond to any order in the database, or the shipment parameter itself is null causing OrderId to default to 0.

Common situations: The order was deleted but the shipment record remains (orphaned shipment); shipment was created in a transaction that was later rolled back but the shipment record persisted; data import created shipments without matching orders; race condition where shipment is processed before the order is committed.

Related errors


AI-assisted analysis of nopSolutions/nopCommerce@64bdf2ff08 (2026-08-13). Data as JSON: /api/errors/12ab333fe2fcfcbd. Report an issue: GitHub.