Copied to clipboard

Flag this post as spam?

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


  • Luke Alderton 192 posts 509 karma points
    Dec 07, 2012 @ 11:49
    Luke Alderton
    0

    Shipping by weight

    I'm having trouble adding a Tea Commerce extension. I've followed http://rune.gronkjaer.dk/en-US/2010/11/26/how-to-use-the-tea-commerce-events/ whilst setting it up however it's not calling 'Initialize()' (or as far as I can tell its not) so I'm wondering what I'm doing wrong, the class is supposed to add shipping by weight into the shop however the shipping price isnt updated. Here's my code:

     

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Web;
    using System.Xml.XPath;
    using TeaCommerce.Data;
    using TeaCommerce.Data.Extensibility;
    using TeaCommerce.Data.Tools;
    
    /// 
    /// Summary description for TeaCommerceExtension
    /// 
    namespace TeaCommerce.WebShop.Integration
    {
        public class TeaCommerceExtension : ITeaCommerceExtension
        {
            #region ITeaCommerceExtension Members
    
            public void Initialize()
            {
                WebshopEvents.OrderLineChanged += WebshopEvents_OrderLineChanged;
                WebshopEvents.ShippingMethodChanged += WebshopEvents_ShippingMethodChanged;
                WebshopEvents.CurrencyChanged += WebshopEvents_CurrencyChanged;
                WebshopEvents.OrderLineRemoved += WebshopEvents_OrderLineRemoved;
            }
    
    
            void WebshopEvents_OrderLineRemoved(Order order, OrderLine orderLine)
            {
                UpdateOrderShippingCost(order);
            }
    
            /// 
            /// On currency change
            /// 
            /// 
            /// 
            void WebshopEvents_CurrencyChanged(Order order, Currency currency)
            {
                UpdateOrderShippingCost(order);
            }
    
            /// 
            /// On shipping method change
            /// 
            /// 
            /// 
            void WebshopEvents_ShippingMethodChanged(Order order, ShippingMethod shippingMethod)
            {
                UpdateOrderShippingCost(order);
            }
    
            void WebshopEvents_OrderLineChanged(Order order, OrderLine orderLine)
            {
                UpdateOrderShippingCost(order);
            }
    
            #endregion
    
            private void UpdateOrderShippingCost(Order order)
            {
                XPathNodeIterator allXml = umbraco.library.GetXmlAll();
    
                XPathNavigator shippingRules = allXml.Current.SelectSingleNode("//Shipping Rules");
                decimal defaultWeightInKg = (decimal)PriceHelper.ParsePrice(shippingRules.SelectSingleNode("defaultWeight").Value);
    
                decimal totalWeightInKg = 0;
                foreach (OrderLine ol in order.OrderLines)
                {
                    OrderLineProperty weightProp = ol.Properties.FirstOrDefault(p => p.Alias == "productWeight");
                    if (weightProp != null && !string.IsNullOrEmpty(weightProp.Value))
                        totalWeightInKg += (decimal)PriceHelper.ParsePrice(weightProp.Value.ToString()) * ol.Quantity;
                    else
                        totalWeightInKg += defaultWeightInKg * ol.Quantity;
                }
    
                XPathExpression weightPathXPath = XPathExpression.Compile("./weightRange");
                weightPathXPath.AddSort("upToWeight", XmlSortOrder.Ascending, XmlCaseOrder.None, string.Empty, XmlDataType.Number);
    
                XPathNodeIterator weightRules = shippingRules.Select(weightPathXPath);
                decimal shippingPricePerKgWithoutVAT = 0;
                while (weightRules.MoveNext())
                {
    
                    if (PriceHelper.ParsePrice(weightRules.Current.SelectSingleNode("upToWeight").Value) >= totalWeightInKg)
                    {
                        shippingPricePerKgWithoutVAT = (decimal)PriceHelper.ParsePrice(weightRules.Current.SelectSingleNode("shippingPrice" + order.Currency.ISOCode).Value);
                        break;
                    }
                };
    
    
                decimal totalShippingPriceWithoutVAT = shippingPricePerKgWithoutVAT * totalWeightInKg;
    
                OrderProperty shippingPriceOP = order.Properties.FirstOrDefault(p => p.Alias == "shippingPriceWithoutVAT" + order.Currency.ISOCode);
                OrderProperty totalWeightInKgProp = order.Properties.FirstOrDefault(p => p.Alias == "totalWeightInKg");
                if (shippingPriceOP == null)
                {
                    shippingPriceOP = new OrderProperty("shippingPriceWithoutVAT" + order.Currency.ISOCode, totalShippingPriceWithoutVAT.ToString());
                    order.AddProperty(shippingPriceOP);
                    totalWeightInKgProp = new OrderProperty("totalWeightInKg", totalWeightInKg.ToString());
                    order.AddProperty(totalWeightInKgProp);
                }
                else
                {
                    if (Decimal.Parse(shippingPriceOP.Value) != totalShippingPriceWithoutVAT)
                        shippingPriceOP.Value = totalShippingPriceWithoutVAT.ToString();
    
                    if (Decimal.Parse(totalWeightInKgProp.Value) != totalWeightInKg)
                        totalWeightInKgProp.Value = totalWeightInKg.ToString();
                }
                if (order.ShippingMethod.Id == 2 && order.ShippingFeeWithoutVAT != totalShippingPriceWithoutVAT)
                    order.ShippingFeeWithoutVAT = totalShippingPriceWithoutVAT;
    
                order.Save();
            }
        }
    }
    
    
  • Rune Grønkjær 1372 posts 3103 karma points
    Dec 07, 2012 @ 12:10
    Rune Grønkjær
    0

    Hi Luke,

    Have you tried debugging it on your local IIS? That's a must do when doing advanced calculations like that. You need to be able to step throught your code and see if it runs, and runs properly.

    If that's not an option you can use the Umbraco Log.Add() method to write to the Umbraco database log table. Then you can see what happens. But debugging locally is much faster.

    Your code looks alright, so I can't say what is wrong.

    /Rune

  • Luke Alderton 192 posts 509 karma points
    Dec 07, 2012 @ 12:33
    Luke Alderton
    0

    Running localy is not realy an option since it's a remote site and would take a while to run. I've added the umbraco log and suddenly my code was being called? Not sure what I did but now I'm getting an error stating that 'Shipping Rules' has an invalid token, any ideas?

  • Rune Grønkjær 1372 posts 3103 karma points
    Dec 07, 2012 @ 12:56
    Rune Grønkjær
    0

    Might be some xpath in your .net code that is wrong.

    Usually when I wan't to debug locally I copy both website files and database to my local maschine. In my experience that's much faster than fumbling in the dark :)

    /Rune

  • Luke Alderton 192 posts 509 karma points
    Dec 07, 2012 @ 12:56
    Luke Alderton
    0

    Nvm it was the wrong name for the node. Thanks for your help anyway. ;)

  • Rune Grønkjær 1372 posts 3103 karma points
    Dec 07, 2012 @ 12:57
    Rune Grønkjær
    0

    Yep, thought so. :)

  • rich hamilton 117 posts 136 karma points
    Feb 04, 2013 @ 11:17
    rich hamilton
    0

    I am using this code and am now getting the following error when adding to cart:

    Cart Error:
    <error><![CDATA[MESSAGE:
    Exception has been thrown by the target of an invocation.

    STACKTRACE:
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)

       at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)

       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

       at TeaCommerce.Presentation.TeaCommerceBase.RequestModule.invokeMethod(restExtension myExtension, Object[] paras)

    INNEREXCEPTION:
    System.MissingMethodException: Method not found: 'System.Decimal TeaCommerce.Data.Tools.PriceHelper.ParsePrice(System.String)'.

       at TeaCommerce.WebShop.Integration.TeaCommerceExtension.UpdateOrderShippingCost(Order order)

       at TeaCommerce.WebShop.Integration.TeaCommerceExtension.WebshopEvents_OrderLineChanged(Order order, OrderLine orderLine)

       at TeaCommerce.Data.Extensibility.WebshopEvents.SendOrderLineChanged(Order order, OrderLine orderLine)

       at TeaCommerce.Data.Order.Save()

       at TeaCommerce.Base.AddOrderLine(Order order, Int32 nodeId, Nullable`1 orderLineId, Decimal quantity, Boolean isUnique, Boolean overwriteQuantity, IDictionary`2 dynamicProperties)

       at TeaCommerce.Base.AddOrderLine(Int32 nodeId, Nullable`1 orderLineId, Decimal quantity, Boolean isUnique)

       at TeaCommerce.Base.AddOrderLine(Int32 nodeId, Decimal quantity)]]></error>

     

    ANy ideas what to look at next?

    Thanks

  • Luke Alderton 192 posts 509 karma points
    Feb 04, 2013 @ 11:38
    Luke Alderton
    0

     

    To to do an alernative to 'ParsePrice(System.String)' the error you need to look at is usualy under the inner exception.

    System.MissingMethodException: Method not found: 'System.Decimal TeaCommerce.Data.Tools.PriceHelper.ParsePrice(System.String)' 

    Edit:

    If you're building it in a seperate library (you should be) then make sure you have referenced the tea commerce dll or it won't find any helpers like the parse price one.

  • rich hamilton 117 posts 136 karma points
    Feb 04, 2013 @ 11:40
    rich hamilton
    0

    Luke, but it says Method not found, does it mean the value that I am trying to parse cannot be found?

  • Luke Alderton 192 posts 509 karma points
    Feb 04, 2013 @ 11:42
    Luke Alderton
    0

    Doubt 'System.String' wouldn't be found, make sure that you've referenced the Tea Commerce dll or it won't run.

  • rich hamilton 117 posts 136 karma points
    Feb 04, 2013 @ 11:53
    rich hamilton
    0

    Sure it is. It builds fine. Then I copy the DLL into the /bin of the Umbraco project.

  • rich hamilton 117 posts 136 karma points
    Feb 04, 2013 @ 12:14
    rich hamilton
    0

    Replaced with Decimal.Parse and it is working now.  But should be able to use the helpers

  • rich hamilton 117 posts 136 karma points
    Feb 05, 2013 @ 11:19
    rich hamilton
    0

    My DLLs were different version on my build and live project.

  • Luke Alderton 192 posts 509 karma points
    Feb 05, 2013 @ 11:19
    Luke Alderton
    0

    Thought so ;)

  • Søren Tidmand 129 posts 366 karma points
    Sep 03, 2013 @ 09:03
    Søren Tidmand
    0

    Hi Luke or Rich.

    I'm working on a similar solution and need to make it work with shipping by weight. Would any of you be able to make a short guide on how-to-do and maybe even share your final code? I know it's a lot to ask for, but I'm having a tight deadline on the project .. you know ... should've been yesterday'ish.

    Thx a lot.

    Søren

Please Sign in or register to post replies

Write your reply to:

Draft