Copied to clipboard

Flag this post as spam?

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


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Rick 92 posts 278 karma points
    Jul 02, 2015 @ 13:00
    Rick
    0

    uCommerce multiple shipping address per customer/member

    Hi,

    I'm using uCommerce 6.7.2 for Umbraco 7.2.6 and have searched high and low on the forum for adding multiple shipping addresses per customer/member.

    I have a scenario where the returning customer is already an Umbraco Member (and signed in) and when they visit the Address page of the order process, I want to retrieve all previous shipping addresses that the customer has used in the past. At this point, the member is invited to select an address or "Enter a new address" which will then save the one that they enter for next time.

    I can't figure out where in the API docs this is?

    I can see in "_Address.cshtml":

    var shipmentAddress = TransactionLibrary.GetShippingInformation();
    

    ... which retrieves the previously used shipping address (if there is one), however, I want to return a list of all shipping addresses that matches against the currently logged in member.

    Can someone please help?

    Thanks,

    Rick

  • Martin 181 posts 740 karma points
    Jul 02, 2015 @ 18:49
    Martin
    0

    Hi Rick,

    var shipmentAddress = TransactionLibrary.GetShippingInformation();
    

    Will give you the default shipping address set on your current basket. If you want to fetch all previous shipping addresses that a Umbraco Member has placed.

    So you have to do a custom Query against uCommerce where you fetch all orders where Customer.MemberId is equal to the current logged in member.

    You can do this in different ways but simpliest will be using PurchaseOrder.All().Where().

    If you need more directions please say so and I will try to guide you :)

    Best regards Martin

  • Rick 92 posts 278 karma points
    Jul 03, 2015 @ 14:57
    Rick
    0

    Hi Martin,

    This is great - thanks - here is what I have so far:

            IEnumerable<PurchaseOrder> customerOrders = PurchaseOrder.All().Where(x => x.Customer.CustomerId == currentCustomer.Id);
            foreach (var order in customerOrders)
            {
                var orderAddress = order.GetAddress("Shipment");
                addresses.Add(orderAddress);
            }
    
            addresses = addresses.Distinct().ToList();
    

    As you can see I am getting the Shipment address from the order and adding it to my own list.

    However, when I call the Distinct() method, it is still getting all shipment addresses - even duplicate shipping addresses.

    Thanks,

    Rick

  • Martin 181 posts 740 karma points
    Jul 06, 2015 @ 16:57
    Martin
    0

    Hi Rick,

    If you want to use distinct you have to write a custom class that implements IEqualityComparer

    I'm not positive but I recall that by default it compares at the class HashCode which in your case is different since it's diffrent objects getting compared.

    Best regards Martin

  • Rick 92 posts 278 karma points
    Jul 07, 2015 @ 08:23
    Rick
    101

    Hi Martin,

    Thanks very much for the pointers - for anyone else that needs this:

    I created my custom address comparer in a separate class:

    public class AddressComparer : IEqualityComparer<OrderAddress>
    {
    
        public bool Equals(OrderAddress x, OrderAddress y)
        {
            return (x.PostalCode == y.PostalCode && x.EmailAddress == y.EmailAddress);
        }
    
        public int GetHashCode(OrderAddress obj)
        {
            return obj.PostalCode.GetHashCode() ^ obj.PostalCode.GetHashCode();
        }
    
    }
    

    You can see from the above that I wanted to match on postcode and email address.

    Then I am doing the following to get distinct addresses:

    var addresses = new List<OrderAddress>();
    
    var orderAddressComparer = new AddressComparer();
    
    IEnumerable<PurchaseOrder> customerOrders = PurchaseOrder.All().Where(x => x.Customer.CustomerId == currentCustomer.Id);
    foreach (var order in customerOrders)
    {
        var orderAddress = order.GetAddress("Shipment");
        if(!addresses.Contains(orderAddress, orderAddressComparer))
        {
              addresses.Add(orderAddress);    
        }
    }
    return addresses.ToList();
    

    Thanks again,

    Rick

  • Martin 181 posts 740 karma points
    Jul 07, 2015 @ 14:20
    Martin
    0

    Hi Rick,

    Glad to hear that the pointers guided you. H5YR! for posting your solution to help others.

    Cheers!

    Best regards Martin

Please Sign in or register to post replies

Write your reply to:

Draft