Calculate cumulative weight of items in cart (Tea Commerce 2)
Hi,
I'm building a /base extension to calculate shipping prices via a third-party api. To do this I need to calculate the cumulative weight of all items in the cart. How would I go about doing this? I've added a property to the product document type called 'weight' and, although I'm not entirely sure what it does I've also added the 'weight' alias to the 'Product property aliases' property on the product tab of the store in the Tea Commerce admin area, which I'm hoping makes this property 'discoverable' by Tea Commerce somehow.
You are on the right track. When you add the property alias in the "Product property alias" Tea Commerce will auto copy that property to the order line from the Umbraco node. Now you can use the TC.GetCurrentOrder method to get the order and loop all order lines and for each find the weight property and make a sum of it.
I'm just trying to do this in Razor to begin with, just to get my head around it before I implement this in the base method. I've basically changed the 'mini cart' razor macro to this:
However, it's not recognising the 'weight' property on the order line:
'TeaCommerce.Api.Models.OrderLine' does not contain a definition for 'weight' and no extension method 'weight' accepting a first argument of type 'TeaCommerce.Api.Models.OrderLine' could be found (are you missing a using directive or an assembly reference?)
Can you see what I'm doing wrong? I didn't think I had to grab the Umbraco node to get the property from there (that's the whole point of adding that property to the order line, right?) but maybe I do?
Calculate cumulative weight of items in cart (Tea Commerce 2)
Hi,
I'm building a /base extension to calculate shipping prices via a third-party api. To do this I need to calculate the cumulative weight of all items in the cart. How would I go about doing this? I've added a property to the product document type called 'weight' and, although I'm not entirely sure what it does I've also added the 'weight' alias to the 'Product property aliases' property on the product tab of the store in the Tea Commerce admin area, which I'm hoping makes this property 'discoverable' by Tea Commerce somehow.
Any pointers would be much appreciated.
Thanks folks.
Hi Dan
You are on the right track. When you add the property alias in the "Product property alias" Tea Commerce will auto copy that property to the order line from the Umbraco node. Now you can use the TC.GetCurrentOrder method to get the order and loop all order lines and for each find the weight property and make a sum of it.
Kind regards
Anders
I'm just trying to do this in Razor to begin with, just to get my head around it before I implement this in the base method. I've basically changed the 'mini cart' razor macro to this:
However, it's not recognising the 'weight' property on the order line:
Can you see what I'm doing wrong? I didn't think I had to grab the Umbraco node to get the property from there (that's the whole point of adding that property to the order line, right?) but maybe I do?
You need to use orderLine["weight"]. It is not an actual .NET property as your code tries to use :)
Kind regards
Anders
I should have tried this first actually, but what worked for me is: orderLine.Properties["weight"]
is working on a reply...