Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • James Dimmer 12 posts 155 karma points
    Aug 31, 2017 @ 16:05
    James Dimmer
    0

    Saving Shipping Address during Custom Checkout Stage

    Hi,

    I am using the Custom Stage in the checkout process as I am to do the checkout process from a single page. Using the following code I am struggling on how I should be setting the Shipping Address that is displayed on the sale/invoice in the back end.

    Can someone point me in the right direction?

    IBasket basket = CurrentCustomer.Basket();
    ICheckoutManagerBase cm = basket.GetCheckoutManager();
    ICheckoutPaymentManager payment = cm.Payment;
    ICheckoutShippingManager shipping = cm.Shipping;
    
    // Check if we have any items in the basket
    if (basket.Items.Any())
    {
        // Set Billing Address
        Address add = new Address();
        add.Name = Request.QueryString["contact-name"].ToString();
        add.Organization = Request.QueryString["company-name"].ToString();
        add.AddressType = AddressType.Billing;
        add.Address1 = Request.QueryString["reg-address-1"].ToString();
        add.Address2 = Request.QueryString["reg-address-2"].ToString();
        //add.Locality = Request.QueryString["reg-address-town"].ToString();
        //add.Region = Request.QueryString["reg-address-region"].ToString();
        add.PostalCode = Request.QueryString["reg-address-postcode"].ToString();
        add.CountryCode = Request.QueryString["reg-address-country"].ToString();
        add.Phone = Request.QueryString["landline-number"].ToString();
        cm.Customer.SaveBillToAddress(add);
    
        // Set Shipping Address
        Address shipAdd = add;
        shipAdd.AddressType = AddressType.Shipping;
        shipAdd.Address1 = Request.QueryString["del-address-1"].ToString();
        if (string.IsNullOrEmpty(shipAdd.Address1)) { shipAdd.Address1 = add.Address1; }
        shipAdd.Address2 = Request.QueryString["del-address-2"].ToString();
        if (string.IsNullOrEmpty(shipAdd.Address2)) { shipAdd.Address2 = add.Address2; }
        //shipAdd.Locality = Request.QueryString["del-address-town"].ToString();
        //if (string.IsNullOrEmpty(shipAdd.Locality)) { shipAdd.Locality = add.Locality; }
        //shipAdd.Region = Request.QueryString["del-address-region"].ToString();
        //if (string.IsNullOrEmpty(shipAdd.Region)) { shipAdd.Region = add.Region; }
        shipAdd.PostalCode = Request.QueryString["del-address-postcode"].ToString();
        if (string.IsNullOrEmpty(shipAdd.PostalCode)) { shipAdd.PostalCode = add.PostalCode; }
        shipAdd.CountryCode = Request.QueryString["del-address-country"].ToString();
        if (string.IsNullOrEmpty(shipAdd.CountryCode)) { shipAdd.CountryCode = add.CountryCode; }
        cm.Customer.SaveShipToAddress(shipAdd);
    
        basket.Save();
    
        // Create invoice for the checkout
        IInvoiceService service = MerchelloContext.Current.Services.InvoiceService;
        IInvoice invoice = payment.PrepareInvoice();
    
        // Set Shipping Address (attempt 2) - quote is always null
        IShipment shipment = basket.PackageBasket(shipAdd).FirstOrDefault();
    
        IShippingContext shippingContext = MerchelloContext.Current.Gateways.Shipping;
        IShipmentRateQuote quote = shippingContext.GetShipRateQuotesForShipment(shipment, true).FirstOrDefault();
        shipping.SaveShipmentRateQuote(quote);
    
        // Generate a 'random' invoice number prefixed by company name
        Random random = new Random();
        invoice.InvoiceNumber = random.Next();
        invoice.InvoiceNumberPrefix = add.Organization;
        service.Save(invoice);
    
        // Add account number as a note
        INoteService noteService = MerchelloContext.Current.Services.NoteService;
        INote accNo = noteService.CreateNote(invoice.Key, EntityType.Invoice, Request.QueryString["account-no"].ToString());
        accNo.Author = "Account Number";
        accNo.CreateDate = DateTime.Now;
        noteService.Save(accNo);
        invoice.Notes = new List<INote>() { accNo };
        service.Save(invoice);
    
        // Send confirmation emails process
        CustomerContext context = new CustomerContext(UmbracoContext);
        context.SetValue("invoiceKey", invoice.Key.ToString());
    
        IEnumerable<GatewayProviderBase> providers = MerchelloContext.Current.Gateways.Notification.GetAllProviders();
    
        IGatewayProviderService providerService = providers.First().GatewayProviderService;
    
        IEnumerable<IGatewayProviderSettings> settings = providerService.GetGatewayProvidersByType(GatewayProviderType.Notification);
    
        IEnumerable<INotificationMethod> methods = providerService.GetNotificationMethodsByProviderKey(settings.First().Key);
    
        IEnumerable<INotificationMessage> messages = providerService.GetNotificationMessagesByMethodKey(methods.First().Key).Where(e => e.Name == "OrderConfirmation");
    
        INotificationMessage email = new NotificationMessage(methods.First().Key, CurrentPage.customerConfirmationSubject, "[email protected]");
        email.Name = CurrentPage.customerConfirmationSubject;
        email.Recipients = Request.QueryString["email-address"].ToString();
    
        string emailText = CurrentPage.customerConfirmationEmail.ToString();
        emailText = emailText.Replace("[[name]]", Request.QueryString["contact-name"].ToString());
    
        string items = "<ul>";
        foreach (ILineItem item in basket.Items)
        {
            items += "<li>" + item.Name + "</li>";
        }
        items += "</ul>";
    
        emailText = emailText.Replace("[[items]]", items);
        emailText = emailText.Replace("[[total]]", basket.TotalBasketPrice.AsFormattedCurrency());
        email.BodyText = emailText;
    
        MerchelloContext.Current.Gateways.Notification.Send(email);
    
        INotificationMessage adminEmail = new NotificationMessage(methods.First().Key, CurrentPage.adminConfirmationSubject, "[email protected]");
        adminEmail.Name = CurrentPage.adminConfirmationSubject;
        adminEmail.Recipients = CurrentPage.adminEmailToAddresses;
    
        emailText = CurrentPage.adminConfirmationEmail.ToString();
        emailText = emailText.Replace("[[name]]", Request.QueryString["contact-name"].ToString());
    
        items = "<ul>";
        foreach (ILineItem item in basket.Items)
        {
            items += "<li>" + item.Name + "</li>";
        }
        items += "</ul>";
    
        emailText = emailText.Replace("[[items]]", items);
        emailText = emailText.Replace("[[total]]", basket.TotalBasketPrice.AsFormattedCurrency());
        adminEmail.BodyText = emailText;
    
        MerchelloContext.Current.Gateways.Notification.Send(adminEmail);
    
        // Clear out the basket
        basket.Empty();
        basket.Save();
    
        // Redirect to the receipt page
        Response.Redirect(ExampleUiHelper.Content.GetReceipt().Url);
    }
    

    Thanks, James

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Sep 01, 2017 @ 11:33
    Simon Dingley
    0

    Hi James,

    Are you saying that your ShipRateQuote is null?

    shippingContext.GetShipRateQuotesForShipment(shipment, true).FirstOrDefault();
    

    If so, that is a possible reason. Make sure the country in the address is valid based on your shipping configurations. In my implementation, I also have my tryGetCached parameter set to false in case that makes a difference?

    Cheers, Simon

  • James Dimmer 12 posts 155 karma points
    Sep 07, 2017 @ 10:38
    James Dimmer
    100

    Hi Simon,

    Sorry for the late reply, what you said was correct, the country was invalid as it was coming through as 'United Kingdom' instead of the CountryCode of 'GB'. I have now sorted this and have moved the saving of the ship rate quote to be above when I am saving the basket and preparing the invoice.

        // Set Shipping Address (attempt 2) - add the shipping line item
        IShipment shipment = basket.PackageBasket(shipAdd).FirstOrDefault();
    
        IShippingContext shippingContext = MerchelloContext.Current.Gateways.Shipping;
        IShipmentRateQuote quote = shippingContext.GetShipRateQuotesForShipment(shipment, true).FirstOrDefault();
        shipping.SaveShipmentRateQuote(quote);
    
        basket.Save();
    
        // Create invoice for the checkout
        IInvoiceService service = MerchelloContext.Current.Services.InvoiceService;
        IInvoice invoice = payment.PrepareInvoice();
    

    Doing these has resolved the issue here.

    Thanks, James

Please Sign in or register to post replies

Write your reply to:

Draft