Copied to clipboard

Flag this post as spam?

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


  • Dan 1288 posts 3921 karma points c-trib
    Mar 21, 2013 @ 21:25
    Dan
    0

    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.

  • Anders Burla 2560 posts 8256 karma points
    Mar 22, 2013 @ 10:46
    Anders Burla
    0

    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

  • Dan 1288 posts 3921 karma points c-trib
    Mar 22, 2013 @ 13:15
    Dan
    0

    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:

    @using TeaCommerce.Umbraco.Web
    @using TeaCommerce.Api.Models
    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
        //Store id
        long storeId = long.Parse( Model._Store );
    
        string totalPrice = "";
        decimal totalQuantity = 0;
        decimal totalWeight = 0;
        OrderLineCollection orderLines = new OrderLineCollection();
    
        if ( TC.HasCurrentOrder( storeId ) ) {
            Order order = TC.GetCurrentOrder( storeId );
            totalPrice = order.SubtotalPrice.ToString();
            totalQuantity = order.OrderLines.Sum( o => o.Quantity );
    
            foreach ( OrderLine orderLine in order.OrderLines ) {
                totalWeight = totalWeight + orderLine.weight;
            }
        }
    }
    <a href="/cart/" class="items-in-cart"><span>@totalQuantity.ToString( "0.####" )</span> item(s) in cart</a>
    <div class="total-amount">
        <span class="total">Total</span>
        <span class="amount">
            @(totalPrice != "" ? totalPrice : "$0.00")
        </span>
        <p>Weight: @(weight) lbs</p>
    </div>

    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?

  • Anders Burla 2560 posts 8256 karma points
    Mar 22, 2013 @ 17:44
    Anders Burla
    100

    You need to use orderLine["weight"]. It is not an actual .NET property as your code tries to use :)

    Kind regards
    Anders

  • Dan 1288 posts 3921 karma points c-trib
    Mar 25, 2013 @ 19:23
    Dan
    0

    I should have tried this first actually, but what worked for me is: orderLine.Properties["weight"]

Please Sign in or register to post replies

Write your reply to:

Draft