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?
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?
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?
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
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:
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)
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
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
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
Hi Anders,
Quick update: I've upgraded to 2.2.1, but the result is the same, no change
Many thanks,
Matt
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
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
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:
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
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
is working on a reply...