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.
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 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.
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
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
with
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
The top line should really be the following for use in an example:
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:
These eventually call into the service, but provide a nice little wrapper if you have the customer.
Sorry about the confusion =)
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!
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
This code executes just fine but when RenderAddresses is called it's throwing me a wild null ref when grabbing the customer.
I'm calling it via
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.
is working on a reply...