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.

  • Zac 223 posts 575 karma points
    Apr 14, 2011 @ 12:41
    Zac
    0

    Getting Shipping Address in various contexts within uCommerce and Umbraco

    Hey guys,

    I'm having a little trouble with shipping and billing addresses. I've successfuly pulled the correct shipping address from within my Checkout pipeline tasks - once when doing my CalculateShippingPrice, and once when connecting with warehousing.

    In my CalculateShippingPrice method in my IShippingMethodService, I simply used the UCommerce.Entities.Shipment that gets passed to that method. From my understanding, UCommerce has already created this object for me, and I'm able to use:

    shipment.OrderAddresses.Single()

    which correctly pulls out the shipping address if there is one, or uses the billing address if not. I don't know how that works, it just does seem to. Either way, the IQueryable<OrderAddress> seems to only contain one element which is why I'm using Single()

    Confusingly, things seem to be slightly different when I get the shipping address to connect with warehousing, which I'm doing in a custom pipeline task at the end of the checkout pipeline:

    public PipelineExecutionResult Execute(PurchaseOrder order) 
    {
        OrderAddress address1 = order.OrderAddresses.ToList()[0];
        OrderAddress address2 = order.OrderAddresses.ToList()[1];
    
        string billingPostcode = address1.PostalCode;
        string shippingPostcode = address2.PostalCode;
    }

    For whatever reason, in here, the shipping address is always the second element of the PurchaseOrder.OrderAddresses, and the billing address is always the first. I don't really get it, but whatever. It makes more sense here, but it makes the shipment.OrderAddresses from before seem weird - seems like a Shipment object should have a single OrderAddress element (called ShippingAddress), and a PurchaseOrder should have two elements (BillingAddress and ShippingAddress, rather than this IQueryable mystery thing).

    Things get more hazy when I try and pull Shipping addresses from anywhere outside of the Checkout pipeline - in other words, in contexts where a Shipment or a PurchaseOrder haven't been explicitly passed to me:

    protected void Page_Load(object sender, EventArgs e)
    {
        Basket b = SiteContext.Current.OrderContext.GetBasket();
        Response.Write(b.PurchaseOrder.OrderAddresses.Count());
     }

    I've used the UCommerce.Runtime.SiteContext to get the Basket. Maybe this is wrong. But I'm doing this in the page after the user has put in their Billing and/or Shipping addresses. Thus uCommerce has that data.

    So I'm simply trying to display via a macro to the user their shipping costs based on products, postcode surcharges etc. I can calculate those fine within the IShippingMethodService, but I just can't display them on a page because I can't figure out how to get their shipping postcode.

    Using the code above, the Response.Write prints out a huge number, like 20 (I'm guessing this is related to the number of times I've gone back and messed with my address information).

    So I have three questions here:

    1) Can you see any problems with the way I'm pulling billing/shipping addresses in the first two instances?

    2) Am I missing something regarding confusing use of these various objects use IQueryable<OrderAddress> properties for all these addresses, or is this just a side effect of the complexity/power of uCommerce?

    3) How can I get the shipping address from any page outside of the checkout pipeline?

    Thanks for your time, 

    Zac

  • Søren Spelling Lund 1797 posts 2786 karma points
    Apr 14, 2011 @ 13:37
    Søren Spelling Lund
    0

    Hi Zac,

    You're doing it right. The confusing stems from two different areas: a) Our old data access layer didn't express with one-one relationships well, and b) a duplication issue with order addresses.

    a) Basically the relationship between shipment and address is one address for each shipment, but the old data access layer doesn't express it this way. Rather it does Shipment.OrderAddresses where OrderAddresses will only ever contain one address. This is fixed in EntitiesV2, but we're still using the old API for compatibility reasons.

    b) The current version creates a new address whenever EditBillingInformation and EditShipmentInformation is called that's why you're seeing a lot of addresses in PurhcaseOrder.OrderAddresses. It doesn't affect operation at all, it's just not as tidy as could be. This is fixed in 1.5.1.

    1) To answer your first question, you're doing it right. You can grab the addresses like this: and the Shipping address via Shipment.OrderAddreses.Single().

    OrderAddress billingAddress = purchaseOrder.GetBillingAddresss():
    OrderAddress shippingAddress = purchaseOrder.Shipments.First().OrderAddresses().SingleOrDefault();
    // or
    shippingAddress = purchaseOrder.GetShippingAddress("Shipping"); // name of the address as specified when CommerceLibrary:EditShipmentInformation is called. 

    2) The PurchaseOrder.OrderAddresses will contain all addresses associated uCommerce. Even the ones not currently used as billing or shipping addresses.

    3) There are a couple of different ways to do it:

    • CommerceLibrary:GetBasket() returns XML with details about the shipments including their shipping address.
    • SiteContext.Current.OrderContext.GetBasket().PurchaseOrder.GetBillingAddress(), PurchaseOrder.GetShippingAddress(AddressName), or finally Shipment.OrderAddresses.SingleOrDefault() which doesn't require you to know the name of the address
    Hope this helps.

  • Zac 223 posts 575 karma points
    Apr 14, 2011 @ 16:54
    Zac
    0

    Thank you Søren, that's great info.

    I've gone for the following for getting the user shipping address after the user has submitted their billing and (if applicable) shipping addresses

    protected void Page_Load(object sender, EventArgs e)
    {
        Basket basket = SiteContext.Current.OrderContext.GetBasket();
        OrderAddress shippingAddress = basket.PurchaseOrder.OrderAddresses.ToList().Last();
        ...
    }
    

    I found, after what you said about pre 1.5.1 uCommerce, that the last two addresses in

    Basket basket = SiteContext.Current.OrderContext.GetBasket();
    IQueryable<OrderAddress> basket.PurchaseOrder.OrderAddresses;

    contain the billing address, followed by the shipping address. So if you pull out the last OrderAddress from the above object, you've got your shipping address. This might not be the same for everyone, but in the demo store, when you update your address you do in this order:

    CommerceLibrary:EditBillingInformation($firstName, ...
    CommerceLibrary:EditShipmentInformation('Shipment', $shipment_firstName, ...

    I'm going to guess that this is why I am able to pull out the last two in that order...

    I found that the following didn't work from the context of a page for getting shipping address. I'm guessing because the Shipment object doesn't exist yet and was null, and possibly GetShippingAddress(AddressName) relies on this?:

    PurchaseOrder.GetShippingAddress(AddressName) //object not set to instance of an object
    SiteContext.Current.OrderContext.GetBasket().PurchaseOrder.Shipments.First().OrderAddresses.SingleOrDefault() //Shipments.First() fails because it's empty

    Hopefully someone finds this useful.

    Many thanks,

    Zac

Please Sign in or register to post replies

Write your reply to:

Draft