Copied to clipboard

Flag this post as spam?

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


  • James Dodwell 21 posts 73 karma points
    May 21, 2015 @ 15:24
    James Dodwell
    0

    Issue with adding customer addresses via the front end

    We are using the Merchello Bazaar code as a guide on our current eCommerce project.  We have just run into an issue with adding addresses via the front-end.  We can sucessfully register a new user via the front-end (http://localhost/account) but adding either type of address (Billing or Shipping) does nothing.  We have checked the database and the addresses aren't being saved.

    When we add addresses for the user via the backoffice they are getting saved to the database.  The addresses then appear in the front-end but only after logging out and back in.  We are also then able to edit the address details via the front-end.

    We don't need a fix for Bazaar but just need to understand what the code should be so that these addresses can be added via the front-end.  Any help would be much appreciated.

    James

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    May 21, 2015 @ 20:50
    Rusty Swayne
    0

    Hi James,

    I missed checking the adding the addresses on the account page when I reused the model in the checkout ... oops

    The ModelState is returning false for IsValid due to the "Key" property on the CustomerAddressModel object.

    The quick fix is to replace the

     if(!ModelState.IsValid) ...
    

    with

           var isValid = ModelState.IsValidField("FullName") && ModelState.IsValidField("Label")
               && ModelState.IsValidField("Address1") && ModelState.IsValidField("Locality")
               && ModelState.IsValidField("PostalCode") && ModelState.IsValidField("CountryCode");
    
            if (!isValid) return this.CurrentUmbracoPage();
    

    Reviewing things, I noticed I saved customer addresses in two different ways in the Bazaar - another oops, sorry about the confusion there.

    In the AccountOperationsController the

            MerchelloServices.CustomerService.Save(customerAddress);  
            CustomerContext.Reinitialize(CurrentCustomer);
    

    The top line should really be the following for use in an example:

     MerchelloContext.Current.CustomerService.Save(customerAddress);
    

    e.g. => MerchelloServices == MerchelloContext.Current.Services;

    In any case, this approach to saving a customer address does so directly against the CustomerService.

    In the SalePreparationOperationsController I went the route of using the extension methods off of the ICustomer.

    Around line 88:

     //// at this point we know the customer is an ICustomer 
     var customer = (ICustomer)CurrentCustomer;
    
     if (saveBilling)
     {
         customer.CreateCustomerAddress(billingAddress, model.BillingAddressLabel, AddressType.Billing);
     }
    
     if (saveShipping)
     {
          if (model.BillingIsShipping) model.ShippingAddressLabel = model.BillingAddressLabel;
          customer.CreateCustomerAddress(shippingAddress, model.ShippingAddressLabel, AddressType.Shipping);
     }
    

    These eventually call into the service, but provide a nice little wrapper if you have the customer.

    Sorry about the confusion =)

  • James Dodwell 21 posts 73 karma points
    May 22, 2015 @ 13:23
    James Dodwell
    0

    Thanks Rusty,

    Your suggested work-around has enabled us to make progress, members can now manage their own address lists independent of making sales!

    Many thanks for your speedy and thorough response, as always!

  • John Mullan 8 posts 28 karma points
    Jun 30, 2015 @ 10:43
    John Mullan
    0

    Sorry to revive a solved issue but I didn't think my issue deserves a new thread.

    I'm creating an Address Manager page as well and used the code above but I've come across a rather strange issue - the CustomerService randomly cannot find the customer with CurrentCustomer.Key

        [HttpPost]
        public ActionResult DeleteAddress(string key)
        {
            Guid guid;
    
            if ( Guid.TryParse( key, out guid ) )
            {
                var address = MerchelloServices.CustomerService.GetAddressByKey( guid );
                if (address != null)
                {
                    MerchelloContext.Current.Services.CustomerService.Delete(address);
                    CustomerContext.Reinitialize( CurrentCustomer );
                }
            }
    
            return RedirectToUmbracoPage(DeliveryId);
        }
    

    This code executes just fine but when RenderAddresses is called it's throwing me a wild null ref when grabbing the customer.

        public ActionResult RenderAddresses(AddressType addressType)
        {
            var model = new DeliveryViewModel();
    
            var customer = MerchelloServices.CustomerService.GetByKey(CurrentCustomer.Key);
    
            model.AddressType = addressType;
            model.Addresses = customer.Addresses.Where(a => a.AddressType == addressType).Select(address => new AddressModel
            { 
                Key = address.Key,
                Address1 = address.Address1,
                Address2 = address.Address2,
                CountryCode = address.CountryCode,
                Locality = address.Locality,
                Phone = address.Phone,
                PostalCode = address.PostalCode,
                Region = address.Region,
            }).ToList();
    
            return PartialView("_Addresses", model);
    
        }
    

    I'm calling it via

    @Html.Action("RenderAddresses", "Checkout", new { area = "MerchelloCheckout", addressType = AddressType.Billing})
    

    Is there an issue with the CustomerService in that it can't find the customer by Key? Could CustomerContext.Reinitialize be the culprit?

    Cheers!

    [edit: Grammar]

    [edit] I removed the CustomerContext.Reinitialize for now and it seems to be working just fine.

Please Sign in or register to post replies

Write your reply to:

Draft