Copied to clipboard

Flag this post as spam?

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


  • Matt Bliss 176 posts 234 karma points c-trib
    Nov 12, 2013 @ 19:44
    Matt Bliss
    0

    CalculateOrderLineUnitPrice and custom properties

    I'm overriding the CalculateOrderLineUnitPrice function in my own OrderCalculator class as per the example in the Tea Commerce documentation.

    The orderline I'm adding contains custom properties and I want to use the values from the custom properties in my calculation of the unit price but when the function gets called to calculate the unit price the custom properties for the orderline do not yet seem to have any values set.

    Is there anything that can be done to alter the order of execution so that the custom properties are populated before the orderline unit price is calculated?

    (TeaCommerce v2.2.0, Umbraco v4.11.10)

  • Anders Burla 2560 posts 8256 karma points
    Nov 12, 2013 @ 20:51
    Anders Burla
    0

    Hi Matthew

    Update to 2.2.1 first of all - just released today :)

    The custom properties should be there because the calculator is run when the order is saved - and the entire order line object should be added before that along with its custom properties. Have you tried to add another one so the quantity is 2 and see if the properties are there the second time?

    Kind regards
    Anders

  • Matt Bliss 176 posts 234 karma points c-trib
    Nov 13, 2013 @ 10:08
    Matt Bliss
    0

    Hi Anders,

    I'll try the update to 2.2.1 to see if that works. 

    The CalculateOrderLineUnitPrice is called when I add an item to the cart using a post to the HTML API AddOrUpdateOrderLine. I'm sending a product with three custom fields (to be used in the calculation of the unit price) and also a variable number of additional products linked to the main product in a bundle.

    Adjustments I've made to the display of the cart - and inspection of the SQL tables - show that the custom properties are added to the orderline, but only if I put a try/catch around the

    orderline.Properties.Get(alias).Value

    which returns a Null in CalculateOrderLineUnitPrice (Later it returns values in the cart display without problem). If I don't catch the error on Properties.Get() then only a partial orderline gets added to the cart.

    Should I be hooking into an event that gets called later in the process?

    Thanks,
    Matt

  • Matt Bliss 176 posts 234 karma points c-trib
    Nov 13, 2013 @ 10:36
    Matt Bliss
    0

    Hi Anders,

    Quick update: I've upgraded to 2.2.1, but the result is the same, no change

    Many thanks,
    Matt 

  • Anders Burla 2560 posts 8256 karma points
    Nov 13, 2013 @ 13:21
    Anders Burla
    100

    I have just tested using a Tea Commerce 2.2.1 and the example 2 on this url. It works fine and I can see that it has two properties.
    http://documentation.teacommerce.net/html-api/order-line/addorupdateorderline/

    Kind regards
    Anders

  • Matt Bliss 176 posts 234 karma points c-trib
    Nov 13, 2013 @ 13:51
    Matt Bliss
    0

    Hi Anders,

    Looking at it again with fresh eyes I've found the problem. You are absolutely right the properties were avaialble at that point. The problem actually elsewhere in the same line of code. It was related to the parsing of one of the property strings to a DateTime which was failing, even though the date string was valid and was actually created by the DateTime.ToString() in the first place

    Sorry for the false alarm!

    Many thanks,
    Matt 

  • Matt Bliss 176 posts 234 karma points c-trib
    Nov 13, 2013 @ 15:46
    Matt Bliss
    0

    Further info for anyone else with a similar problem.

    The problem was caused by parsing a DateTime. When the date was added to the cart the Umbraco page is aware of the current culture and formats the string representation of the date accordingly. In the Tea Commerce OrderCalculator the default culture for the site is used as the code is not aware of the culture for the product page. In this case the two cultures (IIS default and umbraco page) were different and therefore the DateTime.TryParse could not sucessfully parse the date string and returned a null instead.

    I used the following function to return the culture for the product node:

            CultureInfo GetNodeCulture(int id)
            {
                string la = string.Empty;
                Domain[] domains = umbraco.library.GetCurrentDomains(id);
                if (domains != null && domains.Any())
                {
                    return new CultureInfo(domains[0].Language.CultureAlias);
                }
                return CultureInfo.CurrentCulture;
            }
  • Anders Burla 2560 posts 8256 karma points
    Nov 14, 2013 @ 10:18
    Anders Burla
    0

    Hi Matthew

    Thanks for posting your solution. You can also just get the language from the orders language (if you have a culture set for your domain in Umbraco)

    new CultureInfo( Language.GetAllAsList().SingleOrDefault( l => l.id == order.Value ).CultureAlias);

    Kind regards
    Anders

  • Matt Bliss 176 posts 234 karma points c-trib
    Nov 14, 2013 @ 10:32
    Matt Bliss
    0

    Thanks Anders,

    That's worth knowing as an alternative - especially if needed in a function at order level rather than orderline level. 

    Many thanks,
    Matt 

Please Sign in or register to post replies

Write your reply to:

Draft