Copied to clipboard

Flag this post as spam?

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


  • Tolu Jemilua 39 posts 166 karma points
    Oct 11, 2017 @ 13:50
    Tolu Jemilua
    0

    GetShipToAddress setting my recently saved address to null and not displaying on frontend

    Hi,

    I've realised that calling this method sets my most recently saved address to null on my address list, which then doesn't display on the front end. It appears to still be in the backoffice but not showing on the website i'm creating

    enter image description here

    after calling this method circled in red, my recently saved address is set to null.

    any ideas why this is?

  • Puck Holshuijsen 183 posts 726 karma points
    Oct 11, 2017 @ 13:55
    Puck Holshuijsen
    101

    Hi Tolu,

    I had the same problem yesterday, but found this solution.

    In my case the user is allowed to create a new shipping address during checkout, and be able to select all their addresses and stuff.

    When a user creates a new address, this is my code (i am using an ajax call to call this method):

     [HttpGet]
        [WebMethod]
        public ActionResult SaveShippingAddressForm(string Name, string Email, string Organization, string Address1, string Locality, string PostalCode, string CountryCode, string Phone, string culture)
        {
            try
            {
                //    dynamic item = JObject.Parse(json);
                var merchello = new MerchelloHelper(false);
                string key = string.Empty;
    
    
    
                try
                {
                    ICustomerAddress address = new CustomerAddress(CurrentCustomer.Key);
    
                    address.FullName = Name;
                    address.Address1 = Address1;
                    address.AddressType = AddressType.Shipping;
                    address.CountryCode = CountryCode;
                    address.Locality = Locality;
                    address.Phone = Phone;
                    address.PostalCode = PostalCode;
                    address.Region = string.Empty;
    
    
                    IAddress iAddress = address.AsAddress(Name);
                    iAddress.Email = Email;
                    iAddress.Organization = Organization;
    
                    // Temporarily save the address in the checkout manager.
                    CheckoutManager.Customer.SaveShipToAddress(iAddress);
    
                    if (!CurrentCustomer.IsAnonymous)
                    {
                        var createdAddress = ((ICustomer)CurrentCustomer).SaveCustomerAddress(address);
                        key = createdAddress.Key.ToString();
                        CustomerContext.Reinitialize(CurrentCustomer);
                        MerchelloContext.Current.Cache.RuntimeCache.ClearCacheByKeyExpression("merchello\\.itemcache.*\\." + CurrentCustomer.Key + "\\.");
                    }
    
    
                }
                catch (Exception ex)
                {
    
                }
    
                //new address > save to customer
                CultureInfo ci = new CultureInfo(culture);
                CultureInfo.CurrentCulture = ci;
                CultureInfo.CurrentUICulture = ci;
                string html = GetShippingAddressHtml(useDefault: false, key: key);
                return Json(new { success = true, content = html }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                return Json(new { success = false, content = e.Message }, JsonRequestBehavior.AllowGet);
            }
        }
    

    these two lines:

    CustomerContext.Reinitialize(CurrentCustomer); 
        MerchelloContext.Current.Cache.RuntimeCache.ClearCacheByKeyExpression("merchello\\.itemcache.*\\." + CurrentCustomer.Key + "\\.");
    

    Make sure the freshly saved address, is also refreshed inside the cache.

    EDIT:

    I had the issue that when i called SaveBillToAddress/SaveShipToAddress, my address was deleted from the Database. This is because the customer in the cache is being saved again. When you use those 2 lines, it makes sure that the customer in the cache is being refreshed.

    hope this helps!

    Puck

  • Tolu Jemilua 39 posts 166 karma points
    Oct 11, 2017 @ 14:11
    Tolu Jemilua
    0

    That worked like a charm, Thank you so much

  • Puck Holshuijsen 183 posts 726 karma points
    Oct 11, 2017 @ 14:23
    Puck Holshuijsen
    0

    Good to hear! And you're welcome

Please Sign in or register to post replies

Write your reply to:

Draft