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

Enable Transit time on UPS plugin #7183

Open
danFbach opened this issue May 9, 2024 · 0 comments
Open

Enable Transit time on UPS plugin #7183

danFbach opened this issue May 9, 2024 · 0 comments

Comments

@danFbach
Copy link
Contributor

danFbach commented May 9, 2024

nopCommerce version: 4.6+

Current implementation does not return transit times for shipping quotes.
I can confirm these changes allow for transit times for both US Domestic and International shipments.

UPSService.cs function private async Task<RateRequest> CreateRateRequestAsync(GetShippingOptionRequest shippingOptionRequest, bool saturdayDelivery = false):

//set request details
var request = new RateRequest
{
    Request = new RateRequest_Request
    {
        //used to define the request type
        //Shop - the server validates the shipment, and returns rates for all UPS products from the ShipFrom to the ShipTo addresses
        RequestOption = "shoptimeintransit", //change from "shop" to "shoptimeintransit"
    }
};
request.Shipment = new RateRequest_Shipment
{
    Shipper = new Shipment_Shipper
    {
        ShipperNumber = _upsSettings.AccountNumber,
        Address = addressFrom
    },
    ShipFrom = new Shipment_ShipFrom
    {
        Address = addressFromDetails
    },
    ShipTo = new Shipment_ShipTo
    {
        Address = addressToDetails
    },
 //add DeliveryTimeInformation object
    DeliveryTimeInformation = new Shipment_DeliveryTimeInformation()
    {
        PackageBillType = "03",
        Pickup = new DeliveryTimeInformation_Pickup()
        {
            Date = DateTime.UtcNow.ToLocalTime().Date.AddDays(1).ToString("yyyyMMdd"),
        }
    }
};

Add this just before return request;

        request.Shipment.ShipmentTotalWeight = new Shipment_ShipmentTotalWeight()
        {
            UnitOfMeasurement = new ShipmentTotalWeight_UnitOfMeasurement()
            {
                Code = _upsSettings.WeightType,
                Description = _upsSettings.WeightType
            },
            Weight = request.Shipment.Package.Sum(x => decimal.TryParse(x.PackageWeight.Weight, out var wt) ? wt : 0).ToString()
        };

        var currencyCode = (await _currencyService.GetCurrencyByIdAsync(_currencySettings.PrimaryStoreCurrencyId))?.CurrencyCode;
        request.Shipment.InvoiceLineTotal = new Shipment_InvoiceLineTotal()
        {
            CurrencyCode = currencyCode,
            MonetaryValue = shippingOptionRequest.Items.Sum(x => x.Product.Price * x.GetQuantity()).ToString("F2")
        };

UPSService.cs function private async Task<IEnumerable<ShippingOption>> PrepareShippingOptionsAsync(RateResponse rateResponse) (i updated this as total transit days will be more accurate than businessDays alone, but if there are no rest days, then totalDays is null, so fallback to businessDays)

            if (serviceSummary != null)
            {
                if (!string.IsNullOrWhiteSpace(serviceSummary.EstimatedArrival.TotalTransitDays) && 
                    int.TryParse(serviceSummary.EstimatedArrival.TotalTransitDays, out var totalTransitDays))
                    transitDays = totalTransitDays;
                else if(!string.IsNullOrEmpty(serviceSummary.EstimatedArrival.BusinessDaysInTransit) &&
                    int.TryParse(serviceSummary.EstimatedArrival.BusinessDaysInTransit, out var businessDaysInTransit))
                    transitDays = businessDaysInTransit;
            }

NopRateClient.cs function public async Task<RateResponse> ProcessRateAsync(RateRequest request)
Change string.Empty to "timeintransit" for the additionalInfo parameter.

var response = await RateAsync(Guid.NewGuid().ToString(),
    _upsSettings.UseSandbox ? "testing" : UPSDefaults.SystemName, "timeintransit", "v2403", "Shop", new RATERequestWrapper
    {
        RateRequest = request
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants