Copied to clipboard

Flag this post as spam?

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


  • Sam Gooch 24 posts 116 karma points
    Oct 09, 2017 @ 20:53
    Sam Gooch
    0

    Save Shipping Address to Customer after an order

    Hi

    At what point of the chain is the best place to save the shipping address against the authenticated customer? I've tried hooking into the Invoice Created event but to no avail. It seems as though the CustomerKey is always null.

    My code is as follows...

    private void InvoiceService_Created(IInvoiceService sender, Merchello.Core.Events.NewEventArgs<IInvoice> newEventArgs)
        { 
            if (!newEventArgs.Entity.CustomerKey.HasValue)
                return;
    
            var shippingAddress = newEventArgs.Entity.GetShippingAddresses().FirstOrDefault();
            if (shippingAddress == null)
                return;
    
            var customer = MerchelloContext.Current.Services.CustomerService.GetByKey(newEventArgs.Entity.CustomerKey.Value);
            if (customer == null)
                return;
    
            var exists = customer.Addresses.Any(x => x.AddressType == AddressType.Shipping && x.PostalCode.Equals(shippingAddress.PostalCode, StringComparison.InvariantCultureIgnoreCase));
            if (exists)
                return;
    
            var address = customer.CreateCustomerAddress(shippingAddress, string.Empty, AddressType.Shipping);
            customer.SaveCustomerAddress(address);
        }
    

    Thanks, Sam

Please Sign in or register to post replies

Write your reply to:

Draft